|
| 1 | +using System.IO; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEditor; |
| 4 | +using UnityEditor.Animations; |
| 5 | + |
| 6 | +// ---------------------------【构建Animation Controller 和 保存Prefabs】--------------------------- |
| 7 | +public class BuildAnimationAndPrefab : EditorWindow |
| 8 | +{ |
| 9 | + // 当前选择路径 |
| 10 | + public string path = "Assets"; |
| 11 | + public Rect pathRect; |
| 12 | + public GUILayoutOption test; |
| 13 | + // 打包后的文件 |
| 14 | + public string buildPath = "Assets/Build"; |
| 15 | + public Rect buildPathRect; |
| 16 | + |
| 17 | + |
| 18 | + [MenuItem("Tools/构建Animation")] |
| 19 | + public static void showWindow() |
| 20 | + { |
| 21 | + // 能悬浮、能拖拽、能嵌入 |
| 22 | + EditorWindow.CreateInstance<BuildAnimationAndPrefab>().Show(); |
| 23 | + // var window = EditorWindow.GetWindow<TestWindow>(false, "FolderWindow"); |
| 24 | + // window.minSize = new Vector2(400, 400); |
| 25 | + // window.Show(); |
| 26 | + } |
| 27 | + |
| 28 | + public void OnGUI() |
| 29 | + { |
| 30 | + // 鼠标当前选中路径 |
| 31 | + // string[] strs = Selection.assetGUIDs; |
| 32 | + // if (strs.Length != 0) |
| 33 | + // this.path = AssetDatabase.GUIDToAssetPath(strs[0]); |
| 34 | + |
| 35 | + // 需要构建的路径 |
| 36 | + EditorGUILayout.Space(); |
| 37 | + EditorGUILayout.LabelField("需要打包的路径 (鼠标拖拽文件夹到这里)"); |
| 38 | + EditorGUILayout.Space(); |
| 39 | + // this.path = EditorGUILayout.TextField(this.path); |
| 40 | + GUI.SetNextControlName("input1");//设置下一个控件的名字 |
| 41 | + pathRect = EditorGUILayout.GetControlRect(GUILayout.Width(400)); |
| 42 | + EditorGUI.TextField(pathRect, path); |
| 43 | + EditorGUILayout.Space(); |
| 44 | + |
| 45 | + // 构建后的路径 |
| 46 | + EditorGUILayout.LabelField("打包后的路径 (鼠标拖拽文件夹到这里)"); |
| 47 | + EditorGUILayout.Space(); |
| 48 | + // this.buildPath = EditorGUILayout.TextField(this.buildPath); |
| 49 | + GUI.SetNextControlName("input2");//设置下一个控件的名字 |
| 50 | + buildPathRect = EditorGUILayout.GetControlRect(GUILayout.Width(400)); |
| 51 | + EditorGUI.TextField(buildPathRect, buildPath); |
| 52 | + EditorGUILayout.Space(); |
| 53 | + |
| 54 | + // 文件拖拽 |
| 55 | + DragFolder(); |
| 56 | + |
| 57 | + // 创建资源按钮 |
| 58 | + if (GUILayout.Button("Create")) |
| 59 | + { |
| 60 | + this.DoCreateAnimationAssets(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // 鼠标拖拽文件 |
| 65 | + void DragFolder() |
| 66 | + { |
| 67 | + //鼠标位于当前窗口 |
| 68 | + if (mouseOverWindow == this) |
| 69 | + { |
| 70 | + //拖入窗口未松开鼠标 |
| 71 | + if (Event.current.type == EventType.DragUpdated) |
| 72 | + { |
| 73 | + DragAndDrop.visualMode = DragAndDropVisualMode.Generic;//改变鼠标外观 |
| 74 | + // 判断区域 |
| 75 | + if (pathRect.Contains(Event.current.mousePosition)) |
| 76 | + GUI.FocusControl("input1"); |
| 77 | + else if (buildPathRect.Contains(Event.current.mousePosition)) |
| 78 | + GUI.FocusControl("input2"); |
| 79 | + } |
| 80 | + //拖入窗口并松开鼠标 |
| 81 | + else if (Event.current.type == EventType.DragExited) |
| 82 | + { |
| 83 | + //获取焦点, |
| 84 | + string dragPath = string.Join("", DragAndDrop.paths); |
| 85 | + // 判断区域 |
| 86 | + if (pathRect.Contains(Event.current.mousePosition)) |
| 87 | + this.path = dragPath; |
| 88 | + else if (buildPathRect.Contains(Event.current.mousePosition)) |
| 89 | + this.buildPath = dragPath; |
| 90 | + // Debug.Log("path:" + path); |
| 91 | + // Debug.Log("buildPath:" + buildPath); |
| 92 | + |
| 93 | + // 取消焦点(不然GUI不会刷新) |
| 94 | + GUI.FocusControl(null); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // 创建Animation |
| 100 | + void DoCreateAnimationAssets() |
| 101 | + { |
| 102 | + string[] allPath = AssetDatabase.FindAssets("t:GameObject", new string[] { this.path }); |
| 103 | + if (allPath.Length == 0) |
| 104 | + { |
| 105 | + CreateAnimation(this.path); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + for (int i = 0, len = allPath.Length; i < len; i++) |
| 110 | + { |
| 111 | + string childpath = AssetDatabase.GUIDToAssetPath(allPath[i]); |
| 112 | + CreateAnimation(childpath); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // 创建Animation |
| 118 | + void CreateAnimation(string path) |
| 119 | + { |
| 120 | + if (System.IO.Path.GetExtension(path) == ".fbx") |
| 121 | + { |
| 122 | + //创建Controller |
| 123 | + AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(path.Replace(".fbx", ".controller")); |
| 124 | + //得到它的Layer |
| 125 | + AnimatorControllerLayer layer = animatorController.layers[0]; |
| 126 | + AddStateTransition(path, layer); |
| 127 | + // 设置模型设置 |
| 128 | + setModelSetup(path); |
| 129 | + |
| 130 | + Material mat = CreateMaterial(path); |
| 131 | + // 创建预制体 |
| 132 | + CreatePrefab(path, animatorController, mat); |
| 133 | + Debug.Log("-- 创建成功: " + path); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // 修改模型设置 |
| 138 | + void setModelSetup(string path) |
| 139 | + { |
| 140 | + ModelImporter importer = ModelImporter.GetAtPath(path) as ModelImporter; |
| 141 | + if (importer) |
| 142 | + { |
| 143 | + importer.animationType = ModelImporterAnimationType.Generic; |
| 144 | + importer.avatarSetup = ModelImporterAvatarSetup.CreateFromThisModel; |
| 145 | + importer.globalScale = 100; |
| 146 | + importer.materialImportMode = ModelImporterMaterialImportMode.None; |
| 147 | + importer.SaveAndReimport(); |
| 148 | + // Debug.Log("-- 修改模型设置: " + importer.name); |
| 149 | + } |
| 150 | + } |
| 151 | + // 创建预制体 |
| 152 | + void CreatePrefab(string path, AnimatorController animatorCtorl, Material mat) |
| 153 | + { |
| 154 | + // 创建预制体 |
| 155 | + GameObject gameObj = AssetDatabase.LoadAssetAtPath<GameObject>(path); |
| 156 | + // 先实例化 |
| 157 | + GameObject instanceRoot = PrefabUtility.InstantiatePrefab(gameObj) as GameObject; |
| 158 | + // 绑定AnimationController |
| 159 | + Animator animator = instanceRoot.GetComponent<Animator>(); |
| 160 | + animator.runtimeAnimatorController = animatorCtorl; |
| 161 | + // 赋值材质 |
| 162 | + SkinnedMeshRenderer skinMeshRender = instanceRoot.GetComponentInChildren<SkinnedMeshRenderer>(); |
| 163 | + skinMeshRender.material = mat; |
| 164 | + // 保存为预制体 |
| 165 | + // path.Replace(".fbx", ".prefab") |
| 166 | + var variantRoot = PrefabUtility.SaveAsPrefabAsset(instanceRoot, buildPath + "/" + Path.GetFileNameWithoutExtension(path) + ".prefab"); |
| 167 | + // 删除游戏物体 |
| 168 | + Object.DestroyImmediate(instanceRoot); |
| 169 | + } |
| 170 | + |
| 171 | + // 创建模型并且使用Laya Shader |
| 172 | + Material CreateMaterial(string path) |
| 173 | + { |
| 174 | + Material mat = new Material(Shader.Find("LayaAir3D/Mesh/Unlit")); |
| 175 | + // 读取纹理 |
| 176 | + Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path.Replace(".fbx", "_base.png")); |
| 177 | + if (tex == null){ |
| 178 | + Debug.LogError("未找到该纹理贴图" + path.Replace(".fbx", "_base.png")); |
| 179 | + // 重新查找 |
| 180 | + tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path.Replace(".fbx", ".png")); |
| 181 | + } |
| 182 | + mat.mainTexture = tex; |
| 183 | + //保存材质球 |
| 184 | + AssetDatabase.CreateAsset(mat, path.Replace(".fbx", ".mat")); |
| 185 | + AssetDatabase.Refresh(); |
| 186 | + return mat; |
| 187 | + } |
| 188 | + |
| 189 | + // 添加状态 |
| 190 | + void AddStateTransition(string path, AnimatorControllerLayer layer) |
| 191 | + { |
| 192 | + AnimatorStateMachine sm = layer.stateMachine; |
| 193 | + // 读取所有动画AnimationClip对象 |
| 194 | + Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path); |
| 195 | + foreach (Object o in objs) |
| 196 | + { |
| 197 | + if (o is AnimationClip) |
| 198 | + { |
| 199 | + AnimationClip newClip = o as AnimationClip; |
| 200 | + if (newClip == null || newClip.name.Contains("__preview__")) |
| 201 | + continue; |
| 202 | + // 取出动画名子 添加到state里面 |
| 203 | + AnimatorState state = sm.AddState(newClip.name); |
| 204 | + state.motion = newClip; |
| 205 | + // AnimatorStateTransition trans = sm.AddAnyStateTransition(state); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + // ->选择对象发生改变 |
| 211 | + public void OnSelectionChange() |
| 212 | + { |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + // ->获得焦点 |
| 217 | + public void OnFocus() |
| 218 | + { |
| 219 | + |
| 220 | + } |
| 221 | + // ->失去焦点 |
| 222 | + public void OnLostFocus() |
| 223 | + { |
| 224 | + |
| 225 | + } |
| 226 | + // ->Hierarchay视图窗口文件发生改变 |
| 227 | + public void OnHierarchayChange() |
| 228 | + { |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + // ->Project视图窗口文件发生改变 |
| 233 | + public void OnProjectChange() |
| 234 | + { |
| 235 | + |
| 236 | + } |
| 237 | + // ->销毁窗口 |
| 238 | + public void OnDestroy() |
| 239 | + { |
| 240 | + |
| 241 | + } |
| 242 | +} |
0 commit comments