Hi,
I’m currently developing an AR app with Unity and Vuforia.
I’m loading the 3D objects from my server and instantiating them at the recognition of the image target.
Fact is that the objects are quite heavy and the instantiating can take up to 10s. If the user moves away from the target image during this lapse of time then the object is instantiate in front of the camera and will stay there anyway.
Here is how I Instantiate my objects:
using(WWW www = new WWW(assetDwnLink))
{
yield return www;
File.WriteAllBytes(Application.persistentDataPath + "/" + name, www.bytes);
AssetBundle assetBundle = www.assetBundle;
//GameObject gameobj = assetBundle.Load (name) as GameObject;
request = assetBundle.LoadAsync (name, typeof(GameObject));
yield return request;
GO = Instantiate(request.asset) as GameObject;
//GO = Instantiate(gameobj) as GameObject;
GO.transform.parent = GameObject.Find("AugmentationObject").transform;
GO.transform.localPosition = new Vector3(properties[0], properties[1], properties[2]);
GO.transform.eulerAngles = new Vector3(properties[3], properties[4], properties[5]);
GO.transform.localScale = new Vector3(properties[6], properties[7], properties[8]);
rotateAngles = new Vector3(properties[3], properties[4], properties[5]);
distance = properties[6];
zoomMin = properties[9];
zoomMax = properties[10];
zoomFacteur = properties[11];
GO.SetActive(true);
assetBundle.Unload(false);
www.Dispose();
}
And here’s how I’m actually destroying the object when the target is lost:
public void OnTrackingLost()
{
Debug.Log ("OnTrackingLost called");
if(GO)
{
Debug.Log("Destroy called");
Destroy(GO);
}
else if(request != null && request.progress == 1)
{
request = null;
Debug.LogError ("done");
}
}
After some hours reading the API, and reading about CancelInvoke etc. it doesn’t look like there’s a way to stop the Instantiate while it is instantiating…
Though, if any of way have an other idea of how to work around that, or know if I’m missing something, I’m listening to you!
As said in the answers I’m now using Async Loading so I can work with .progress and .isDone. But I can’t find the condition to use and the action to do to cancel the Instantiate.
Thanks a lot!