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
|
public class FindReferencesInProjectEditor : EditorWindow { private Rect m_viewportRect = default; private Rect m_listRect = default; private Rect m_bottomRect = default;
private float m_scrollPosition = default; private string[] m_targetPaths = null;
private const float ELEMENT_HEIGHT = 20.0f; private const float ELEMENT_ICON_SIZE = 20.0f; private const float SCROLLER_WIDTH = 15.0f;
private bool m_hasGetGUIStyle = false;
private GUIStyle m_scrollerStyle = null; private GUIStyle m_buttonElementStyle = null;
private UnityEngine.Object m_selectedUnityObject = null; private System.Type m_uobjType = null;
public static void OpenWindow() { FindReferencesInProjectEditor window = GetWindow<FindReferencesInProjectEditor>(); Texture2D icon = EditorGUIUtility.Load("icons/UnityEditor.ConsoleWindow.png") as Texture2D; window.titleContent = new GUIContent("reference find result", icon); Vector2 minSize = new Vector2(500.0f, 300.0f); window.minSize = minSize; }
public static FindReferencesInProjectEditor GetWindow() { return GetWindow<FindReferencesInProjectEditor>(); }
public void SetPaths(in string[] paths) { m_targetPaths = paths; }
public void SetTargetUobject(UnityEngine.Object uobject) { m_selectedUnityObject = uobject; }
private void DrawContent() { m_viewportRect = new Rect(0.0f, 0.0f, this.position.width - SCROLLER_WIDTH, this.position.height * 0.8f); m_listRect = new Rect(0.0f, 0.0f, m_viewportRect.width, m_viewportRect.height); int elementCount = m_targetPaths.Length;
if (elementCount < 1) return;
GUI.BeginClip(m_listRect); int indexOffset = Mathf.FloorToInt(m_scrollPosition / ELEMENT_HEIGHT); int showCount = Mathf.CeilToInt(m_listRect.height / ELEMENT_HEIGHT); showCount = showCount > elementCount ? elementCount : showCount; float startPosY = (indexOffset * ELEMENT_HEIGHT) - m_scrollPosition;
for (int i = 0; i < showCount; i++) { Rect elementRect = new Rect(0, 0 + startPosY + i * ELEMENT_HEIGHT, m_listRect.width, ELEMENT_HEIGHT); DrawElement(elementRect, indexOffset + i); } GUI.EndClip(); }
private void DrawElement(Rect elementRect, int dataIndex) { if (dataIndex < m_targetPaths.Length && GUI.Button(elementRect, $"path {m_targetPaths[dataIndex]} ;", m_buttonElementStyle)) { Debug.Log($"{m_targetPaths[dataIndex]}"); UnityEngine.Object targetObj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(m_targetPaths[dataIndex]); EditorGUIUtility.PingObject(targetObj); } }
private void DrawScroller() { int elementCount = m_targetPaths.Length; float fullElementHeight = elementCount * ELEMENT_HEIGHT;
Rect scrollbarRect = new Rect(m_viewportRect.x + m_viewportRect.width, m_viewportRect.y, SCROLLER_WIDTH, m_viewportRect.height); m_scrollPosition = Mathf.Max(0, GUI.VerticalScrollbar(scrollbarRect, m_scrollPosition, m_listRect.height, 0, Mathf.Max(fullElementHeight, m_listRect.height)));
int controlId = GUIUtility.GetControlID(FocusType.Passive); float scrollSensitivity = ELEMENT_HEIGHT; float maxScrollPos = (fullElementHeight > m_listRect.height) ? (fullElementHeight - m_listRect.height) : 0;
if (EventType.ScrollWheel == Event.current.GetTypeForControl(controlId)) { m_scrollPosition = Mathf.Clamp(m_scrollPosition + Event.current.delta.y * scrollSensitivity, 0, maxScrollPos); Event.current.Use(); } }
private void GetGUIStyle() { m_scrollerStyle = new GUIStyle(GUI.skin.verticalScrollbar); m_scrollerStyle.stretchWidth = false; m_scrollerStyle.fixedWidth = SCROLLER_WIDTH;
m_buttonElementStyle = new GUIStyle(GUI.skin.button); m_buttonElementStyle.alignment = TextAnchor.MiddleLeft;
m_uobjType = typeof(UnityEngine.Object); }
private void DrawBottom() { m_bottomRect = new Rect(0.0f, this.position.height * 0.8f, this.position.width, this.position.height * 0.2f); Rect fieldRect = new Rect(10.0f, m_bottomRect.y + 10.0f, 100.0f, 20.0f);
if (null == m_uobjType) m_uobjType = typeof(UnityEngine.Object);
m_selectedUnityObject = EditorGUI.ObjectField(fieldRect, m_selectedUnityObject, m_uobjType, true);
Rect buttonRect = new Rect(150.0f, m_bottomRect.y, 200.0f, 40.0f); if (GUI.Button(buttonRect, "try find reference")) { FindObjectReference(); }
}
private void FindObjectReference() { if (null == m_selectedUnityObject) { return; }
Clear(); FindReferencesInProject.TryFindReferenceOfUobject(m_selectedUnityObject); }
private void Clear() { m_targetPaths = System.Array.Empty<string>(); }
private void OnGUI() { if (!m_hasGetGUIStyle) { GetGUIStyle(); m_hasGetGUIStyle = true; }
DrawContent(); DrawScroller(); DrawBottom(); }
}
|