Example #1
0
        private void SetBodyTextureSolidColor(Asset asset)
        {
            ColorAsset            colorAsset  = (ColorAsset)asset;
            SolidColorPixelSource pixelSource = new SolidColorPixelSource(1024, 1024, colorAsset.Color);

            mBodyTexture.SetLayerSource(asset.Type, pixelSource);
        }
        // Change a texture color
        private void SetFaceTextureAlpha(Asset asset)
        {
            ColorAsset colorAsset = asset as ColorAsset;

            if (colorAsset == null)
            {
                throw new ArgumentNullException("asset");
            }
            mFaceTexture.SetLayerFilterAlpha(asset.Type, colorAsset.Color);
        }
Example #3
0
        // Change a texture color
        private void SetBodyTextureFilterColor(Asset asset)
        {
            ColorAsset colorAsset = (ColorAsset)asset;

            mBodyTexture.SetLayerFilterColor(colorAsset.Type, colorAsset.Color);
        }
        //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);
                }
            }
        }