Skip to content

Commit 3ff7641

Browse files
committed
change unity version to 2019.3.5 and add learn unity editor demo
1 parent 77900dc commit 3ff7641

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3662
-35
lines changed

Assets/Editor/CreateAnimatorAndPrefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
}

Assets/Editor/CreateAnimatorAndPrefab/BuildAnimationAndPrefab.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/DragFloderWindow.cs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
public class DragFloderWindow : EditorWindow
4+
{
5+
6+
// 当前选择路径
7+
public string path = "Assets";
8+
public Rect pathRect;
9+
public GUILayoutOption test;
10+
// 打包后的文件
11+
public string buildPath = "Assets/Build";
12+
public Rect buildPathRect;
13+
14+
15+
[MenuItem("Tools/文件拖拽")]
16+
static void createWindow()
17+
{
18+
var window = EditorWindow.GetWindow<DragFloderWindow>(false, "FolderWindow");
19+
window.minSize = new Vector2(400, 400);
20+
window.Show();
21+
}
22+
private void OnGUI()
23+
{
24+
// 需要构建的路径
25+
EditorGUILayout.Space();
26+
EditorGUILayout.LabelField("需要打包的路径 (鼠标拖拽文件夹到这里)");
27+
EditorGUILayout.Space();
28+
// this.path = EditorGUILayout.TextField(this.path);
29+
GUI.SetNextControlName("input1");//设置下一个控件的名字
30+
pathRect = EditorGUILayout.GetControlRect(GUILayout.Width(400));
31+
EditorGUI.TextField(pathRect, path);
32+
EditorGUILayout.Space();
33+
34+
// 构建后的路径
35+
EditorGUILayout.LabelField("打包后的路径 (鼠标拖拽文件夹到这里)");
36+
EditorGUILayout.Space();
37+
// this.buildPath = EditorGUILayout.TextField(this.buildPath);
38+
GUI.SetNextControlName("input2");//设置下一个控件的名字
39+
buildPathRect = EditorGUILayout.GetControlRect(GUILayout.Width(400));
40+
EditorGUI.TextField(buildPathRect, buildPath);
41+
EditorGUILayout.Space();
42+
43+
// 文件拖拽
44+
DragFolder();
45+
}
46+
47+
void DragFolder()
48+
{
49+
//鼠标位于当前窗口
50+
if (mouseOverWindow == this)
51+
{
52+
//拖入窗口未松开鼠标
53+
if (Event.current.type == EventType.DragUpdated)
54+
{
55+
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;//改变鼠标外观
56+
// 判断区域
57+
if (pathRect.Contains(Event.current.mousePosition))
58+
GUI.FocusControl("input1");
59+
else if (buildPathRect.Contains(Event.current.mousePosition))
60+
GUI.FocusControl("input2");
61+
}
62+
//拖入窗口并松开鼠标
63+
else if (Event.current.type == EventType.DragExited)
64+
{
65+
//获取焦点,
66+
string dragPath = string.Join("", DragAndDrop.paths);
67+
// 判断区域
68+
if (pathRect.Contains(Event.current.mousePosition))
69+
this.path = dragPath;
70+
else if (buildPathRect.Contains(Event.current.mousePosition))
71+
this.buildPath = dragPath;
72+
// Debug.Log("path:" + path);
73+
// Debug.Log("buildPath:" + buildPath);
74+
75+
// 取消焦点(不然GUI不会刷新)
76+
GUI.FocusControl(null);
77+
}
78+
}
79+
}
80+
}

Assets/Editor/DragFloderWindow.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/LearnEditor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)