2015-10-22 32 views
1

我正在開發一個Unity託管插件。目前,然而,當我想用​​嵌入式資源,如圖像,Texture2D不加載字節的dll工作正常。有沒有人遇到過這種情況?將紋理圖像統一爲嵌入式資源

這裏是我的代碼,看看如果圖像是真正加載到字節,它的工作原理:

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

結果是在統一編輯器中的測試項目出現以下錯誤:

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

回答

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