- Home /
Loading Textures from AssetBundle
I am currently working on a Vuforia AR Project. I am working on a part where I have an assetbundle containing png textures. There are around 177 textures. I want to download them and make them play in a sequence on a plane. This is my code for the downloading and playing part:
public class TextureLoadScript : MonoBehaviour
{
Texture2D[] tex = new Texture2D[177];
Material GoMaterial;
int frameCounter = 0;
public TrackableBehaviour trackableBehaviour;
public string imageSequenceName="ImageSequenceName_";
public string FolderName="Resources/ImageSequence";
public int numOfFrames;
AssetBundle bundle;
bool playing;
bool isLoading;
void Awake()
{
this.GoMaterial = this.renderer.material;
}
// Use this for initialization
void Start ()
{
if (trackableBehaviour) {
trackableBehaviour.RegisterTrackableEventHandler (this);
}
tex[0] = (Texture2D)bundle.Load(FolderName+imageSequenceName+"00000",typeof(Texture2D));
}
// Update is called once per frame
void Update ()
{
if(isLoading)
{
StartCoroutine("Download");
}
if(playing)
{
StartCoroutine("Play",0.0588f);
GoMaterial.mainTexture = tex[frameCounter];
}
}
IEnumerator Download()
{
string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
WWW download = WWW.LoadFromCacheOrDownload(url,1);
yield return download;
if(download.error!=null)
{
Debug.LogError(download.error);
}
bundle=download.assetBundle;
Debug.Log ("Bundle Loaded Successfully"+bundle.name);
isLoading = true;
}
IEnumerator Play(float delay)
{
yield return new WaitForSeconds(delay);
if(frameCounter<numOfFrames-1)
{
++frameCounter;
tex[frameCounter] = (Texture2D)bundle.Load(FolderName+imageSequenceName+frameCounter.ToString("D5"),typeof(Texture2D));
}
else
{
playing = false;
}
}
During runtime it shows object reference not set to the instance of an object. I can see I am close enough. But I am not able to proceed further. Kindly help me on this
Why are you doing StopCoroutine at the end of the coroutines? Also you're missing to say what is the line raising the NullReference exception, and please edit your post and adjust the text so that the code is correctly displayed, it's a bit unreadable as it is now. And you're also missing the code that you use to call the two functions.
Sorry about the alignment. I hope this is fine. I am getting the null exception errors in lines 72 and 34.
Answer by SkaredCreations · Dec 15, 2014 at 11:18 AM
This code has some logic errors:
in Start you're accessing "bundle" but it isn't initialized yet, since you're initializing it in Download
numOfFrames is never assigned, so it'll be ever 0
I think bundle.Load accepts only the name of the resource, not a path
Here is how I would do:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TextureLoadScript : MonoBehaviour {
Material GoMaterial;
public TrackableBehaviour trackableBehaviour;
public string imageSequenceName="ImageSequenceName_";
AssetBundle bundle;
bool playing;
bool isLoading;
void Start ()
{
if (trackableBehaviour) {
trackableBehaviour.RegisterTrackableEventHandler (this);
}
GoMaterial = renderer.material;
isLoading = true;
StartCoroutine(Download());
StartCoroutine(Play(0.0588f));
}
IEnumerator Download()
{
string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
WWW download = WWW.LoadFromCacheOrDownload(url,1);
yield return download;
if (!string.IsNullOrEmpty(download.error))
{
Debug.LogError(download.error);
}
else
{
bundle = download.assetBundle;
Debug.Log ("Bundle Loaded Successfully" + bundle.name);
}
isLoading = false;
}
IEnumerator Play(float delay)
{
while (isLoading)
yield return null;
playing = true;
if (bundle != null)
{
List<Texture2D> textures = new List<Texture2D>((Texture2D[])bundle.LoadAll(typeof(Texture2D)));
for (int frameCounter = 0; frameCounter < textures.Count; ++frameCounter) {
Texture2D tex = textures.Find(t => t.name.Equals(imageSequenceName + frameCounter.ToString("D5")));
if (tex != null)
GoMaterial.mainTexture = tex;
yield return new WaitForSeconds(delay);
}
}
playing = false;
}
}
Thanks a lot SkaredCreations! I tried running this code. It says InvalidCastException at line :52 And I have a question? How will the textures be applied everyframe without the update function?
I managed to fix it myself. Now its working. Thanks a lot for your help