Tanoda
CUIGraphicEditor.cs
Go to the documentation of this file.
1
3
4using UnityEditor;
5
7{
8 [CustomEditor(typeof(CUIGraphic), true)]
9 public class CUIGraphicEditor : Editor {
10
11 protected static bool isCurveGpFold = false;
12
13 protected Vector3[] reuse_Vector3s = new Vector3[4];
14
15 public override void OnInspectorGUI()
16 {
17 CUIGraphic script = (CUIGraphic)this.target;
18
19 EditorGUILayout.HelpBox("CurlyUI (CUI) should work with most of the Unity UI. For Image, use CUIImage; for Text, use CUIText; and for others (e.g. RawImage), use CUIGraphic", MessageType.Info);
20
21 if (script.UIGraphic == null)
22 {
23 EditorGUILayout.HelpBox("CUI is an extension to Unity's UI. You must set Ui Graphic with a Unity Graphic component (e.g. Image, Text, RawImage)", MessageType.Error);
24 }
25 else
26 {
27 if (script.UIGraphic is Image && script.GetType() != typeof(CUIImage))
28 {
29 EditorGUILayout.HelpBox("Although CUI components are generalized. It is recommended that for Image, use CUIImage", MessageType.Warning);
30 }
31 else if (script.UIGraphic is Text && script.GetType() != typeof(CUIText))
32 {
33 EditorGUILayout.HelpBox("Although CUI components are generalized. It is recommended that for Text, use CUIText", MessageType.Warning);
34 }
35
36 EditorGUILayout.HelpBox("Now that CUI is ready, change the control points of the top and bottom bezier curves to curve/morph the UI. Improve resolution when the UI seems to look poorly when curved/morphed should help.", MessageType.Info);
37
38 }
39
40 DrawDefaultInspector();
41
42 // draw the editor that shows the position ratio of all control points from the two bezier curves
43 isCurveGpFold = EditorGUILayout.Foldout(isCurveGpFold, "Curves Position Ratios");
44 if (isCurveGpFold)
45 {
46 EditorGUI.indentLevel++;
47 EditorGUILayout.LabelField("Top Curve");
48 EditorGUI.indentLevel++;
49 Vector3[] controlPoints = script.RefCurvesControlRatioPoints[1].array;
50
51 EditorGUI.BeginChangeCheck();
52 for (int p = 0; p < controlPoints.Length; p++)
53 {
54 reuse_Vector3s[p] = EditorGUILayout.Vector3Field(string.Format("Control Points {0}", p + 1), controlPoints[p]);
55 }
56
57 if (EditorGUI.EndChangeCheck())
58 {
59 Undo.RecordObject(script, "Change Ratio Points");
60 EditorUtility.SetDirty(script);
61
62 System.Array.Copy(reuse_Vector3s, script.RefCurvesControlRatioPoints[1].array, controlPoints.Length);
64 }
65 EditorGUI.indentLevel--;
66 EditorGUILayout.LabelField("Bottom Curve");
67 EditorGUI.indentLevel++;
68 controlPoints = script.RefCurvesControlRatioPoints[0].array;
69
70 EditorGUI.BeginChangeCheck();
71 for (int p = 0; p < controlPoints.Length; p++)
72 {
73 reuse_Vector3s[p] = EditorGUILayout.Vector3Field(string.Format("Control Points {0}", p + 1), controlPoints[p]);
74 }
75
76 if (EditorGUI.EndChangeCheck())
77 {
78 Undo.RecordObject(script, "Change Ratio Points");
79 EditorUtility.SetDirty(script);
80
81 System.Array.Copy(reuse_Vector3s, controlPoints, controlPoints.Length);
83 }
84 EditorGUI.indentLevel--;
85 EditorGUI.indentLevel--;
86 }
87
88 EditorGUILayout.Space();
89 if (GUILayout.Button("Fit Bezier curves to rect transform"))
90 {
91 Undo.RecordObject(script, "Fit to Rect Transform");
92 Undo.RecordObject(script.RefCurves[0], "Fit to Rect Transform");
93 Undo.RecordObject(script.RefCurves[1], "Fit to Rect Transform");
94 EditorUtility.SetDirty(script);
95
96 script.FixTextToRectTrans();
97
98 script.Refresh();
99 }
100
101 EditorGUILayout.Space();
102
103 // disable group to prevent allowing the reference be used when there is no reference CUI
104 EditorGUI.BeginDisabledGroup(script.RefCUIGraphic == null);
105
106 if (GUILayout.Button("Reference CUI component for curves"))
107 {
108 Undo.RecordObject(script, "Reference CUI");
109 Undo.RecordObject(script.RefCurves[0], "Reference CUI");
110 Undo.RecordObject(script.RefCurves[1], "Reference CUI");
111 EditorUtility.SetDirty(script);
112
113 script.ReferenceCUIForBCurves();
114
115 script.Refresh();
116 }
117
118 EditorGUILayout.HelpBox("Auto set the curves' control points by referencing another CUI. You need to set Ref CUI Graphic (e.g. CUIImage) first.", MessageType.Info);
119
120 EditorGUI.EndDisabledGroup();
121 }
122
123 protected virtual void OnSceneGUI()
124 {
125 // for CUITextEditor, allow using scene UI to change the control points of the bezier curves
126
127 CUIGraphic script = (CUIGraphic)this.target;
128
129 script.ReportSet();
130
131 for (int c = 0; c < script.RefCurves.Length; c++)
132 {
133
134 CUIBezierCurve curve = script.RefCurves[c];
135
136 if (curve.ControlPoints != null)
137 {
138
139 Vector3[] controlPoints = curve.ControlPoints;
140
141 Transform handleTransform = curve.transform;
142 Quaternion handleRotation = curve.transform.rotation;
143
144 for (int p = 0; p < CUIBezierCurve.CubicBezierCurvePtNum; p++)
145 {
146 EditorGUI.BeginChangeCheck();
147 Handles.Label(handleTransform.TransformPoint(controlPoints[p]), string.Format("Control Point {0}", p + 1));
148 Vector3 newPt = Handles.DoPositionHandle(handleTransform.TransformPoint(controlPoints[p]), handleRotation);
149 if (EditorGUI.EndChangeCheck())
150 {
151
152 Undo.RecordObject(curve, "Move Point");
153 Undo.RecordObject(script, "Move Point");
154 EditorUtility.SetDirty(curve);
155 controlPoints[p] = handleTransform.InverseTransformPoint(newPt);
156
157 }
158 }
159
160 Handles.color = Color.gray;
161 Handles.DrawLine(handleTransform.TransformPoint(controlPoints[0]), handleTransform.TransformPoint(controlPoints[1]));
162 Handles.DrawLine(handleTransform.TransformPoint(controlPoints[1]), handleTransform.TransformPoint(controlPoints[2]));
163 Handles.DrawLine(handleTransform.TransformPoint(controlPoints[2]), handleTransform.TransformPoint(controlPoints[3]));
164
165 int sampleSize = 10;
166
167 Handles.color = Color.white;
168 for (int s = 0; s < sampleSize; s++)
169 {
170 Handles.DrawLine(handleTransform.TransformPoint(curve.GetPoint((float)s / sampleSize)), handleTransform.TransformPoint(curve.GetPoint((float)(s + 1) / sampleSize)));
171 }
172
173 curve.EDITOR_ControlPoints = controlPoints;
174 }
175 }
176
177
178 if (script.RefCurves != null)
179 {
180 Handles.DrawLine(script.RefCurves[0].transform.TransformPoint(script.RefCurves[0].ControlPoints[0]), script.RefCurves[1].transform.TransformPoint(script.RefCurves[1].ControlPoints[0]));
181 Handles.DrawLine(script.RefCurves[0].transform.TransformPoint(script.RefCurves[0].ControlPoints[3]), script.RefCurves[1].transform.TransformPoint(script.RefCurves[1].ControlPoints[3]));
182 }
183
184 script.Refresh();
185 }
186 }
187}
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
Assume to be a cubic bezier curve at the moment.
Vector3 GetPoint(float _time)
call this to get a sample
virtual void ReportSet()
Check, prepare and set everything needed.
Definition: CUIGraphic.cs:354
Vector3_Array2D[] RefCurvesControlRatioPoints
Definition: CUIGraphic.cs:110
Credit Erdener Gonenc - @PixelEnvision.