Unity纹理图像作为嵌入资源

本文关键字:嵌入资源 纹理图像 Unity | 更新日期: 2023-09-27 18:18:51

我正在开发一个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

Unity纹理图像作为嵌入资源

问题解决了。变量初始化错误。下面的代码可以工作:

 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));