Exemple #1
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);

    }
    void Awake()
    {
        // Application.targetFrameRate = 60;

        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);
    }