Пример #1
0
 static void CreateInstanceIfNeeded()
 {
     if (instance == null)
     {
         instance = new EditorCoroutines();
         instance.Initialize();
     }
 }
Пример #2
0
        IEnumerator DoConvertAnimator(AnimatorData animData, string assetPath)
        {
            var go = animData.gameObject;

            Animate newAnim;

            newAnim = go.AddComponent <Animate>();

            var newAnimTarget = newAnim as ITarget;
            var oldAnimTarget = animData as AMITarget;

            //fill in common data
            newAnim.sequenceLoadAll       = animData.sequenceLoadAll;
            newAnim.sequenceKillWhenDone  = animData.sequenceKillWhenDone;
            newAnim.playOnEnable          = animData.playOnEnable;
            newAnim.isGlobal              = animData.isGlobal;
            newAnim.onDisableAction       = (Animate.DisableAction)((int)animData.onDisableAction);
            newAnim.updateType            = animData.updateType;
            newAnim.updateTimeIndependent = animData.updateTimeIndependent;

            if (oldAnimTarget.isMeta)
            {
                var oldMetaTarget = animData as AMIMeta;
                var oldMetaPath   = AssetDatabase.GetAssetPath(oldMetaTarget.meta);
                var oldMetaGUID   = AssetDatabase.AssetPathToGUID(oldMetaPath);

                //grab matching meta for new anim., no need to construct takes
                MetaInfo metaInfo;
                if (mGUIDMetaMatch.TryGetValue(oldMetaGUID, out metaInfo))
                {
                    newAnimTarget.meta = metaInfo.meta;
                }
                else
                {
                    AddMessage("Unable to find matching meta for: " + oldMetaPath, Color.yellow);
                }
            }
            else
            {
                //construct and convert takes
                foreach (var oldTake in oldAnimTarget.takes)
                {
                    var newTake = new Take();

                    AddMessage(" - convert take: " + oldTake.name);

                    yield return(EditorCoroutines.StartCoroutine(DoConvertTake(oldTake, newTake, false, assetPath), this));

                    newAnimTarget.takes.Add(newTake);
                }
            }

            newAnim.defaultTakeName = animData.defaultTakeName;
        }
Пример #3
0
        IEnumerator DoConvertAnimatorMeta(AnimatorMeta oldMeta, AnimateMeta newMeta, string assetPath)
        {
            foreach (var oldTake in oldMeta.takes)
            {
                var newTake = new Take();

                AddMessage(" - convert take: " + oldTake.name);

                yield return(EditorCoroutines.StartCoroutine(DoConvertTake(oldTake, newTake, true, assetPath), this));

                newMeta.takes.Add(newTake);
            }
        }
Пример #4
0
        IEnumerator DoConvertFromLoadedScenes(bool removeOldReference)
        {
            AddMessage("Grabbing Animators from loaded objects...");
            yield return(new WaitForFixedUpdate());

            var animDatas = Resources.FindObjectsOfTypeAll <AnimatorData>();

            UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();

            //go through and convert
            for (int i = 0; i < animDatas.Length; i++)
            {
                var go = animDatas[i].gameObject;

                if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
                {
                    continue;
                }

                //if(UnityEditor.EditorUtility.IsPersistent(go.transform.root.gameObject)) //exclude prefab instance
                //  continue;

                //exclude ones with Animate on it, assume it is already converted
                var animate = go.GetComponent <Animate>();
                if (animate)
                {
                    continue;
                }

                AddMessage("Converting Animator: " + animDatas[i].name);
                yield return(new WaitForFixedUpdate());

                yield return(EditorCoroutines.StartCoroutine(DoConvertAnimator(animDatas[i], animDatas[i].name), this));

                //delete old animator?
                if (removeOldReference)
                {
                    Object.DestroyImmediate(animDatas[i]);
                }
            }
        }
Пример #5
0
        void OnGUI()
        {
            var messageBkgrndColors = new Color[] { new Color(0.7f, 0.7f, 0.7f), new Color(0.5f, 0.5f, 0.5f) };

            var messageStyle = new GUIStyle(GUI.skin.label);

            messageStyle.normal.background = mTextureBlank;
            messageStyle.wordWrap          = true;

            bool defaultEnabled      = GUI.enabled;
            var  defaultColor        = GUI.color;
            var  defaultContentColor = GUI.contentColor;
            var  defaultBkgrndColor  = GUI.backgroundColor;

            GUILayout.Label("Messages");

            mScrollPos = GUILayout.BeginScrollView(mScrollPos, GUI.skin.box);

            for (int i = 0; i < mMessages.Count; i++)
            {
                GUI.contentColor    = mMessages[i].clr;
                GUI.backgroundColor = messageBkgrndColors[i % messageBkgrndColors.Length];

                GUILayout.Label(mMessages[i].text, messageStyle);
            }

            GUI.contentColor    = defaultContentColor;
            GUI.backgroundColor = defaultBkgrndColor;

            GUILayout.EndScrollView();

            GUILayout.Space(8f);

            GUI.enabled = !mIsConverting;

            bool         doConvert    = false;
            ConvertFlags convertFlags = ConvertFlags.None;

            bool doDeleteOldMetas = false;

            mIsRemoveOldAnimatorComp = GUILayout.Toggle(mIsRemoveOldAnimatorComp, new GUIContent("Remove Old Animator Component", "If toggled, will remove AnimatorData component, leave it in place otherwise."));

            if (GUILayout.Button("Convert From Assets"))
            {
                doConvert    = true;
                convertFlags = ConvertFlags.Assets;
            }

            if (GUILayout.Button("Convert From All Scenes"))
            {
                doConvert    = true;
                convertFlags = ConvertFlags.AllScenes;
            }

            if (GUILayout.Button("Convert From Loaded Objects"))
            {
                doConvert    = true;
                convertFlags = ConvertFlags.Loaded;
            }

            if (GUILayout.Button("Convert All"))
            {
                doConvert    = true;
                convertFlags = ConvertFlags.Assets | ConvertFlags.AllScenes;
            }

            GUILayout.Space(8f);

            if (GUILayout.Button(new GUIContent("Delete All AnimatorMeta")))
            {
                doDeleteOldMetas = true;
            }

            GUI.enabled = defaultEnabled;

            if (doConvert)
            {
                if (UnityEditor.EditorUtility.DisplayDialog("Convert", "This will go through and convert AnimatorData to Animate. Make sure to go through your scripts and update the references.", "Proceed"))
                {
                    EditorCoroutines.StartCoroutine(DoConvert(convertFlags, mIsRemoveOldAnimatorComp), this);
                }
            }
            else if (doDeleteOldMetas)
            {
                if (UnityEditor.EditorUtility.DisplayDialog("Convert", "This will delete all AnimatorMeta in Assets. Only do this AFTER you have converted everything, especially Animators that are hooked with metas.", "Proceed"))
                {
                    EditorCoroutines.StartCoroutine(DoDeleteMetas(), this);
                }
            }
        }
Пример #6
0
        IEnumerator DoConvert(ConvertFlags flags, bool removeOldReference)
        {
            mIsConverting = true;

            var prefabGUIDs = AssetDatabase.FindAssets("t:Prefab");

            //convert Meta Animators, record its GUID match for later
            for (int i = 0; i < prefabGUIDs.Length; i++)
            {
                var guid = prefabGUIDs[i];

                MetaInfo metaInfo;
                if (mGUIDMetaMatch.TryGetValue(guid, out metaInfo))  //already processed from before
                //make sure meta still exists
                {
                    if (metaInfo.meta)
                    {
                        continue;
                    }
                    else   //recreate
                    {
                        mGUIDMetaMatch.Remove(guid);
                    }
                }

                var path = AssetDatabase.GUIDToAssetPath(prefabGUIDs[i]);

                var animMeta = AssetDatabase.LoadAssetAtPath <AnimatorMeta>(path);
                if (animMeta)
                {
                    //check if it already has a converted asset
                    string metaConvertPath = GetNewMetaPath(path);
                    string metaConvertGUID = AssetDatabase.AssetPathToGUID(metaConvertPath);
                    if (!string.IsNullOrEmpty(metaConvertGUID))
                    {
                        //add to meta match
                        var meta = AssetDatabase.LoadAssetAtPath <AnimateMeta>(metaConvertPath);
                        if (meta)  //if null, need to recreate
                        {
                            mGUIDMetaMatch.Add(guid, new MetaInfo()
                            {
                                guid = metaConvertGUID, path = metaConvertPath, meta = meta
                            });
                            continue;
                        }
                    }

                    //convert
                    var newMeta = ScriptableObject.CreateInstance <AnimateMeta>();

                    AddMessage("Creating new AnimateMeta: " + metaConvertPath);
                    yield return(new WaitForFixedUpdate());

                    yield return(EditorCoroutines.StartCoroutine(DoConvertAnimatorMeta(animMeta, newMeta, metaConvertPath), this));

                    AssetDatabase.CreateAsset(newMeta, metaConvertPath);
                    AssetDatabase.SaveAssets();

                    mGUIDMetaMatch.Add(guid, new MetaInfo()
                    {
                        guid = AssetDatabase.AssetPathToGUID(metaConvertPath), path = metaConvertPath, meta = newMeta
                    });
                }
            }

            //convert from Asset
            if ((flags & ConvertFlags.Assets) != ConvertFlags.None)
            {
                AddMessage("Grabbing Animators from Assets...");
                yield return(new WaitForFixedUpdate());

                //Grab prefabs with AnimatorData
                var animDataList = new List <AnimatorAssetInfo>();

                for (int i = 0; i < prefabGUIDs.Length; i++)
                {
                    var path     = AssetDatabase.GUIDToAssetPath(prefabGUIDs[i]);
                    var animData = AssetDatabase.LoadAssetAtPath <AnimatorData>(path);
                    if (animData)
                    {
                        //AddMessage(path);

                        //exclude ones with Animate on it, assume it is already converted
                        var animate = animData.GetComponent <Animate>();
                        if (animate)
                        {
                            continue;
                        }

                        var rootGO = animData.transform.root.gameObject;

                        UnityEditor.EditorUtility.SetDirty(rootGO);

                        animDataList.Add(new AnimatorAssetInfo()
                        {
                            guid = prefabGUIDs[i], path = path, animData = animData, rootGO = rootGO
                        });
                    }
                }

                //Go through and convert animators
                for (int i = 0; i < animDataList.Count; i++)
                {
                    AddMessage("Converting Animator: " + animDataList[i].path);
                    yield return(new WaitForFixedUpdate());

                    yield return(EditorCoroutines.StartCoroutine(DoConvertAnimator(animDataList[i].animData, animDataList[i].path), this));

                    //delete old animator?
                    if (removeOldReference)
                    {
                        Object.DestroyImmediate(animDataList[i].animData, true);
                    }
                }
            }

            if ((flags & ConvertFlags.AllScenes) != ConvertFlags.None)  //convert from all scenes
            //UnityEngine.SceneManagement.SceneManager.GetAllScenes();

            {
                var sceneGUIDs = AssetDatabase.FindAssets("t:Scene");
                for (int i = 0; i < sceneGUIDs.Length; i++)
                {
                    var scenePath = AssetDatabase.GUIDToAssetPath(sceneGUIDs[i]);

                    AddMessage("Loading scene: " + scenePath);

                    var scene = UnityEditor.SceneManagement.EditorSceneManager.OpenScene(scenePath, UnityEditor.SceneManagement.OpenSceneMode.Single);

                    yield return(new WaitForSeconds(0.3f));

                    yield return(EditorCoroutines.StartCoroutine(DoConvertFromLoadedScenes(removeOldReference), this));

                    UnityEditor.SceneManagement.EditorSceneManager.SaveScene(scene);

                    yield return(new WaitForSeconds(0.3f));
                }
            }
            else if ((flags & ConvertFlags.Loaded) != ConvertFlags.None)  //convert from current scene
            {
                yield return(EditorCoroutines.StartCoroutine(DoConvertFromLoadedScenes(removeOldReference), this));
            }

            mIsConverting = false;
            Repaint();
        }