少年修仙传客户端基础资源
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
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
using System;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using qtools.qhierarchy.pcomponent.pbase;
using qtools.qhierarchy.pdata;
using qtools.qhierarchy.phelper;
 
namespace qtools.qhierarchy.pcomponent
{
    public class QComponentsComponent: QBaseComponent
    {
        // PRIVATE
        private GUIStyle hintLabelStyle;
        private Color grayColor;
        private Color backgroundColor;
        private Color backgroundDarkColor;
        private Texture2D componentIcon;
        private Component[] components;        
 
        // CONSTRUCTOR
        public QComponentsComponent ()
        {
            this.backgroundColor     = QResources.getInstance().getColor(QColor.Background);
            this.backgroundDarkColor = QResources.getInstance().getColor(QColor.BackgroundDark);
            this.grayColor           = QResources.getInstance().getColor(QColor.Gray);
            this.componentIcon       = QResources.getInstance().getTexture(QTexture.QComponentUnknownIcon);
 
            hintLabelStyle = new GUIStyle();
            hintLabelStyle.normal.textColor = grayColor;
            hintLabelStyle.fontSize = 11;
            hintLabelStyle.clipping = TextClipping.Clip;  
 
            QSettings.getInstance().addEventListener(QSetting.ShowComponentsComponent              , settingsChanged);
            QSettings.getInstance().addEventListener(QSetting.ShowComponentsComponentDuringPlayMode, settingsChanged);
            settingsChanged();
        }
 
        // PRIVATE
        private void settingsChanged()
        {
            enabled = QSettings.getInstance().get<bool>(QSetting.ShowComponentsComponent);
            showComponentDuringPlayMode = QSettings.getInstance().get<bool>(QSetting.ShowComponentsComponentDuringPlayMode);
        }
 
        // DRAW
        public override void layout(GameObject gameObject, QObjectList objectList, ref Rect curRect)
        {
            components = gameObject.GetComponents<Component>();
            int totalWidth = 2 + 16 * (components.Length - 1);
            curRect.x -= totalWidth;
            curRect.width = totalWidth;
        }
 
        public override void draw(GameObject gameObject, QObjectList objectList, Rect selectionRect, Rect curRect)
        {
            EditorGUI.DrawRect(curRect, backgroundColor);
            curRect.width = 16;
 
            for (int i = 0, n = components.Length; i < n; i++)
            {
                Component component = components[i];
                if (component is Transform) continue;
                                
                GUIContent content = EditorGUIUtility.ObjectContent(component, null);
 
                bool enabled = true;
                try
                {
                    PropertyInfo propertyInfo = component.GetType().GetProperty("enabled");
                    enabled = (bool)propertyInfo.GetGetMethod().Invoke(component, null);
                }
                catch {}
 
                Color color = GUI.color;
                color.a = enabled ? 1f : 0.3f;
                GUI.color = color;
                GUI.DrawTexture(curRect, content.image == null ? componentIcon : content.image);
                color.a = 1;
                GUI.color = color;
 
                if (curRect.Contains(Event.current.mousePosition))
                {        
                    string componentName = "Missing script";
                    if (component != null) componentName = component.GetType().Name;
 
                    int labelWidth = Mathf.CeilToInt(hintLabelStyle.CalcSize(new GUIContent(componentName)).x);                    
                    selectionRect.x = curRect.x - labelWidth / 2 - 4;
                    selectionRect.width = labelWidth + 8;
                    selectionRect.height -= 1;
 
                    if (selectionRect.y > 16) selectionRect.y -= 16;
                    else selectionRect.x += labelWidth;
 
                    EditorGUI.DrawRect(selectionRect, backgroundColor);
 
                    EditorGUI.DrawRect(selectionRect, backgroundDarkColor);
                    selectionRect.x += 4;
                    selectionRect.y += 1;
 
                    GUI.Label(selectionRect, componentName, hintLabelStyle);
                }
 
                curRect.x += 16;
            }
        }
 
        public override void eventHandler(GameObject gameObject, QObjectList objectList, Event currentEvent, Rect curRect)
        {
            if (currentEvent.isMouse && currentEvent.button == 0 && curRect.Contains(currentEvent.mousePosition))
            {
                if (currentEvent.type == EventType.MouseDown)
                {
                    int id = Mathf.FloorToInt((currentEvent.mousePosition.x - curRect.x) / 16) + 1;
 
                    try
                    {
                        PropertyInfo propertyInfo = components[id].GetType().GetProperty("enabled");
                        bool enabled = (bool)propertyInfo.GetGetMethod().Invoke(components[id], null);
                        Undo.RecordObject(components[id], enabled ? "Disable Component" : "Enable Component");
                        propertyInfo.GetSetMethod().Invoke(components[id], new object[] { !enabled });
                    }
                    catch {}
 
                    EditorUtility.SetDirty(gameObject);
                }
                currentEvent.Use();
            }
        }
    }
}