Пример #1
0
        private void LoadUnityObjectFromPath <T>(string path, Action <UnityEngine.Object> loadedAssetCallback) where T : Asset
        {
            try
            {
                //This hack is here because we don't have an empty texture on the web yet.  We need to figure out how to handle that.
                if (path == "")
                {
                    UnityEngine.Object unityEngineObject = Resources.Load("EmptyTexture");
                    if (unityEngineObject == null)
                    {
                        throw new Exception("Could not instantiate Empty Texture.");
                    }

                    loadedAssetCallback(unityEngineObject);
                    return;
                }

                Pair <string> protocolAndPath   = ProtocolUtility.SplitAndResolve(path);
                string        loadAssetProtocol = protocolAndPath.First;
                string        resourcePath      = protocolAndPath.Second;
                if (loadAssetProtocol == "http" || loadAssetProtocol == "file" || loadAssetProtocol == "assets")
                {
                    //If the repo is already downloading from the path, the callback
                    //is added to this dictionary which gets called when the object at
                    //said path is finished downloading.
                    if (mCallbacksForFinishedDownloadAtPath.ContainsKey(resourcePath))
                    {
                        //If we've downloaded the path before we'll have the key but nothing in
                        //the list so we have to download it again.
                        mCallbacksForFinishedDownloadAtPath[resourcePath].Add(loadedAssetCallback);
                    }
                    else
                    {
                        List <Action <UnityEngine.Object> > downloadCompleteCallbacks = new List <Action <UnityEngine.Object> >();
                        downloadCompleteCallbacks.Add(loadedAssetCallback);
                        mCallbacksForFinishedDownloadAtPath.Add(resourcePath, downloadCompleteCallbacks);

                        if (typeof(T) == typeof(TextureAsset) || typeof(T) == typeof(ImageAsset) || typeof(T) == typeof(Asset))
                        {
                            LoadTextureAssetFromWeb(resourcePath, loadedAssetCallback);
                        }
                        else if (typeof(T) == typeof(SoundAsset))
                        {
                            LoadSoundAssetFromWeb(resourcePath, loadedAssetCallback);
                        }
                        else if (typeof(T) == typeof(UnityEngineAsset) || typeof(T) == typeof(RigAnimationAsset))
                        {
                            LoadLateBoundUnityObject(resourcePath, loadedAssetCallback);
                        }
                        else
                        {
                            throw new Exception("ClientAssetRepository doesn't know how to handle type: " + typeof(T).Name);
                        }
                    }
                }
                else if (loadAssetProtocol == "resources")
                {
                    if (resourcePath == "Avatar/Gabriella Templates")
                    {
                        loadedAssetCallback(mAvatarTemplate);
                        return;
                    }
                    UnityEngine.Object unityEngineObject = Resources.Load(resourcePath);
                    if (unityEngineObject == null)
                    {
                        throw new Exception("Could not instantiate object from path " + resourcePath + ".");
                    }

                    loadedAssetCallback(unityEngineObject);
                    return;
                }
                else
                {
                    throw new Exception("ClientAssetRepo does not know how to load an object with protocol: " + loadAssetProtocol);
                }
            }
            catch (System.Exception ex)
            {
                Console.LogError("Exception in LoadUnityObjectFromPath: " + path + " " + ex.ToString());
            }
        }
Пример #2
0
        public void LoadAssetFromPath <T>(string path, Action <T> loadedAssetCallback) where T : Asset
        {
            string uniqueKey = path;

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            Asset loadedAsset;

            if (mAssetDictionary.TryGetValue(uniqueKey, out loadedAsset))
            {
                if (!(loadedAsset is T))
                {
                    throw new Exception("Asset loaded from " + path + " expected to be type: " + typeof(T).Name + ", actually was: " + loadedAsset.GetType());
                }
                loadedAssetCallback((T)loadedAsset);
            }
            // This might not ever be called.
            else if (typeof(T) == typeof(RigAnimationAsset))
            {
                LoadUnityObjectFromPath <RigAnimationAsset>(path, delegate(UnityEngine.Object loadedUnityObject)
                {
                    GameObject gameObject = (GameObject)GameObject.Instantiate(loadedUnityObject);
                    try
                    {
                        Animation animation = gameObject.GetComponent <Animation>();

                        Asset asset = new RigAnimationAsset(AssetSubType.NotSet, animation.clip, path, path, path, RigAnimationName.None);

                        if (!mAssetDictionary.ContainsKey(uniqueKey))
                        {
                            mAssetDictionary.Add(uniqueKey, asset);
                        }

                        loadedAssetCallback((T)asset);
                    }
                    finally
                    {
                        GameObject.Destroy(gameObject);
                    }
                });
            }
            else if (typeof(T) == typeof(UnityEngineAsset))
            {
                LoadUnityObjectFromPath <UnityEngineAsset>(path, delegate(UnityEngine.Object loadedUnityObject)
                {
                    Asset asset = new UnityEngineAsset(loadedUnityObject, path);
                    if (!mAssetDictionary.ContainsKey(uniqueKey))
                    {
                        mAssetDictionary.Add(uniqueKey, asset);
                    }

                    loadedAssetCallback((T)asset);
                });
            }
            else if (typeof(T) == typeof(SoundAsset))
            {
                LoadUnityObjectFromPath <SoundAsset>(path, delegate(UnityEngine.Object loadedUnityObject)
                {
                    AudioClip audioClip = loadedUnityObject as AudioClip;
                    if (audioClip == null)
                    {
                        throw new Exception("audioClip could not be cast from UnityEngine.Object");
                    }
                    Asset asset = new SoundAsset(audioClip, path);
                    if (!mAssetDictionary.ContainsKey(uniqueKey))
                    {
                        mAssetDictionary.Add(uniqueKey, asset);
                    }
                    loadedAssetCallback((T)asset);
                });
            }
            else if (typeof(T) == typeof(ImageAsset))
            {
                LoadUnityObjectFromPath <ImageAsset>(path, delegate(UnityEngine.Object loadedUnityObject)
                {
                    Texture2D texture = loadedUnityObject as Texture2D;
                    if (texture == null)
                    {
                        throw new Exception("texture could not be cast from UnityEngine.Object");
                    }
                    Asset asset = new ImageAsset(texture, path);
                    if (!mAssetDictionary.ContainsKey(uniqueKey))
                    {
                        mAssetDictionary.Add(uniqueKey, asset);
                    }
                    loadedAssetCallback((T)asset);
                });
            }
            else if (typeof(T) == typeof(XmlAsset))
            {
                string resolvedPath = ProtocolUtility.SplitAndResolve(path).Second;
                Console.WriteLine("resolved path for XmlAsset: " + resolvedPath + ", path = " + path);
                GameFacade.Instance.RetrieveMediator <SchedulerMediator>().Scheduler.StartCoroutine(DownloadText(resolvedPath, delegate(string wwwData)
                {
                    Asset result = new XmlAsset(wwwData, uniqueKey);
                    loadedAssetCallback((T)result);
                }));
            }
            else
            {
                throw new Exception("LoadAssetFromPath doesn't support " + typeof(T).Name);
            }
        }