private void ApplyAssetToType(Asset asset)
 {
     if (this.Entity.UnityGameObject == null)
     {
         throw new System.Exception("Error: game object for green screen room entity is not set up.");
     }
     switch (asset.Type)
     {
     case AssetSubType.RoomBackgroundTexture:
         ImageAsset roomBackgroundTextureAsset = asset as ImageAsset;
         mImageBackground.renderer.material.mainTexture = roomBackgroundTextureAsset.Texture2D;
         break;
     }
 }
        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);
            }
        }
        //Interface which all classes will use to get an asset.
        //It first checks to see if the asset is in the dictionary.  If so it returns that asset.
        //Otherwise it attempts to load it from disk or download it from the web based on that path.
        private void GetAsset <T>
        (
            XmlNode assetData,
            System.Type assetType,
            AssetSubType assetSubType,
            string key,
            string path,
            Action <T> returnAssetCallback
        ) where T : Asset
        {
            //Check if the asset is in the dictionary already.
            Asset returnAsset;

            if (mAssetDictionary.TryGetValue(key, out returnAsset))
            {
                if (!(returnAsset.GetType() == assetType))
                {
                    throw new ArgumentException("The Asset with key: (" + key + ") expected type " + typeof(T).Name + " actually was " + returnAsset.GetType());
                }
                returnAssetCallback((T)returnAsset);
            }
            else
            {
                //Determine type of asset, build a delegate to handle creating the asset when it's downloaded
                //or instantiated, and return it using the callback supplied.
                if (assetType == typeof(TextureAsset))
                {
                    LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        Texture2D texture = (Texture2D)loadedObject;
                        TexturePixelSource texturePixelSource = new TexturePixelSource(texture);
                        TextureAsset textureAsset             = new TextureAsset(assetSubType, texturePixelSource, texture.name, path, key);
                        if (textureAsset == null)
                        {
                            throw new Exception("Couldn't create TexturePixelSource.");
                        }
                        returnAsset = textureAsset;
                        if (!mAssetDictionary.ContainsKey(key))
                        {
                            mAssetDictionary.Add(key, returnAsset);
                        }
                        returnAssetCallback((T)returnAsset);
                    });
                }
                else if (assetType == typeof(ImageAsset))
                {
                    LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        Texture2D texture     = (Texture2D)loadedObject;
                        ImageAsset imageAsset = new ImageAsset(assetSubType, texture, texture.name, path, key);
                        if (imageAsset == null)
                        {
                            throw new Exception("Couldn't create ImageAsset.");
                        }
                        returnAsset = imageAsset;
                        if (!mAssetDictionary.ContainsKey(key))
                        {
                            mAssetDictionary.Add(key, returnAsset);
                        }
                        returnAssetCallback((T)returnAsset);
                    });
                }
                else if (assetType == typeof(ColorAsset))
                {
                    //Pull out color values.
                    string hexColorValue = assetData.SelectSingleNode("AssetColor").InnerText;
                    string alphaValue    = assetData.SelectSingleNode("Alpha").InnerText;
                    //Set color values.
                    Color color = ColorUtility.HexToColor(hexColorValue);
                    color.a = float.Parse(alphaValue);

                    ColorAsset colorAsset = new ColorAsset(assetSubType, color, path, path, key);
                    if (colorAsset == null)
                    {
                        throw new Exception("Couldn't create color asset.");
                    }
                    returnAsset = colorAsset;
                    if (!mAssetDictionary.ContainsKey(key))
                    {
                        mAssetDictionary.Add(key, returnAsset);
                    }
                    returnAssetCallback((T)returnAsset);
                }
                else if (assetType == typeof(MeshAsset))
                {
                    LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        GameObject avatar = loadedObject as GameObject;
                        if (avatar == null)
                        {
                            throw new Exception("couldn't instantiate gameobject at path: " + path);
                        }

                        string meshFilterToFind = assetData.SelectSingleNode("MeshName").InnerText;
                        foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform)))
                        {
                            if (child.name == meshFilterToFind)
                            {
                                MeshFilter meshFilter = child.GetComponent(typeof(MeshFilter)) as MeshFilter;
                                Mesh mesh             = meshFilter.mesh;

                                MeshAsset meshAsset = new MeshAsset(assetSubType, mesh, path, path, key);

                                if (meshAsset == null)
                                {
                                    throw new Exception("Couldn't create MeshAsset.");
                                }
                                returnAsset = meshAsset;
                                if (!mAssetDictionary.ContainsKey(key))
                                {
                                    mAssetDictionary.Add(key, returnAsset);
                                }
                                returnAssetCallback((T)returnAsset);
                                break;
                            }
                        }
                    });
                }
                else if (assetType == typeof(SkinnedMeshAsset))
                {
                    LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        GameObject avatar = loadedObject as GameObject;
                        if (avatar == null)
                        {
                            throw new Exception("couldn't instantiate gameobject at path: " + path);
                        }
                        string skinnedMeshRendererToFind = assetData.SelectSingleNode("MeshName").InnerText;

                        foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform)))
                        {
                            if (child.name == skinnedMeshRendererToFind)
                            {
                                SkinnedMeshRenderer skinnedMeshRenderer = child.GetComponent(typeof(SkinnedMeshRenderer)) as SkinnedMeshRenderer;
                                SkinnedMeshAsset skinnedMeshAsset       = new SkinnedMeshAsset(assetSubType, skinnedMeshRenderer, "", path, key);

                                if (skinnedMeshRenderer == null)
                                {
                                    throw new Exception("Couldn't create MeshAsset.");
                                }
                                returnAsset = skinnedMeshAsset;
                                if (!mAssetDictionary.ContainsKey(key))
                                {
                                    mAssetDictionary.Add(key, returnAsset);
                                }
                                returnAssetCallback((T)returnAsset);
                                break;
                            }
                        }
                    });
                }
                else if (assetType == typeof(FaceMeshAsset))
                {
                    LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        GameObject avatar = loadedObject as GameObject;
                        if (avatar == null)
                        {
                            throw new Exception("couldn't instantiate gameobject at path: " + path);
                        }

                        string meshFilterToFind = assetData.SelectSingleNode("MeshName").InnerText;

                        foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform)))
                        {
                            if (child.name == meshFilterToFind)
                            {
                                XmlNode meshInfoNode = assetData.SelectSingleNode("MeshData/FaceMeshInfo");
                                if (meshInfoNode == null)
                                {
                                    throw new XmlException("Couldn't get face mesh info xml from node: " + assetData.OuterXml);
                                }
                                MeshFilter meshFilter        = child.GetComponent(typeof(MeshFilter)) as MeshFilter;
                                meshFilter.gameObject.active = true;
                                Mesh mesh = meshFilter.mesh;

                                FaceMeshAsset faceMeshAsset = new FaceMeshAsset(assetSubType, mesh, path, path, key, meshInfoNode);

                                if (faceMeshAsset == null)
                                {
                                    throw new Exception("Couldn't create MeshAsset.");
                                }
                                returnAsset = faceMeshAsset;
                                if (!mAssetDictionary.ContainsKey(key))
                                {
                                    mAssetDictionary.Add(key, returnAsset);
                                }
                                returnAssetCallback((T)returnAsset);
                                break;
                            }
                        }
                    });
                }
                else if (assetType == typeof(FaceAnimationAsset))
                {
                    string             moodName           = assetData.SelectSingleNode("Name").InnerText;
                    FaceAnimationName  faceAnimationName  = (FaceAnimationName)Enum.Parse(typeof(FaceAnimationName), moodName);
                    FaceAnimation      faceAnimation      = mFaceAnimationFactory.GetFaceAnimation(moodName);
                    FaceAnimationAsset faceAnimationAsset = new FaceAnimationAsset(assetSubType, faceAnimation, moodName, path, key, faceAnimationName);

                    GameFacade.Instance.RetrieveProxy <AnimationProxy>().AddFaceAnimation(faceAnimationAsset);

                    returnAsset = faceAnimationAsset;
                    if (!mAssetDictionary.ContainsKey(key))
                    {
                        mAssetDictionary.Add(key, returnAsset);
                    }
                    returnAssetCallback((T)returnAsset);
                }
                else if (assetType == typeof(RigAnimationAsset))
                {
                    LoadUnityObjectFromPath <UnityEngineAsset>(path, delegate(UnityEngine.Object loadedObject)
                    {
                        GameObject animationGameObject = GameObject.Instantiate(loadedObject as UnityEngine.Object) as GameObject;
                        if (animationGameObject == null)
                        {
                            Console.LogError("Could not cast loadedObject to GameObject from path: " + path);
                        }
                        Animation animation = animationGameObject.GetComponent(typeof(Animation)) as Animation;
                        if (animation == null)
                        {
                            Console.LogError("No animation component on loadedObject cast as a gameobject loaded from path: " + path);
                        }

                        string nameOfAnimation = assetData.SelectSingleNode("AnimationName").InnerText;
                        RigAnimationName animationName;
                        if (nameOfAnimation == null)
                        {
                            Console.LogError("AssetData on rig animation asset info does not contain an RigAnimationName node.");
                            animationName = RigAnimationName.None;
                        }
                        else
                        {
                            animationName = (RigAnimationName)Enum.Parse(typeof(RigAnimationName), nameOfAnimation);
                        }

                        RigAnimationAsset rigAnimationAsset = new RigAnimationAsset(assetSubType, animation.clip, animation.clip.name, path, key, animationName);
                        GameObject.Destroy(animationGameObject);

                        GameFacade.Instance.RetrieveProxy <AnimationProxy>().AddRigAnimation(rigAnimationAsset);

                        returnAsset = rigAnimationAsset;
                        if (!mAssetDictionary.ContainsKey(key))
                        {
                            mAssetDictionary.Add(key, returnAsset);
                        }
                        returnAssetCallback((T)returnAsset);
                    });
                }
                else
                {
                    throw new ArgumentException("The ClientAssetRepository does not know how to load an asset of type " + typeof(T).Name);
                }
            }
        }