-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseAsReference.cs
168 lines (146 loc) · 5.14 KB
/
UseAsReference.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.UI;
/// <summary>
/// 将一张图片用作参考效果图
/// 目前的快捷键:
/// 选中某张图片后:
/// "~":快速将选中的图片作为参考图(背景)
/// "Shift+~":将选中图片作为参考图 (前景,50%透明度)
///
/// 创建参考图后:
/// "Ctrl+~":清除参考图
///
/// 在SceneView中:
/// "F1":将参考图切换到最底层/最上层
/// "Ctrl+F1":切换参考图是否可以被选中
///
/// </summary>
public static class UseAsReference
{
//static GameObject refImage;
static GameObject RefImage
{
get { return GameObject.Find(RefImageName); }
}
const string RefImageName = "83E16BBB-DCCA-4DDF-A580-426B49180D97";
const HideFlags hideFlags = HideFlags.HideAndDontSave; //创建出的效果图默认层
const KeyCode alterKey = KeyCode.F1; //切换是否可选中的热键
static bool IsSelectionTexture()
{
return Selection.activeObject != null && Selection.activeObject.GetType().IsSubclassOf(typeof(Texture));
}
[MenuItem("Assets/UI Utils/用作效果图(最上层) #`", true, 11)]
static bool UseAsRefCheckTop()
{
return IsSelectionTexture();
}
[MenuItem("Assets/UI Utils/用作效果图(最上层) #`", false, 11)]
private static void UseAsRefTop()
{
CreateAsRef(true);
}
[MenuItem("Assets/UI Utils/用作效果图(最底层) _`", true, 10)]
static bool UseAsRefCheck()
{
return IsSelectionTexture();
}
[MenuItem("Assets/UI Utils/用作效果图(最底层) _`", false, 10)]
private static void UseAsRef()
{
CreateAsRef(false);
}
[MenuItem("Assets/UI Utils/清除效果图 %`", true, 12)]
static bool ClearRefCheck()
{
return RefImage != null;
}
[MenuItem("Assets/UI Utils/清除效果图 %`", false, 12)]
private static void ClearRef()
{
SceneView.onSceneGUIDelegate -= OnSceneChange;
while (RefImage != null)
{
GameObject.DestroyImmediate(RefImage);
}
}
static void CreateAsRef(bool atTop)
{
Canvas canvas = UIUtils.GetOrCreateCanvasGameObject().GetComponent<Canvas>().rootCanvas;
GameObject gameObject = CreateRefrence(canvas, atTop);
gameObject.hideFlags = hideFlags;
//使SceneView能接受当前的操作
PrepareSceneView();
}
private static GameObject CreateRefrence(Canvas canvas, bool atTop)
{
GameObject old = GameObject.Find(RefImageName);
if (old != null)
GameObject.DestroyImmediate(old);
GameObject refImage = new GameObject(RefImageName, typeof(RectTransform));
refImage.transform.SetParent(canvas.transform, false);
refImage.transform.localScale = Vector3.one;
refImage.transform.localPosition = Vector3.zero;
RawImage rawImage = refImage.AddComponent<RawImage>();
rawImage.texture = Selection.activeObject as Texture;
rawImage.SetNativeSize();
if (atTop)
{
refImage.transform.SetSiblingIndex(refImage.transform.parent.childCount - 1);
rawImage.color = new Color(1, 1, 1, 0.5f);
}
else
{
refImage.transform.SetSiblingIndex(0);
}
return refImage;
}
private static void PrepareSceneView()
{
SceneView.onSceneGUIDelegate -= OnSceneChange;
SceneView.onSceneGUIDelegate += OnSceneChange;
}
static bool isCtrlDown = false;
static void OnSceneChange(SceneView scene)
{
if (RefImage != null)
{
if (Event.current.keyCode == KeyCode.LeftControl)
{
if (Event.current.type == EventType.KeyDown)
isCtrlDown = true;
else if (Event.current.type == EventType.KeyUp)
isCtrlDown = false;
}
if (Event.current.type == EventType.KeyDown)
{
//Debug.Log("KeyDown:" + Event.current.keyCode);
if (Event.current.keyCode == alterKey && isCtrlDown)
{
if (RefImage.hideFlags == HideFlags.None)
{
RefImage.hideFlags = hideFlags;
Selection.activeGameObject = null;
}
else
RefImage.hideFlags = HideFlags.None;
}
else if (Event.current.keyCode == alterKey)
{
RawImage rawImage = RefImage.GetComponent<RawImage>();
int sibling = RefImage.transform.GetSiblingIndex();
if (sibling == 0)
sibling = RefImage.transform.parent.childCount - 1;
else
sibling = 0;
rawImage.transform.SetSiblingIndex(sibling);
Event.current.Use();
}
EditorApplication.RepaintHierarchyWindow();
}
}
}
}