-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathObjectDrawerPickerTool.cs
164 lines (147 loc) · 5.33 KB
/
ObjectDrawerPickerTool.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
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.EditorTools;
#if UNITY_2021_2_OR_NEWER
using UnityEditor.SceneManagement;
#else
using UnityEditor.Experimental.SceneManagement;
#endif
using UnityEngine;
using Object = UnityEngine.Object;
namespace cratesmith.assetui
{
class ObjectDrawerPickerTool : EditorTool
{
Object m_PickObj;
Type m_PropType;
Object[] m_TargetObjects;
string m_SerializedPropertyPath;
static Texture2D s_PickerButton;
static ObjectDrawerPickerTool s_Instance;
public static ObjectDrawerPickerTool Instance => s_Instance = s_Instance
? s_Instance
: CreateInstance<ObjectDrawerPickerTool>();
public static Texture2D PickerIcon => s_PickerButton
? s_PickerButton
: s_PickerButton = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("objectdrawer_pick t:texture").FirstOrDefault()));
public override GUIContent toolbarIcon => new GUIContent(PickerIcon);
public override bool IsAvailable() => m_TargetObjects != null && m_TargetObjects.Length>0;
public static bool CanPickProperty(SerializedProperty property)
{
if(property== null || property.propertyType != SerializedPropertyType.ObjectReference)
return false;
var propType = property.GetSerializedPropertyType();
if (propType!=typeof(Object)
&& !typeof(GameObject).IsAssignableFrom(propType)
&& !typeof(Component).IsAssignableFrom(propType))
return false;
foreach (var target in property.serializedObject.targetObjects)
{
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(target)))
return false;
}
return true;
}
public static void DoPicker(SerializedProperty property)
{
if (property == null || property.propertyType != SerializedPropertyType.ObjectReference)
{
Debug.LogError("Picker must point to an object reference property");
}
Instance.m_SerializedPropertyPath = property.propertyPath;
Instance.m_PropType = property.GetSerializedPropertyType();
Instance.m_TargetObjects = property.serializedObject.targetObjects;
ToolManager.SetActiveTool(Instance);
}
public override void OnActivated()
{
m_PickObj = null;
Selection.selectionChanged += SelectionChanged;
}
void SelectionChanged()
{
ToolManager.RestorePreviousTool();
}
public override void OnWillBeDeactivated()
{
m_SerializedPropertyPath = null;
m_TargetObjects = null;
m_PickObj = null;
m_PropType = null;
Selection.selectionChanged -= SelectionChanged;
DestroyImmediate(s_Instance);
}
public override void OnToolGUI(EditorWindow window)
{
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
if (!(window is SceneView sceneView))
{
Debug.LogError("Not a scene view");
return;
}
var serializedObject = m_TargetObjects != null && m_TargetObjects.Length > 0 ? new SerializedObject(m_TargetObjects) : null;
var property = serializedObject != null ? serializedObject.FindProperty(m_SerializedPropertyPath):null;
if (property==null || m_PropType==null)
{
ToolManager.RestorePreviousTool();
return;
}
if (Event.current.type == EventType.MouseMove)
{
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
var filter = typeof(Component).IsAssignableFrom(m_PropType)
? (prefabStage==null
? GameObject.FindObjectsOfType(m_PropType)
: prefabStage.prefabContentsRoot.GetComponentsInChildren(m_PropType))
.SelectMany(x=>((Component)x).GetComponentsInChildren<Component>())
.Select(x=>x.gameObject).ToArray()
: null;
if (!TryPickObject(Event.current.mousePosition, true, filter, out m_PickObj))
{
TryPickObject(Event.current.mousePosition, false, filter, out m_PickObj);
}
}
if (m_PickObj)
{
EditorGUIUtility.AddCursorRect(sceneView.position, MouseCursor.Link);
Handles.Label(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition - Vector2.down*30f).GetPoint(0), m_PickObj.name);
}
if (Event.current.type == EventType.MouseDown
&& Event.current.button==0
&& m_PickObj)
{
Undo.RecordObjects(m_TargetObjects, $"Picking object {m_PickObj.name}");
property.objectReferenceValue = m_PickObj;
serializedObject.ApplyModifiedProperties();
ToolManager.RestorePreviousTool();
}
}
bool TryPickObject(Vector2 position, bool selectPrefabRoot, GameObject[] filter, out Object result)
{
var pickGO = HandleUtility.PickGameObject(position, selectPrefabRoot, null, filter);
return result = pickGO==null || !typeof(Component).IsAssignableFrom(m_PropType)
? pickGO
: (Object)pickGO.GetComponentInParent(m_PropType);
}
public static bool IsPickingFor(SerializedProperty property)
{
return ToolManager.IsActiveTool(Instance)
&& Instance.m_SerializedPropertyPath !=null
&& Instance.m_TargetObjects!=null
&& property !=null
&& new HashSet<Object>(property.serializedObject.targetObjects).SetEquals(Instance.m_TargetObjects)
&& property.propertyPath == Instance.m_SerializedPropertyPath;
}
public static void Cancel()
{
if (ToolManager.IsActiveTool(Instance))
{
ToolManager.RestorePreviousTool();
}
}
}
}
#endif