-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreviewRoleAnimaWindow.cs
652 lines (601 loc) · 22.5 KB
/
PreviewRoleAnimaWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using Sirenix.Utilities;
using System.IO;
using System.Linq;
using Sirenix.OdinInspector;
namespace YGame.Drama
{
public class Element : SerializedScriptableObject
{
[HideInInspector]
public string _name;
[HideInInspector]
public string path;
[HideInInspector]
public string meshPath;
[HideInInspector]
public bool select;
[HideInInspector]
public Texture icon;
[ReadOnly]
public GameObject meshAsset;
[ReadOnly]
public AnimationClip asset;
}
[CustomEditor(typeof(Element))]
public class ElementInspector : OdinEditor
{
private GameObject fbx;
private AnimationClip animationClip;
private GameObject m_root;
private GameObject m_PreviewInstance;
private Vector3 centerPosition;
private int cullingLayer = 0;
private float time = 0;
private bool m_Playing = true;
private float m_lastUpdateTime = 0;
private float m_AvatarScale = 1f;
private float m_ZoomFactor = 1f;
private Vector3 m_PivotPositionOffset = new Vector3(0, 1, 0);
private float m_BoundingVolumeScale;
private ViewTool m_ViewTool;
private Vector2 m_PreviewDir = new Vector2(120f, -20f);
public override void OnInspectorGUI()
{
if (target == null)
{
return;
}
var _target = target as Element;
////////////////////////////
base.OnInspectorGUI();
//EditorGUILayout.BeginVertical();
fbx = _target.meshAsset;
animationClip = _target.asset;
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("播放"))
{
m_Playing = true;
time = 0;
}
if (GUILayout.Button("暂停"))
{
m_Playing = false;
time = 0;
AnimationMode.StartAnimationMode();
AnimationMode.SampleAnimationClip(m_PreviewInstance, animationClip, time);
Repaint();
}
EditorGUILayout.EndHorizontal();
var lastRect = GUILayoutUtility.GetLastRect();
var pos = GUIHelper.CurrentWindow.position;
// Debug.Log(GUIHelper.CurrentWindow.position);
var rect = new Rect(lastRect.xMin + 50, lastRect.yMax + 50, pos.width * 0.9f, pos.height * 0.9f);
Handles.DrawCamera(rect, camera);
GetPreviewObject();
var m_PreviewHint = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().GetHashCode();
var m_PreviewSceneHint = m_PreviewHint;
var controlID = GUIUtility.GetControlID(m_PreviewHint, FocusType.Passive, rect);
var typeForControl = Event.current.GetTypeForControl(controlID);
DoRenderPreview();
int controlID2 = GUIUtility.GetControlID(m_PreviewSceneHint, FocusType.Passive);
typeForControl = Event.current.GetTypeForControl(controlID2);
HandleViewTool(Event.current, typeForControl, controlID2, rect);
DoAvatarPreviewFrame(Event.current, typeForControl, rect);
}
/// <summary>
/// 视图工具
/// </summary>
protected enum ViewTool
{
None,
/// <summary>
/// 平移
/// </summary>
Pan,
/// <summary>
/// 缩放
/// </summary>
Zoom,
/// <summary>
/// 旋转
/// </summary>
Orbit
}
private Camera m_camera;
private Camera camera
{
get
{
if (m_camera == null)
{
if (m_root == null)
{
m_root = new GameObject("m_previewRoot");
}
m_camera = new GameObject("EditorCamera").AddComponent<Camera>();
var light = m_camera.gameObject.AddComponent<Light>();
light.Reset();
light.type = LightType.Directional;
cullingLayer = LayerMask.NameToLayer("PreviewCullingLayer");
m_camera.transform.SetParent(m_root.transform);
m_camera.cullingMask = 1 << cullingLayer;
m_camera.fieldOfView = 30f;
m_camera.clearFlags = CameraClearFlags.Color;
m_camera.backgroundColor = Color.grey;
m_camera.depth = -10;
DestroyImmediate(m_camera.GetComponent<AudioListener>());
}
return m_camera;
}
}
Camera GetPreviewCamera()
{
return camera;
}
protected ViewTool viewTool
{
get
{
Event current = Event.current;
if (m_ViewTool == ViewTool.None)
{
bool flag = current.control && Application.platform == RuntimePlatform.OSXEditor;
bool actionKey = EditorGUI.actionKey;
bool flag2 = !actionKey && !flag && !current.alt;
if ((current.button <= 0 && flag2) || (current.button <= 0 && actionKey) || current.button == 2)
{
m_ViewTool = ViewTool.Pan;
}
else
{
if ((current.button <= 0 && flag) || (current.button == 1 && current.alt))
{
m_ViewTool = ViewTool.Zoom;
}
else
{
if ((current.button <= 0 && current.alt) || current.button == 1)
{
m_ViewTool = ViewTool.Orbit;
}
}
}
}
return m_ViewTool;
}
}
protected MouseCursor currentCursor
{
get
{
switch (m_ViewTool)
{
case ViewTool.Pan:
return MouseCursor.Pan;
case ViewTool.Zoom:
return MouseCursor.Zoom;
case ViewTool.Orbit:
return MouseCursor.Orbit;
default:
return MouseCursor.Arrow;
}
}
}
public Vector3 bodyPosition
{
get
{
if (m_PreviewInstance == null)
{
return Vector3.zero;
}
return m_PreviewInstance.transform.position;
}
}
protected override void OnEnable()
{
EditorApplication.update -= InspectorUpdate;
EditorApplication.update += InspectorUpdate;
Clear();
base.OnEnable();
}
protected override void OnDisable()
{
Clear();
EditorApplication.update -= InspectorUpdate;
base.OnDisable();
}
void InspectorUpdate()
{
float curTime = Time.realtimeSinceStartup;
m_lastUpdateTime = Mathf.Min(m_lastUpdateTime, curTime);//very important!!!
float dt = curTime - m_lastUpdateTime;
PlayAnima(dt);
if (m_Playing)
{
Repaint();
}
m_lastUpdateTime = curTime;
}
void Clear()
{
if (m_root == null)
{
m_root = GameObject.Find("m_previewRoot");
}
if (m_root)
{
GameObject.DestroyImmediate(m_root);// DestroyImmediate(m_root);
}
animationClip = null;
m_root = null;
m_camera = null;
m_PreviewInstance = null;
}
private void DoRenderPreview()
{
Vector3 bodyPosition = this.bodyPosition;
Quaternion quaternion = Quaternion.identity;
Vector3 vector = Vector3.zero;
Quaternion quaternion2 = Quaternion.identity;
Vector3 pivotPos = Vector3.zero;
GetPreviewCamera().nearClipPlane = 0.5f * m_ZoomFactor;
GetPreviewCamera().farClipPlane = 100f * m_AvatarScale;
Quaternion rotation = Quaternion.Euler(-m_PreviewDir.y, -m_PreviewDir.x, 0f);
Vector3 position2 = rotation * (Vector3.forward * -5.5f * m_ZoomFactor) + bodyPosition + m_PivotPositionOffset;
GetPreviewCamera().transform.position = position2;
GetPreviewCamera().transform.rotation = rotation;
}
void GetPreviewObject()
{
if (m_PreviewInstance)
{
if (m_PreviewInstance.name != fbx.name)
{
DestroyImmediate(m_PreviewInstance);
m_PreviewInstance = null;
}
}
if (m_PreviewInstance == null)
{
if (fbx == null)
{
return;
}
m_PreviewInstance = Instantiate(fbx);
if (m_root == null)
{
m_root = new GameObject("m_previewRoot");
}
m_PreviewInstance.transform.SetParent(m_root.transform);
m_PreviewInstance.name = fbx.name;
m_PreviewInstance.transform.position = Vector3.one * 2000;
m_PreviewInstance.layer = cullingLayer;
foreach (var i in m_PreviewInstance.transform.GetComponentsInChildren<Transform>())
{
i.gameObject.layer = cullingLayer;
}
var anima = m_PreviewInstance.AddComponent<Animation>();
Bounds bounds = new Bounds(m_PreviewInstance.transform.position, Vector3.zero);
foreach (var _renderer in m_PreviewInstance.GetComponentsInChildren<Renderer>())
{
bounds.Encapsulate(_renderer.bounds);
}
centerPosition = bounds.center;
camera.transform.position = centerPosition + Vector3.forward * -5;
}
}
void PlayAnima(float dt)
{
if (m_PreviewInstance == null || animationClip == null)
{
return;
}
if (m_Playing)
{
if (time <= 1)
{
time += dt;
AnimationMode.StartAnimationMode();
AnimationMode.SampleAnimationClip(m_PreviewInstance, animationClip, time);
if (time >= 1)
{
time = 0;
}
}
}
else
{
time = 0;
}
}
protected void HandleMouseDown(Event evt, int id, Rect previewRect)
{
if (viewTool != ViewTool.None && previewRect.Contains(evt.mousePosition))
{
EditorGUIUtility.SetWantsMouseJumping(1);
evt.Use();
GUIUtility.hotControl = id;
}
}
protected void HandleMouseUp(Event evt, int id)
{
if (GUIUtility.hotControl == id)
{
m_ViewTool = ViewTool.None;
GUIUtility.hotControl = 0;
EditorGUIUtility.SetWantsMouseJumping(0);
evt.Use();
}
}
protected void HandleMouseDrag(Event evt, int id, Rect previewRect)
{
if (m_PreviewInstance == null)
{
return;
}
if (GUIUtility.hotControl == id)
{
switch (m_ViewTool)
{
case ViewTool.Pan:
DoAvatarPreviewPan(evt);
break;
case ViewTool.Zoom:
DoAvatarPreviewZoom(evt, -HandleUtility.niceMouseDeltaZoom * ((!evt.shift) ? 0.5f : 2f));
break;
case ViewTool.Orbit:
DoAvatarPreviewOrbit(evt, previewRect);
break;
default:
Debug.Log("Enum value not handled");
break;
}
}
}
protected void HandleViewTool(Event evt, EventType eventType, int id, Rect previewRect)
{
switch (eventType)
{
case EventType.MouseDown:
HandleMouseDown(evt, id, previewRect);
break;
case EventType.MouseUp:
HandleMouseUp(evt, id);
break;
case EventType.MouseDrag:
HandleMouseDrag(evt, id, previewRect);
break;
case EventType.ScrollWheel:
DoAvatarPreviewZoom(evt, HandleUtility.niceMouseDeltaZoom * ((!evt.shift) ? 0.5f : 2f));
break;
}
}
public void DoAvatarPreviewOrbit(Event evt, Rect previewRect)
{
m_PreviewDir -= evt.delta * (float)((!evt.shift) ? 1 : 3) / Mathf.Min(previewRect.width, previewRect.height) * 140f;
m_PreviewDir.y = Mathf.Clamp(m_PreviewDir.y, -90f, 90f);
evt.Use();
}
public void DoAvatarPreviewPan(Event evt)
{
Vector3 vector = camera.WorldToScreenPoint(bodyPosition + m_PivotPositionOffset);
Vector3 a = new Vector3(-evt.delta.x, evt.delta.y, 0f);
vector += a * Mathf.Lerp(0.25f, 2f, m_ZoomFactor * 0.5f);
Vector3 b = camera.ScreenToWorldPoint(vector) - (bodyPosition + m_PivotPositionOffset);
m_PivotPositionOffset += b;
evt.Use();
}
/// <summary>
/// 定位事件
/// 按F近距离查看对象
/// 按G视图平移到鼠标位置
/// </summary>
/// <param name="evt"></param>
/// <param name="type"></param>
/// <param name="previewRect"></param>
public void DoAvatarPreviewFrame(Event evt, EventType type, Rect previewRect)
{
if (type == EventType.KeyDown && evt.keyCode == KeyCode.F)
{
m_PivotPositionOffset = new Vector3(0, 1, 0);
m_ZoomFactor = m_AvatarScale;
evt.Use();
}
if (type == EventType.KeyDown && Event.current.keyCode == KeyCode.G)
{
m_PivotPositionOffset = GetCurrentMouseWorldPosition(evt, previewRect) - bodyPosition;
evt.Use();
}
if (type == EventType.KeyDown && evt.keyCode == KeyCode.Space)
{
if (m_Playing)
{
m_Playing = false;
}
else
{
m_Playing = true;
}
evt.Use();
}
}
protected Vector3 GetCurrentMouseWorldPosition(Event evt, Rect previewRect)
{
float scaleFactor = 1;
return camera.ScreenToWorldPoint(new Vector3((evt.mousePosition.x - previewRect.x) * scaleFactor, (previewRect.height - (evt.mousePosition.y - previewRect.y)) * scaleFactor, 0f)
{
z = Vector3.Distance(bodyPosition, camera.transform.position)
});
}
public void DoAvatarPreviewZoom(Event evt, float delta)
{
float num = -delta * 0.05f;
m_ZoomFactor += m_ZoomFactor * num;
m_ZoomFactor = Mathf.Max(m_ZoomFactor, m_AvatarScale / 10f);
evt.Use();
}
}
public class PreviewRoleAnimaWindow : OdinMenuEditorWindow
{
private Dictionary<string, List<Element>> dic = new Dictionary<string, List<Element>>();
private string path = "Assets/Develop/Art/Story/Common/Character/Cromwell/Set0";
private string meshPath = "Assets/Develop/Art/Roles/Character/Cromwell/Set0/Rig/R_Cromwell0_HQ.FBX";
[MenuItem("YGame/Tools/RoleAnimaPreview[角色动画预览]")]
public static void OpenWindow()
{
var winodw = GetWindow<PreviewRoleAnimaWindow>()
.position = GUIHelper.GetEditorWindowRect().AlignCenter(800, 600).SetSize(800, 600);
// winodw.MaxHeight(800);
// winodw.MaxWidth(800);
// winodw.MinHeight(800);
// winodw.MinWidth(800);
}
protected override void OnDestroy()
{
if (dic != null)
{
foreach (var i in dic.Values)
{
foreach (var x in i)
{
ScriptableObject.DestroyImmediate(x);
}
}
dic.Clear();
}
var m_root = GameObject.Find("m_previewRoot");
if (m_root)
{
DestroyImmediate(m_root);
}
}
protected override OdinMenuTree BuildMenuTree()
{
AddTreeElement();
var tree = new OdinMenuTree();
tree.DefaultMenuStyle.IconSize = 15;
tree.Config.DrawSearchToolbar = true;
tree.Config.SearchToolbarHeight = 30;
tree.DefaultMenuStyle.Height = 25;
foreach (var i in dic)
{
if (i.Value == null || i.Value.Count == 0)
{
tree.Add(i.Key, null);
}
else
{
foreach (var x in i.Value)
{
var key = i.Key + "/" + x._name;
tree.Add(key, x, x.icon);
}
}
}
tree.EnumerateTree().Where(x => x.Value as Element).ForEach(AddDragHandles);
tree.SortMenuItemsByName();
return tree;
}
private void AddDragHandles(OdinMenuItem menuItem)
{
menuItem.OnDrawItem += x => Drag(menuItem);
}
private void Drag(OdinMenuItem menuItem)
{
if (menuItem.IsSelected)
{
if (Event.current.type == EventType.MouseDown)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.StartDrag("elemnet");
var g = (menuItem.Value as Element).asset;
DragAndDrop.objectReferences = new Object[1] { g };
}
switch (Event.current.type)
{
case EventType.DragUpdated:
case EventType.DragPerform:
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (Event.current.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
}
Event.current.Use();
break;
default:
break;
}
}
}
private void AddTreeElement()
{
var roles = YGame.Drama.DramaImporter.GetDramaRoleList();
if (roles != null)
{
dic = new Dictionary<string, List<Element>>();
foreach (var i in roles)
{
if (string.IsNullOrEmpty(i.NameEditor) || string.IsNullOrEmpty(i.RoleAnimatorRoot) || string.IsNullOrEmpty(i.RoleBodyMeshPath))
{
continue;
}
var _key = i.NameEditor + "[" + i.NameEditorCN + "]";
var _path = i.RoleAnimatorRoot;
var _meshPath = i.RoleBodyMeshPath;
AddTreeElements(_key, _path, _meshPath);
}
}
}
private void AddTreeElements(string _key = "ROLE", string _path = "", string _meshPath = "")
{
if (dic.ContainsKey(_key))
{
return;
}
var elements = new List<Element>();
var _meshAsset = AssetDatabase.LoadAssetAtPath<GameObject>(_meshPath);
if (Directory.Exists(_path) == false)
{
return;
}
foreach (var i in Directory.GetFiles(_path))
{
var assets = AssetDatabase.LoadAllAssetsAtPath(i);
UnityEngine.Object asset = null;
foreach (var x in assets)
{
if (x is AnimationClip)
{
var id = x.GetInstanceID();
asset = EditorUtility.InstanceIDToObject(id);
break;
}
}
if (asset == null)
{
continue;
}
var element = ScriptableObject.CreateInstance<Element>();
element._name = asset.name;
element.path = i;
var assetPath = AssetDatabase.GetAssetPath(asset);
element.icon = AssetDatabase.GetCachedIcon(assetPath);
element.asset = asset as AnimationClip;
element.meshAsset = _meshAsset;
elements.Add(element);
}
dic.Add(_key, elements);
}
}
}
#endif