Пример #1
0
        public Form1()
        {
            InitializeComponent();

            _polyline = new BezierPolyline(_DEFAULT_BEZIER_POLY_POINTS,
                                           canvas.Width, canvas.Height);
            _bezierPen = new Pen(Color.Black);

            _animatorCreator = new AnimatorCreator(_image, _polyline, canvas.Width, canvas.Height);

            _imageLoader = new ImageLoader(_IMAGE_WIDTH, _IMAGE_HEIGHT);
            _SetImage(_imageLoader.GetDefaultImage());
        }
Пример #2
0
        public override void OnInspectorGUI()
        {
            {
                skeletonDataAssetSaveId = AssetDatabase.GetAssetPath(target) + "_skeletonDataAssetId";
                string skeletonDataAssetPath = PlayerPrefs.GetString(skeletonDataAssetSaveId);

                skeletonDataAsset = AssetDatabase.LoadAssetAtPath <SkeletonDataAsset>(skeletonDataAssetPath);
                skeletonDataAsset = (SkeletonDataAsset)EditorGUILayout.ObjectField("SkeletonDataAsset", skeletonDataAsset, typeof(SkeletonDataAsset));

                PlayerPrefs.SetString(skeletonDataAssetSaveId, skeletonDataAsset ? AssetDatabase.GetAssetPath(skeletonDataAsset) : "");
            }

            {
                string excelPathKey = AssetDatabase.GetAssetPath(target) + "_ExcelPath";
                excelPath = PlayerPrefs.GetString(excelPathKey);
                PlayerPrefs.SetString(excelPathKey, EditorGUILayout.TextField("ExcelPath", excelPath));
            }

            if (MyGUI.Button("更新状态机"))
            {
                string path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(target));
                JsonPath = path + "/" + target.name + ".json";
                text     = AssetDatabase.LoadAssetAtPath <TextAsset>(JsonPath);

                if (text == null)
                {
                    if (MyGUI.Button("生成状态机模板"))
                    {
                        AssetDatabase.CopyAsset("Assets/Scripts/Tang/Edit/Editor/Animator/AnimatorControllerTemplate.json", JsonPath);
                    }
                }

                if (text != null)
                {
                    CreateAnimatorByJsonText(text.text);
                }
            }

            if (MyGUI.Button("添加帧事件支持"))
            {
                AnimatorController        animatorController       = target as AnimatorController;
                AnimatorControllerLayer[] animatorControllerLayers = animatorController.layers;
                AnimatorStateMachine      animatorStateMachine     = animatorControllerLayers[0].stateMachine;
                foreach (var state in animatorStateMachine.states)
                {
                    if (state.state != null)
                    {
                        if (state.state.motion != null)
                        {
                            if (state.state.motion is UnityEditor.Animations.BlendTree)
                            {
                                var blendTree = state.state.motion as UnityEditor.Animations.BlendTree;
                                if (blendTree.children.Length != 0)
                                {
                                    FrameEventBehaviour frameEventBehaviour = state.state.AddStateBehaviour <FrameEventBehaviour>();
                                    Motion motion = blendTree.children[0].motion;
                                    if (motion != null)
                                    {
                                        frameEventBehaviour.animName  = motion.name;
                                        frameEventBehaviour.Parameter = blendTree.blendParameter;
                                        frameEventBehaviour.namelist.Clear();
                                        for (int i = 0; i < blendTree.children.Length; i++)
                                        {
                                            UnityEditor.Animations.ChildMotion child = blendTree.children[i];
                                            Motion childmotion = blendTree.children[i].motion;
                                            if (childmotion != null && childmotion.name != null && childmotion.name != "")
                                            {
                                                if (frameEventBehaviour.namelist.Count - 1 < (int)child.threshold)
                                                {
                                                    int count = (int)child.threshold - (frameEventBehaviour.namelist.Count - 1);
                                                    for (int c = 0; c < count; c++)
                                                    {
                                                        frameEventBehaviour.namelist.Add("null");
                                                    }
                                                }
                                                frameEventBehaviour.namelist.Insert((int)child.threshold, childmotion.name);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Motion motion = state.state.motion;

                                FrameEventBehaviour frameEventBehaviour = state.state.AddStateBehaviour <FrameEventBehaviour>();
                                frameEventBehaviour.animName = motion.name;
                            }
                        }
                    }
                }

                EditorUtility.SetDirty(animatorController);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            if (MyGUI.Button("添加状态通知支持"))
            {
                SupportEventDispatch();
            }

            if (MyGUI.Button("copy"))
            {
                AnimatorController oldAnimatorController     = target as AnimatorController;
                string             oldAnimatorControllerPath = AssetDatabase.GetAssetPath(oldAnimatorController);
                string             newAnimatorControllerPath = oldAnimatorControllerPath.Substring(0, oldAnimatorControllerPath.IndexOf("."));
                string             newpath = newAnimatorControllerPath + "_new" + ".controller";

                AnimatorController NewAnimatorController = AnimatorController.CreateAnimatorControllerAtPath(newpath);

                var      unityAnimationClipTable = new Dictionary <string, Motion>();
                Object[] objects = AssetDatabase.LoadAllAssetRepresentationsAtPath(oldAnimatorControllerPath);
                foreach (var item in objects)
                {
                    //新建AnimationClip
                    if (item is AnimationClip)
                    {
                        var           clip    = item as AnimationClip;
                        AnimationClip newClip = new AnimationClip
                        {
                            name = clip.name
                        };
                        AssetDatabase.AddObjectToAsset(newClip, NewAnimatorController);
                        newClip.SetCurve("", typeof(GameObject), "dummy", AnimationCurve.Linear(0, 0, clip.length, 0));
                        var settings = AnimationUtility.GetAnimationClipSettings(newClip);
                        settings.stopTime = clip.length;
                        settings.loopTime = clip.isLooping;

                        AnimationUtility.SetAnimationClipSettings(newClip, settings);

                        AnimationUtility.SetAnimationEvents(newClip, clip.events);

                        EditorUtility.SetDirty(newClip);
                        unityAnimationClipTable.Add(clip.name, newClip);
                    }
                    else if (item is UnityEditor.Animations.BlendTree)
                    {
                        var    additem     = item as UnityEditor.Animations.BlendTree;
                        string additemjson = Tools.Obj2Json <UnityEditor.Animations.BlendTree>(additem);
                        UnityEditor.Animations.BlendTree blendTree = Tools.Json2Obj <UnityEditor.Animations.BlendTree>(additemjson);
                        AssetDatabase.AddObjectToAsset(blendTree, NewAnimatorController);
                        unityAnimationClipTable.Add(blendTree.name, blendTree);
                    }
                }

                //新建AnimationClip保存
                EditorUtility.SetDirty(NewAnimatorController);
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();

                //转移动画AnimationClip引用
                NewAnimatorController.parameters = oldAnimatorController.parameters;

                AnimatorControllerLayer[] oldAnimatorControllerLayers = oldAnimatorController.layers;
                AnimatorStateMachine      oldAnimatorStateMachine     = oldAnimatorControllerLayers[0].stateMachine;

                AnimatorControllerLayer[] NewAnimatorControllerLayers = NewAnimatorController.layers;
                AnimatorStateMachine      NewAnimatorStateMachine     = NewAnimatorControllerLayers[0].stateMachine;

                foreach (var oldChildenstate in oldAnimatorStateMachine.states)
                {
                    if (oldChildenstate.state != null)
                    {
                        var oldChildAnimatorState = oldChildenstate.state;
                        var oldStatemotion        = oldAnimatorController.GetStateEffectiveMotion(oldChildAnimatorState, 0);
                        UnityEditor.Animations.AnimatorState newState = NewAnimatorStateMachine.AddState(oldChildAnimatorState.name, oldChildenstate.position);
                        newState.name = oldChildAnimatorState.name;
                        newState.tag  = oldChildAnimatorState.tag;

                        if (oldStatemotion != null)
                        {
                            if (unityAnimationClipTable.ContainsKey(oldStatemotion.name))
                            {
                                var Motion = unityAnimationClipTable[oldStatemotion.name];
                                NewAnimatorController.SetStateEffectiveMotion(newState, Motion);
                                //NewAnimatorStateMachine.AddState(newState, oldChildenstate.position);
                            }
                        }
                    }
                }

                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();

                foreach (var oldChildenstate in oldAnimatorStateMachine.states)
                {
                    if (oldChildenstate.state != null)
                    {
                        var oldChildAnimatorState = oldChildenstate.state;
                        UnityEditor.Animations.AnimatorState newState = NewAnimatorStateMachine.GetState(oldChildAnimatorState.name);
                        if (newState != null)
                        {
                            foreach (var oldTransition in oldChildAnimatorState.transitions)
                            {
                                UnityEditor.Animations.AnimatorState tostate = NewAnimatorStateMachine.GetState(oldTransition.destinationState.name);
                                var transition = newState.AddTransition(tostate);
                                // transition.interruptionSource = UnityEditor.Animations.TransitionInterruptionSource.Destination;
                                transition.interruptionSource = oldTransition.interruptionSource;// UnityEditor.Animations.TransitionInterruptionSource.Source;
                                transition.hasExitTime        = oldTransition.hasExitTime;
                                transition.exitTime           = oldTransition.exitTime;
                                transition.duration           = oldTransition.duration;
                                transition.offset             = oldTransition.offset;

                                foreach (var conditionConfig in oldTransition.conditions)
                                {
                                    transition.AddCondition(conditionConfig.mode, conditionConfig.threshold, conditionConfig.parameter);
                                }
                            }

                            //newState.AddStateMachineBehaviour<RoleBaseStateMachineBehaviour>();

                            foreach (var item in oldChildAnimatorState.behaviours)
                            {
                                //string sd = item.GetType().FullName;
                                //Type sda = Type.GetType(item.GetType().FullName);
                                //if (sda == null)
                                //{
                                //    sda = Type.GetType(item.GetType().Namespace + "." + item.GetType().FullName);
                                //}

                                newState.AddStateMachineBehaviour(item.GetType());
                            }
                        }
                    }
                }
                //EditorUtility.SetDirty(NewAnimatorController);
                //AssetDatabase.SaveAssets();

                EditorUtility.SetDirty(NewAnimatorController);
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();


                System.IO.File.Delete(oldAnimatorControllerPath);
                System.IO.File.Move(newpath, oldAnimatorControllerPath);
            }

            if (MyGUI.Button("移除多余clip"))
            {
                AnimatorController        animatorController       = target as AnimatorController;
                AnimatorControllerLayer[] animatorControllerLayers = animatorController.layers;
                AnimatorStateMachine      animatorStateMachine     = animatorControllerLayers[0].stateMachine;

                var objList = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(animatorController));

                Dictionary <int, bool> tmp = new Dictionary <int, bool>();

                foreach (var item in objList)
                {
                    if (item != null)
                    {
                        if (tmp.ContainsKey(item.GetInstanceID()))
                        {
                            DestroyImmediate(item, true);
                        }
                        else
                        {
                            tmp.Add(item.GetInstanceID(), true);
                        }
                    }
                }

                foreach (var item in objList)
                {
                    if (item != null)
                    {
                        bool needRemove = true;

                        foreach (var state in animatorStateMachine.states)
                        {
                            if (state.state != null && state.state.motion != null && state.state.motion.Equals(item))
                            {
                                needRemove = false;
                            }
                        }

                        if (needRemove)
                        {
                            DestroyImmediate(item, true);
                        }
                    }
                }

                EditorUtility.SetDirty(animatorController);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            if (MyGUI.Button("新的状态机生成"))
            {
                AnimatorCreator animatorCreator = new AnimatorCreator(AnimatorLoadExcel.GetJObjectFromExcel(excelPath).ToObject <RoleAnimator>());

                try
                {
//                    skeletonDataAsset.RoleAnimator = animatorCreator.RoleAnimator;
//                    skeletonDataAsset.RoleAnimatorString = Tools.Obj2Json(animatorCreator.RoleAnimator);



                    AnimatorController animatorController = target as AnimatorController;


                    List <KeyValue <string, string> > RoleAnimatorStringList = null;

                    try
                    {
                        RoleAnimatorStringList = JObject.Parse(skeletonDataAsset.RoleAnimatorString)
                                                 .ToObject <List <KeyValue <string, string> > >();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    if (RoleAnimatorStringList == null)
                    {
                        RoleAnimatorStringList = new List <KeyValue <string, string> >();
                    }

                    RoleAnimatorStringList.RemoveAll(pair => pair.Key == animatorController.name);
                    RoleAnimatorStringList.Add(new KeyValue <string, string>(animatorController.name, Tools.Obj2Json(animatorCreator.RoleAnimator)));

                    skeletonDataAsset.RoleAnimatorString = Tools.Obj2Json(RoleAnimatorStringList);

                    animatorCreator.Create();
                    CreateAnimatorByConfig(animatorCreator.GetConfig());
                }
                catch (InvalidOperationException elements)
                {
                    Debug.Log(elements);
                }
            }

            DrawDefaultInspector();
        }