Example #1
0
    public override ITreeNode Clone(ITreeNode node)
    {
        EntityParamAnimation param = node as EntityParamAnimation;

        if (param == null)
        {
            param = new EntityParamAnimation();
        }
        param.animationClip = this.animationClip;
        param.length        = this.length;
        param.mode          = this.mode;

        return(base.Clone(param));
    }
Example #2
0
    private static TreeNodeGraph CreateTemplate()
    {
        var modelParam = new EntityParamModel();

        modelParam.rect = new Rect(20, 20, TreeNode.WIDTH, TreeNode.HEIGHT);

        var actionParam = new EntityParamAction();

        actionParam.rect = new Rect(300, 20, TreeNode.WIDTH, TreeNode.HEIGHT);
        modelParam.AddChild(actionParam);

        var animationParam = new EntityParamAnimation();

        animationParam.rect = new Rect(600, 20, TreeNode.WIDTH, TreeNode.HEIGHT);
        modelParam.AddChild(animationParam);

        return(TreeNodeGraph.CreateGraph(modelParam));
    }
Example #3
0
    private void CreateAnimationNode(UnityEngine.Object go)
    {
        if (go == null)
        {
            return;
        }

        string path = UnityEditor.AssetDatabase.GetAssetPath(go);

        string dirName = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/') + 1) + path.Substring(0, path.LastIndexOf('/') + 1);

        System.IO.DirectoryInfo dir   = new System.IO.DirectoryInfo(dirName);
        System.IO.FileInfo[]    files = dir.GetFiles("*.fbx");

        int WIDTH  = 240;
        int HEIGHT = 20;

        for (int i = 0; i < files.Length; ++i)
        {
            if (files[i].Name.Contains("@") == false)     // 跳过不是动作的文件
            {
                //Debug.Log("跳过无效文件:" + fileInfos[i].Name + "   "  + obj.name + "@");
                continue;
            }

            string animName = System.IO.Path.GetFileNameWithoutExtension(files[i].Name).Split('@')[1];

            string animationClip = files[i].FullName.Replace("\\", "/");
            animationClip = animationClip.Substring(animationClip.LastIndexOf("Assets/"));
            var clip = UnityEditor.AssetDatabase.LoadAssetAtPath <AnimationClip>(animationClip);

            ActionType        type        = GetActionType(animName);
            EntityParamAction actionParam = null;
            for (int j = 0; j < children.Count; ++j)
            {
                var child = children[j] as EntityParamAction;
                if (child != null && type == child.action)
                {
                    actionParam = child; break;
                }
            }
            if (actionParam == null)
            {
                actionParam      = new EntityParamAction();
                actionParam.rect = new Rect(300, 20, WIDTH, HEIGHT);
                AddChild(actionParam);
            }
            actionParam.action = type;
            actionParam.weight = EntityParamAction.ActionWeights[type];

            EntityParamAnimation animationParam = null;
            for (int j = 0; j < actionParam.children.Count; ++j)
            {
                var child = actionParam.children[j] as EntityParamAnimation;
                if (child != null && child.animationClip == animationClip)
                {
                    animationParam = child; break;
                }
            }
            if (animationParam == null)
            {
                animationParam      = new EntityParamAnimation();
                animationParam.rect = new Rect(600, 20, WIDTH, HEIGHT);
                AddChild(animationParam);
            }
            animationParam.animationClip = animName;
            animationParam.length        = clip.length;
            animationParam.mode          = GetWrapMode(animName);
        }
    }
Example #4
0
    static void CreateAnimationPrefab(GameObject obj)
    {
        if (obj == null)
        {
            return;
        }

        string path = AssetDatabase.GetAssetPath(Selection.activeGameObject);

        string dirName = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/') + 1) + path.Substring(0, path.LastIndexOf('/') + 1);

        DirectoryInfo dir = new DirectoryInfo(dirName);

        FileInfo[] files = dir.GetFiles("*.fbx");

        EntityParamModel modelParam     = null;
        string           configFullPath = string.Format("{0}/Resources/r/config/{1}.txt", Application.dataPath, obj.name.ToLower());

        Debug.Log(configFullPath);
        if (File.Exists(configFullPath) == false)
        {
            modelParam      = new EntityParamModel();
            modelParam.rect = new Rect(20, 20, TreeNode.WIDTH, TreeNode.HEIGHT);
        }
        else
        {
            modelParam = EntityParam.Create(File.ReadAllText(configFullPath)) as EntityParamModel;
        }

        modelParam.asset = obj.name.ToLower() + ".prefab";

        AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(path.ToLower().Replace(".fbx", ".controller"));

        AnimatorStateMachine sm = animatorController.layers[0].stateMachine;

        int leftIndex  = 0;
        int rightIndex = 0;

        for (int i = 0; i < files.Length; ++i)
        {
            if (files[i].Name.Contains("@") == false)     // 跳过不是动作的文件
            {
                //Debug.Log("跳过无效文件:" + fileInfos[i].Name + "   "  + obj.name + "@");
                continue;
            }

            string animName = Path.GetFileNameWithoutExtension(files[i].Name).Split('@')[1];

            AnimatorState state = null;

            if (leftIndex < 5)
            {
                state = sm.AddState(animName, new Vector3(-140, 25 + 50 * leftIndex++, 0));
            }
            else
            {
                state = sm.AddState(animName, new Vector3(180, 25 + 50 * rightIndex++, 0));
            }
            string animationClip = files[i].FullName.Replace("\\", "/");
            animationClip = animationClip.Substring(animationClip.LastIndexOf("Assets/"));
            var clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(animationClip);
            state.motion = clip;
            if (animName.Equals("idle"))
            {
                sm.defaultState = state;
            }

            if (modelParam != null)
            {
                ActionType        type        = EntityParamModel.GetActionType(animName);
                EntityParamAction actionParam = null;
                for (int j = 0; j < modelParam.children.Count; ++j)
                {
                    var child = modelParam.children[j] as EntityParamAction;
                    if (child != null && type == child.action)
                    {
                        actionParam = child; break;
                    }
                }
                if (actionParam == null)
                {
                    actionParam      = new EntityParamAction();
                    actionParam.rect = new Rect(300, 20, TreeNode.WIDTH, TreeNode.HEIGHT);
                    modelParam.AddChild(actionParam);
                }
                actionParam.action = type;
                actionParam.weight = EntityParamAction.ActionWeights[type];

                EntityParamAnimation animationParam = null;
                for (int j = 0; j < actionParam.children.Count; ++j)
                {
                    var child = actionParam.children[j] as EntityParamAnimation;
                    if (child != null && child.animationClip == animationClip)
                    {
                        animationParam = child; break;
                    }
                }
                if (animationParam == null)
                {
                    animationParam      = new EntityParamAnimation();
                    animationParam.rect = new Rect(600, 20, TreeNode.WIDTH, TreeNode.HEIGHT);
                    modelParam.AddChild(animationParam);
                }
                animationParam.animationClip = animName;
                animationParam.length        = clip.length;
                animationParam.mode          = EntityParamModel.GetWrapMode(animName);
            }
        }
        var empty = sm.AddState("empty", new Vector3(180, 25 + 50 * rightIndex, 0));

        empty.speed = 0;


        if (File.Exists(configFullPath))
        {
            File.Delete(configFullPath);
        }
        EntityParamWindow.Save(modelParam, configFullPath);

        // 如果目录下预制体不存在,则创建预制体
        string prefabPath = string.Format("Assets/Resources/r/model/{0}.prefab", obj.name.ToLower());

        string prefabFullPath = string.Format("{0}/Resources/r/model/{1}.prefab", Application.dataPath, obj.name.ToLower());

        if (File.Exists(prefabFullPath))
        {
            bool delete = EditorUtility.DisplayDialog("提示", string.Format("预设{0}.prefab已存在,是否替换?", obj.name), "确认", "取消");
            if (delete)
            {
                File.Delete(prefabFullPath);
            }
            else
            {
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                return;
            }
        }

        obj.GetComponent <Animator>().runtimeAnimatorController = animatorController;
        GameObject go = PrefabUtility.SaveAsPrefabAsset(obj, prefabPath);

        go.transform.position   = Vector3.zero;
        go.transform.rotation   = Quaternion.identity;
        go.transform.localScale = Vector3.one;


        int layer = LayerMask.NameToLayer("Default");

        Transform[] transforms = go.GetComponentsInChildren <Transform>();
        for (int i = 0; i < transforms.Length; ++i)
        {
            transforms[i].gameObject.layer = layer;

            SetBonePoint(transforms[i]);
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
Example #5
0
    public override void OnDraw(ref Rect r)
    {
        base.OnDraw(ref r);

        int size = Mathf.Clamp(UnityEditor.EditorGUILayout.IntField("Size", animations.Count), 0, 10);

        r.height += 20;

        if (size > animations.Count)
        {
            for (int i = animations.Count; i < size; i++)
            {
                animations.Add(new EntityParamPluginAnimationClip());
            }
        }
        else if (size < animations.Count)
        {
            for (int i = animations.Count - 1; i >= size; i--)
            {
                animations.RemoveAt(i);
            }
        }

        if (root != null)
        {
            var action = parent as EntityParamAction;

            var   anims  = root.GetParams <EntityParamAnimation>();
            var   names  = anims.Select(a => { return(a.animationClip); }).ToList();
            float length = 0;
            for (int i = 0; i < animations.Count; i++)
            {
                UnityEditor.EditorGUILayout.LabelField("  Element " + i);
                r.height += 20;

                animations[i].beginAt = UnityEditor.EditorGUILayout.FloatField("     Begin At", animations[i].beginAt);
                r.height += 20;

                if (string.IsNullOrEmpty(animations[i].animationClip))
                {
                    if (action != null)
                    {
                        animations[i].animationClip = action.action.ToString();
                    }
                }

                int index = names.IndexOf(animations[i].animationClip);

                int j = UnityEditor.EditorGUILayout.Popup("     AnimationClip", index, names.ToArray());
                r.height += 20;

                EntityParamAnimation anim = null;
                if (j >= 0 && j < anims.Count)
                {
                    anim = anims[j];
                }

                if (anim != null && j != index)
                {
                    animations[i].animationClip = anim.animationClip;
                    animations[i].length        = anim.length;
                }
                animations[i].speed = Mathf.Clamp(UnityEditor.EditorGUILayout.FloatField("Speed", animations[i].speed), 0.01f, 10);
                r.height           += 20;

                UnityEditor.EditorGUILayout.BeginHorizontal();
                animations[i].length = UnityEditor.EditorGUILayout.FloatField("     Length", animations[i].length);
                if (anim != null)
                {
                    animations[i].beginAt = Mathf.Clamp(animations[i].beginAt, 0, anim.length);

                    bool fix = UnityEditor.EditorGUILayout.Toggle(false);
                    if (fix)
                    {
                        animations[i].length = anim.length - animations[i].beginAt;
                    }
                }

                UnityEditor.EditorGUILayout.EndHorizontal();

                r.height += 20;

                length += animations[i].length / animations[i].speed;
            }
            if (action != null &&
                action.duration != DEFAULT_DURATION
                )
            {
                action.duration = length;
            }
        }
    }