- Home /
AssetBundle.Load() to Texture2D
Hello, these assetbundles are really hard to understand how to use. Here's my current problem :
I got an asset bundle, i load an object, it's a texture 2D, and i cast it into Texture2D, but i get the folloxing error :
Instantiating a non-readable 'de_1__main' texture is not allowed! Please mark the texture readable in the inspector or don't instantiate it.
UnityEngine.Object:Instantiate(Object)
LoadGallery:Update() (at Assets/Script/LoadGallery.cs:57)
This is the code calling it :
Object obj;
for (int i = 0; i < galleryObjects.Length; i++)
{
obj = tmp.assetBundle.Load(pars.Country.ToLower() + "_" + (i + 1).ToString() + "__main");
galleryObjects[i].renderer.material.mainTexture = (Texture2D)Instantiate(obj);
galleryObjects[i].transform.Rotate(new Vector3(0, 0, 45));
}
Have i misunderstood the Load() part of the assetbundles? Because i don't see other ways to cast an Object into a Texture2D.
Answer by cregox · Dec 22, 2011 at 05:00 PM
I'm just guessing here, but maybe you want to do this instead (as advised by the error):
galleryObjects[i].renderer.material.mainTexture = (Texture2D)obj;
Hi, thank you for the answer, i already tried it, but i still got an error, which is found also with the code above :
InvalidCastException: Cannot cast from source type to destination type.
That's why i thought what i did was wrong. The error occurs on the last image, image 8. "i" goes from 0 to 7, and images go from 1 to 8, so i shouldn't get an error with (i + 1). But somehow the last image seems to be the problem here.
Edit : After checking the images folders, i found out the error comes from the fact that the 8th image is in fact a video. So, i suppose i'll go try to find how to distinguish images from videos in assetbundles. Anyway, validating this answer.
Thanks for the feedback! And yeah, sometimes we just need a little shaking. :)
Answer by flamy · Dec 23, 2011 at 08:53 AM
did you try
galleryObjects[i].renderer.material.mainTexture = obj as Texture2D;
this should work!
Actually, the problem is solved, but thank you for your concern nonetheless.