I have more than 10 assetbundles containing texture sequences. I need to stream them during runtime on the AR camera. So when i detect an image target, the texture sequence should start playing. I have achieved this. But when I load multiple assetbundles, I see that the camera shows hiccups and when i detect the image target, and the streaming experiences lagging. I am building this on Unity 4.6 pro on Mac. The project is for android. This is my code. Kindly need some suggestions as I have not been able to find sufficient information on this
Texture texture;
//The material to be applied on the plane
Material goMaterial;
//The AssetBundle containing the texture bundle
AssetBundle bundle;
//To keep a count of the number of frames
int frameCounter = 0;
// GameObject load;
//The url of the location containing the asset bundle
string url = "http://mobilesandbox.cloudapp.net/Mybundle1_android.unity3d";
//The trackable image target
public TrackableBehaviour trackablebehaviour;
//The image sequence name as stored in the AssetBundle
string ImageSequenceName = "Mybundle_";
//The number of frames for the Host Animation
public int numOfFrames;
public AudioClip audioClip;
//The boolean for playing the animation
bool playing;
void Awake()
{
//Applying the rendering material
this.goMaterial = this.renderer.material;
}
void Start ()
{
StartDownload ();
//Detect the trackable behaviour
if (trackablebehaviour)
{
trackablebehaviour.RegisterTrackableEventHandler (this);
}
//Start the AssetBundle Download
Debug.Log ("Download started");
}
// Update is called once per frame
void FixedUpdate ()
{
if(playing)
{
//Start the Host Animation
StartCoroutine("Play",0.05f);
goMaterial.mainTexture=this.texture;
}
}
//This function performs the AssetBundle download operation
void StartDownload()
{
bundle = AssetBundleManager.getAssetBundle (url, 1);
if (!bundle)
{
StartCoroutine("DownloadAsset");
}
}
//This coroutine has a reference to the AssetBundleManager class that takes care of the assetbundle loading operation
IEnumerator DownloadAsset()
{
yield return StartCoroutine (AssetBundleManager.downloadAssetBundle (url, 1));
bundle=AssetBundleManager.getAssetBundle(url,1);
}
//This coroutine plays the texture sequence
IEnumerator Play(float delay)
{
//yield return StartDownload ();
yield return DownloadAsset ();
yield return new WaitForSeconds (delay);
if (frameCounter < numOfFrames - 1) {
++frameCounter;
this.texture = (Texture)bundle.Load (ImageSequenceName + frameCounter.ToString ("D5"), typeof(Texture));
Debug.Log ("Content Streamed: " + goMaterial.mainTexture);
}
else
{
playing = false;
audio.Stop ();
audio.clip = null;
}
//A delay is given so that the assetbundle is unloaded after the host sequence finishes playing
yield return new WaitForSeconds (60.0f);
AssetBundleManager.Unload (url, 1, false);
StopCoroutine ("Play");
}
public void OnTrackableStateChanged (TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
switch (newStatus) {
case TrackableBehaviour.Status.DETECTED:
case TrackableBehaviour.Status.TRACKED:
case TrackableBehaviour.Status.EXTENDED_TRACKED:
OnTrackingFound ();
break;
default:
OnTrackingLost ();
break;
}
}
private void OnTrackingFound ()
{//Reset the frame counter and the audio clip
Debug.Log ("Tracking Found");
if (!playing) {
frameCounter = 0;
audio.clip = audioClip;
audio.Play ();
}
playing = true;
}
private void OnTrackingLost ()
{
}
I am making use of the Assetbundle manager script found on the Unity website to download and cache the assetbundles. But it takes some time to download and while it does, the hiccups occur.
↧