Unity纹理图像作为嵌入资源

时间:2015-10-22 09:52:44

标签: c# dll unity3d texture2d

我正在开发一个Unity托管插件。目前dll工作正常但是当我想使用嵌入式资源(如图像)时,Texture2D不会加载字节。有人遇到过同样的情况吗?

这是我的代码,看看图像是否真的加载到字节并且它可以工作:

try
    {
        System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream myStream = myAssembly.GetManifestResourceStream("QBoard");
        img = ReadFully (myStream);
        print(img.Length);

    }
    catch
    {
        print("Error accessing resources!");
    }

但是,当我想从dll加载图像时如下:

questionTexture.LoadImage(img);
GUI.Box (new Rect (dWidth/2-50, dHeight/2-50,200,50),new 
GUIContent(qlist.text,questionTexture));

结果是Unity编辑器中的测试项目出现以下错误:

NullReferenceException: Object reference not set to an instance of an object

1 个答案:

答案 0 :(得分:0)

它解决了。变量初始化是错误的。以下代码正在运行:

 try
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream myStream = myAssembly.GetManifestResourceStream("QBoard");
        img = ReadFully (myStream);

    }
    catch
    {
        print("Error accessing resources!");
    }

    Texture2D myTexture = new Texture2D(2, 2);
    myTexture.LoadImage(img);

    GUI.Box (new Rect (dWidth/2-50, dHeight/2-50,200,50),new GUIContent(myObject.text,myTexture));