2014-03-03 46 views
0

我一直在試圖使通過把這些代碼紋理蘋果:如何設置紋理型號編程

GameObject cube = Instantiate(Resources.Load("apple", typeof(GameObject))) as GameObject; 
Texture my3dModelTexture = Resources.Load("Textures/apple2", typeof(Texture)) as Texture; 
cube.renderer.material.mainTexture = my3dModelTexture; 

然而,我的蘋果似乎是無網紋。但後來我在一個立方體上嘗試了相同的概念,它正在工作。

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 

有人可以幫忙嗎?我一直在嘗試很多方法,但是他們都沒有工作,我仍然無法找到原因。

+0

'Resources.Load'如果沒有這樣的資源,則返回null;如果輸入爲空或具有無法成功轉換的類型,那麼'as'運算符將返回null。如果將材質的紋理設置爲null,則會獲得無紋理的對象。因此,您可以運行MonoDevelop的調試器或使用'Debug.Log()'來檢查:您是否設置了空紋理?除此之外,你是否使用了首先接受紋理參數的材質/着色器? – rutter

回答

0

//假設有圖像文件apple.png或JPG的資源文件夾

Texture2D tex = Resources.Load(apple) as Texture2D; 
if(tex == null) 
    Debug.LogError("Error loading image"); 
else 
    Debug.Log ("Scuccess in loading image"); 
cube.renderer.material.mainTexture = tex;