Пример #1
0
 /// <summary>
 /// Load an AssetBundle from the dedicated AssetServer and serve it to the
 /// specified Callback-Method.
 /// Callback will get a null-argument if an error occured while fetchting the Asset.
 /// </summary>
 /// <param name="assetURI">AssetBundle to fetch from server (preserves folder hirarchy)</param>
 /// <param name="callBack">Method to call once the request is finished</param>
 /// <notes>
 /// The Asset - or rather GameObject - which gets created and served to the callback is a clone
 /// of the asset. You may do whatever you want with it 
 /// </notes>
 public void RequestAsset(string assetURI, AssetLoadedFunc callBack)
 {
     // Start the download process
     Debug.Log(string.Format("Asset Manager: Requesting asset from server: {0}...", assetURI));
     UnityWebRequest assetRequest = UnityWebRequest.GetAssetBundle((string.Format("{0}/Bundle/{1}", this.ServerAddress, assetURI)));
     StartCoroutine(LoadAsset(assetRequest, callBack, assetURI));
 }
Пример #2
0
    IEnumerator LoadAsset(UnityWebRequest assetRequest, AssetLoadedFunc callBack, string assetURI)
    {
        GameObject gameObject = null;

        // Check if asset is already loaded, if it is, just call the callback immediately
        if (_gameObjects.ContainsKey(assetURI))
        {
            gameObject = _gameObjects[assetURI];
        }
        else
        {
            // Ensure server download is finished, before continuing
            yield return assetRequest.SendWebRequest();

            if (assetRequest.isHttpError || assetRequest.isNetworkError)
            {
                _lastError = assetRequest.error;

                if (callBack != null)
                    callBack(null);

                yield break;
            }
        }

        if (gameObject == null)
        {
            Debug.Log(string.Format("Asset Manager: Asset download completed."));

            AssetBundle bundle = ((DownloadHandlerAssetBundle)assetRequest.downloadHandler).assetBundle;

            // Build game object from request bundle
            // Per definition (or more likely due to technical limitations at this time) 
            // it has to be the first asset in the bundle
            gameObject = bundle.LoadAsset(bundle.GetAllAssetNames()[0]) as GameObject;

            // Per definition, every Asset can be saved.
            SceneTransfer.SetSavableStatus(gameObject, true).AssetBundleName = bundle.name;

            // Add gameobject to caching map
            _gameObjects.Add(assetURI, gameObject);
        }

        gameObject = GameObject.Instantiate(gameObject);

        // Dispatch event
        if (callBack != null)
            callBack(gameObject);

        if (OnAssetLoaded != null)
            OnAssetLoaded(gameObject);

    }