简体   繁体   中英

Texture2D is working in Editor but not in Android device

I am trying to change the texture of my object with this code:

Texture2D baileyburlwood = Instantiate(Resources.Load("bailey burlwood") as Texture2D);
    myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;

It is working perfectly fine in the editor, the texture changes but when I tried to run it in my android device, my object just goes black. There is also no error or any warning. Pls help! Thanks!

I am using Unity 5.5.1f btw

我添加了文件位置的屏幕截图

From the screenshot in your updated question, the image you want to load is called bailey burlwood.jpg which is already in the Resources folder..

Herein lies the problem:

Instantiate(Resources.Load("bailey burlwood") as Texture2D);

You instantiate prefabs, scripts and components not normal classes like Texture2D .

Your code would have worked if bailey burlwood.jpg is bailey burlwood.prefab and you load it with GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject; but that's not the case here.

Since the "bailey burlwood" file is a JPG file, you should load like this:

Texture2D baileyburlwood = Resources.Load("bailey burlwood") as Texture2D;
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;

Note that there is no Instantiate function involved. See this post for how to load other image files with different image settings when using the Resources folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM