Log in or register to post comments

load assetbundle with WWW and StreamingAsset

November 19, 2019 - 4:16am #1

i want to make 3D object can load with www and streamingasset, but i have a problem in load assetbundle with streamingasset after www

example: i scan image one and image one have metadata to load assetbundle with WWW object its show. and then i scan image two load assetbundle with streamingasset the object cannot show. but debug.log show "the asset bundle cannot be loaded because another assetbundle with the same file..."

and this the code:

BundleAugmneterStream

</p>
<p>using System.Collections.Generic;<br>
	using UnityEngine;<br>
	using Vuforia;<br>
	using System.IO;</p>
<p><br>
	public class BundleAugmenterStream : MonoBehaviour, ITrackableEventHandler {</p>
<p>    private string AssetName;<br>
	    private int Version;<br>
	    private GameObject mBundleInstance = null;<br>
	    public static TrackableBehaviour mTrackableBehaviour;<br>
	    private bool mAttached = false;<br>
	    private string metadata = "";<br>
	    private GameObject cloningan;</p>
<p>    // Use this for initialization<br>
	    void Start()<br>
	    {</p>
<p>        mTrackableBehaviour = GetComponent<TrackableBehaviour>();<br>
	        if (mTrackableBehaviour)<br>
	        {<br>
	            mTrackableBehaviour.RegisterTrackableEventHandler(this);<br>
	        }<br>
	    }</p>
<p>    void OnDestroy()<br>
	    {<br>
	        if (mTrackableBehaviour)<br>
	            mTrackableBehaviour.UnregisterTrackableEventHandler(this);<br>
	    }</p>
<p>    // Update is called once per frame<br>
	    IEnumerator DownloadAndCache()<br>
	    {<br>
	        metadata = CloudHandler.mTargetMetadata;<br>
	        var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "bakteri")); // bakteri nama assetbundle<br>
	        yield return bundleLoadRequest;</p>
<p>        var myLoadedAssetBundle = bundleLoadRequest.assetBundle;<br>
	        if (myLoadedAssetBundle == null)<br>
	        {<br>
	            Debug.Log("Failed to load AssetBundle!");<br>
	            yield break;<br>
	        }</p>
<p>        var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync<GameObject>(metadata);<br>
	        yield return assetLoadRequest;</p>
<p>        //GameObject prefab = assetLoadRequest.asset as GameObject;<br>
	        //Instantiate(prefab);<br>
	        mBundleInstance = Instantiate(assetLoadRequest.asset) as GameObject;<br>
	        mBundleInstance.transform.gameObject.SetActive(false);<br>
	        cloningan = Instantiate(mBundleInstance) as GameObject;<br>
	        cloningan.transform.parent = mTrackableBehaviour.transform;<br>
	        cloningan.transform.localScale = new Vector3(100f, 100f, 100f);<br>
	        cloningan.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);<br>
	        cloningan.transform.localRotation = Quaternion.identity;<br>
	        cloningan.transform.gameObject.SetActive(true);<br>
	        myLoadedAssetBundle.Unload(false);<br>
	    }</p>
<p>    public void OnTrackableStateChanged(<br>
	    TrackableBehaviour.Status previousStatus,<br>
	    TrackableBehaviour.Status newStatus)<br>
	    {<br>
	        if (newStatus == TrackableBehaviour.Status.DETECTED ||<br>
	            newStatus == TrackableBehaviour.Status.TRACKED)<br>
	        {<br>
	            OnTrackingFound();<br>
	        }<br>
	        else<br>
	        {<br>
	            OnTrackingLost();<br>
	        }<br>
	    }</p>
<p>    private void OnTrackingFound()<br>
	    {<br>
	        StartCoroutine(DownloadAndCache());<br>
	    }</p>
<p><br>
	    private void OnTrackingLost()<br>
	    {<br>
	        Destroy(cloningan);<br>
	    }</p>
<p>    // Update is called once per frame<br>
	    void Update () {<br>
	 <br>
	}<br>
	}

AssetBundleAugmenter

</p>
<p>using System.Collections.Generic;<br>
	using UnityEngine;<br>
	using Vuforia;</p>
<p>public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler {</p>
<p>    private string AssetName;<br>
	    private int Version;<br>
	    private GameObject mBundleInstance = null;<br>
	    public static TrackableBehaviour mTrackableBehaviour;<br>
	    private bool mAttached = false;<br>
	    private string metadata = "";<br>
	    private GameObject cloningan;</p>
<p>    // Use this for initialization<br>
	    void Start () {<br>
	       <br>
	        mTrackableBehaviour = GetComponent<TrackableBehaviour>();<br>
	        if (mTrackableBehaviour)<br>
	        {<br>
	            mTrackableBehaviour.RegisterTrackableEventHandler(this);<br>
	        }<br>
	       <br>
	    }</p>
<p>    void OnDestroy()<br>
	    {<br>
	        if (mTrackableBehaviour)<br>
	            mTrackableBehaviour.UnregisterTrackableEventHandler(this);<br>
	    }</p>
<p>    // Update is called once per frame<br>
	    IEnumerator DownloadAndCache()<br>
	    {<br>
	        // example URL of file on PC filesystem (Windows)<br>
	        // string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";<br>
	        // example URL of file on Android device SD-card<br>
	        metadata = CloudHandler.mTargetMetadata;</p>
<p>        string bundleURL = metadata;</p>
<p>        using (WWW www = new WWW(bundleURL))<br>
	        {<br>
	            yield return www;<br>
	            if (www.error != null)<br>
	                throw new UnityException("WWW download had an error:" + www.error);<br>
	            AssetBundle bundle = www.assetBundle;<br>
	            if (AssetName == "")<br>
	               mBundleInstance = Instantiate(bundle.mainAsset) as GameObject;<br>
	            else<br>
	                mBundleInstance = Instantiate(bundle.LoadAsset("arcloud")) as GameObject;<br>
	                mBundleInstance.transform.gameObject.SetActive(false);<br>
	                cloningan = Instantiate(mBundleInstance) as GameObject;<br>
	                cloningan.transform.parent = mTrackableBehaviour.transform;<br>
	                cloningan.transform.localScale = new Vector3(100f, 100f, 100f);<br>
	                cloningan.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);<br>
	                cloningan.transform.localRotation = Quaternion.identity;<br>
	                cloningan.transform.gameObject.SetActive(true);<br>
	            // Unload the AssetBundles compressed contents to conserve memory<br>
	            bundle.Unload(false);<br>
	        }<br>
	    }</p>
<p>    public void OnTrackableStateChanged(<br>
	    TrackableBehaviour.Status previousStatus,<br>
	    TrackableBehaviour.Status newStatus)<br>
	    {<br>
	        if (newStatus == TrackableBehaviour.Status.DETECTED ||<br>
	            newStatus == TrackableBehaviour.Status.TRACKED)<br>
	        {<br>
	                OnTrackingFound();        <br>
	        }<br>
	        else<br>
	        {<br>
	            OnTrackingLost();<br>
	        }<br>
	    }</p>
<p>    private void OnTrackingFound()<br>
	    {      <br>
	        StartCoroutine(DownloadAndCache());<br>
	    }</p>
<p><br>
	    private void OnTrackingLost()<br>
	    {<br>
	        Destroy(cloningan);<br>
	    }</p>
<p>    // Update is called once per frame<br>
	    void Update () {<br>
	 <br>
	}<br>
	}
AttachmentSize
Image icon unity.JPG46.68 KB
Log in or register to post comments