public override ITreeNode Clone(ITreeNode node) { EntityParamAction param = node as EntityParamAction; if (param == null) { param = new EntityParamAction(); } param.action = this.action; param.weight = this.weight; param.duration = this.duration; param.beforeAt = this.beforeAt; param.afterAt = this.afterAt; return(base.Clone(param)); }
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)); }
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); } }
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(); }