Esempio n. 1
0
    void OnPreprocessAnimation()
    {
        if (!assetPath.Contains("Assets/Model"))
        {
            return;
        }

        ModelImporter modelImporter = assetImporter as ModelImporter;

        if (modelImporter.clipAnimations.Length == 0)
        {
            modelImporter.clipAnimations = modelImporter.defaultClipAnimations;
        }

        ModelImporterClipAnimation[] clipAnimations = modelImporter.clipAnimations;
        for (int i = 0; i < clipAnimations.Length; i++)
        {
            ModelImporterClipAnimation clip = clipAnimations[i];
            string clipName = clip.name;
            int    indexOf  = clipName.IndexOf('@');
            if (indexOf > 0)
            {
                clipName = clipName.Substring(indexOf + 1, clipName.Length - indexOf - 1);
            }

            if (EntityParamModel.IsLoop(clipName))
            {
                clip.loopTime = true;
            }
        }
        modelImporter.clipAnimations = clipAnimations;
    }
Esempio n. 2
0
    public override void OnDraw(ref Rect r)
    {
        base.OnDraw(ref r);

        UnityEditor.EditorGUILayout.LabelField("AnimationClip", animationClip);
        r.height += 20;

        UnityEditor.EditorGUILayout.LabelField("Length", length.ToString());
        r.height += 20;

        mode      = (WrapMode)UnityEditor.EditorGUILayout.EnumPopup("Mode", mode);
        r.height += 20;

        if (mAnimationClip == null && string.IsNullOrEmpty(animationClip) == false && parent != null)
        {
            EntityParamModel model = parent as EntityParamModel;
            if (model != null)
            {
                mAnimationClip = model.GetAnimationClip(animationClip);
            }
        }


        AnimationClip clip = (AnimationClip)UnityEditor.EditorGUILayout.ObjectField(mAnimationClip, typeof(AnimationClip), false, new GUILayoutOption[0]);

        if (clip != null && mAnimationClip != clip)
        {
            mAnimationClip = clip;
            animationClip  = mAnimationClip.name;
            length         = mAnimationClip.length;
            mode           = mAnimationClip.wrapMode;
        }
        r.height += 30;
    }
Esempio n. 3
0
    public static void Save(EntityParamModel param, string path)
    {
        string xml = EntityParam.ToXml(param);

        if (!string.IsNullOrEmpty(path))
        {
            File.WriteAllText(path, xml, System.Text.Encoding.UTF8);
        }
    }
Esempio n. 4
0
    public void LoadModel()
    {
        if (gameObject != null)
        {
            return;
        }
        EntityParamModel param = agent.param;

        if (param != null)
        {
            LoadAsset(param.asset);
        }
    }
Esempio n. 5
0
    public override ITreeNode Clone(ITreeNode node)
    {
        EntityParamModel param = node as EntityParamModel;

        if (param == null)
        {
            param = new EntityParamModel();
        }
        param.asset = this.asset;

        param.scale = this.scale;
        return(base.Clone(param));
    }
Esempio n. 6
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));
    }
Esempio n. 7
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();
    }