少年修仙传客户端基础资源
dabaoji
2023-11-13 0a23e1b83f8fda8ab9f9e32fe71c26c431faf63a
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
using System;
using UnityEngine;
using UnityEditor;
using qtools.qhierarchy.pcomponent.pbase;
using qtools.qhierarchy.phierarchy;
using qtools.qhierarchy.phelper;
using qtools.qhierarchy.pdata;
using System.Reflection;
 
namespace qtools.qhierarchy.pcomponent
{
    public class QGameObjectIconComponent: QBaseComponent
    {
        // PRIVATE
        private MethodInfo getIconMethodInfo;
        private object[] getIconMethodParams;
        private Color backgroundColor;
 
        // CONSTRUCTOR
        public QGameObjectIconComponent ()
        {
            getIconMethodInfo   = typeof(EditorGUIUtility).GetMethod("GetIconForObject", BindingFlags.NonPublic | BindingFlags.Static );
            getIconMethodParams = new object[1];
 
            backgroundColor = QResources.getInstance().getColor(QColor.Background);
 
            QSettings.getInstance().addEventListener(QSetting.ShowGameObjectIconComponent   , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowGameObjectIconComponentDuringPlayMode   , settingsChanged);
            settingsChanged();
        }
        
        // PRIVATE
        private void settingsChanged()
        {
            enabled = QSettings.getInstance().get<bool>(QSetting.ShowGameObjectIconComponent);
            showComponentDuringPlayMode = QSettings.getInstance().get<bool>(QSetting.ShowGameObjectIconComponentDuringPlayMode);
        }
 
        // DRAW
        public override void layout(GameObject gameObject, QObjectList objectList, ref Rect rect)
        {
            rect.x -= 18;
            rect.width = 18;
        }
 
        public override void draw(GameObject gameObject, QObjectList objectList, Rect selectionRect, Rect rect)
        {                      
            getIconMethodParams[0] = gameObject;
            Texture2D icon = (Texture2D)getIconMethodInfo.Invoke(null, getIconMethodParams );              
            EditorGUI.DrawRect(rect, backgroundColor);
            if (icon != null)
            {
                rect.width = 16;
                GUI.DrawTexture(rect, icon, ScaleMode.ScaleToFit, true);
            }
        }
                
        public override void eventHandler(GameObject gameObject, QObjectList objectList, Event currentEvent, Rect curRect)
        {
            if (currentEvent.isMouse && currentEvent.type == EventType.MouseDown && currentEvent.button == 0 && curRect.Contains(currentEvent.mousePosition))
            {
                currentEvent.Use();
 
                Type iconSelectorType = Assembly.Load("UnityEditor").GetType("UnityEditor.IconSelector");
                MethodInfo showIconSelectorMethodInfo = iconSelectorType.GetMethod("ShowAtPosition", BindingFlags.Static | BindingFlags.NonPublic);
                showIconSelectorMethodInfo.Invoke(null, new object[] { gameObject, curRect, true });
            }
        }
    }
}