|
| 1 | +// source https://gist.github.com/GieziJo/f80bcb24c4caa68ebfb204148ccd4b18 |
| 2 | +// =============================== |
| 3 | +// AUTHOR : J. Giezendanner |
| 4 | +// CREATE DATE : 12.03.2020 |
| 5 | +// MODIFIED DATE : |
| 6 | +// PURPOSE : Adds helper functions to the RectTransform to align the rect to the anchors and vise-versa |
| 7 | +// SPECIAL NOTES : Sources for certain informations: |
| 8 | +// Display anchors gizmos: |
| 9 | +// https://forum.unity.com/threads/recttransform-custom-editor-ontop-of-unity-recttransform-custom-editor.455925/ |
| 10 | +// Draw default inspector: |
| 11 | +// https://forum.unity.com/threads/extending-instead-of-replacing-built-in-inspectors.407612/ |
| 12 | +// =============================== |
| 13 | +// Change History: |
| 14 | +//================================== |
| 15 | + |
| 16 | +#if UNITY_EDITOR |
| 17 | +using System; |
| 18 | +using System.Reflection; |
| 19 | +using UnityEditor.SceneManagement; |
| 20 | +using UnityEngine; |
| 21 | + |
| 22 | + |
| 23 | +namespace UnityEditor |
| 24 | +{ |
| 25 | + [CustomEditor(typeof(RectTransform), true)] |
| 26 | + [CanEditMultipleObjects] |
| 27 | + public class CustomRectTransformInspector : Editor |
| 28 | + { |
| 29 | + //Unity's built-in editor |
| 30 | + Editor defaultEditor = null; |
| 31 | + RectTransform rectTransform; |
| 32 | + |
| 33 | + bool rect2Anchors_foldout = false; |
| 34 | + bool anchors2Rect_foldout = false; |
| 35 | + bool rect2Anchors__previousState = false; |
| 36 | + bool anchors2Rect_previousState = false; |
| 37 | + |
| 38 | + private bool playerPrefsChecked = false; |
| 39 | + |
| 40 | + void OnEnable() |
| 41 | + { |
| 42 | + //When this inspector is created, also create the built-in inspector |
| 43 | + defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.RectTransformEditor, UnityEditor")); |
| 44 | + rectTransform = target as RectTransform; |
| 45 | + } |
| 46 | + |
| 47 | + void OnDisable() |
| 48 | + { |
| 49 | + //When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage. |
| 50 | + //Also, make sure to call any required methods like OnDisable |
| 51 | + |
| 52 | + if (defaultEditor != null) |
| 53 | + { |
| 54 | + MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable", |
| 55 | + BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); |
| 56 | + if (disableMethod != null) |
| 57 | + disableMethod.Invoke(defaultEditor, null); |
| 58 | + DestroyImmediate(defaultEditor); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + void checkPlayerPrefs() |
| 63 | + { |
| 64 | + rect2Anchors_foldout = PlayerPrefs.GetInt("giezi_tools_rect2Anchors_foldout_bool", 0) != 0; |
| 65 | + anchors2Rect_foldout = PlayerPrefs.GetInt("giezi_tools_anchors2Rect_foldout_bool", 0) != 0; |
| 66 | + |
| 67 | + rect2Anchors__previousState = rect2Anchors_foldout; |
| 68 | + anchors2Rect_previousState = anchors2Rect_foldout; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public override void OnInspectorGUI() |
| 73 | + { |
| 74 | + if (!playerPrefsChecked) |
| 75 | + { |
| 76 | + checkPlayerPrefs(); |
| 77 | + playerPrefsChecked = true; |
| 78 | + } |
| 79 | + |
| 80 | + defaultEditor.OnInspectorGUI(); |
| 81 | + |
| 82 | + |
| 83 | + if (rectTransform.parent != null) |
| 84 | + { |
| 85 | + var centerButtonStyle = new GUIStyle(GUI.skin.button); |
| 86 | + centerButtonStyle.fontStyle = FontStyle.Bold; |
| 87 | + |
| 88 | + EditorGUILayout.Space(); |
| 89 | + EditorGUILayout.LabelField("Helper Functions", EditorStyles.boldLabel); |
| 90 | + |
| 91 | + rect2Anchors_foldout = EditorGUILayout.Foldout(rect2Anchors_foldout, "Set Rect to Anchors"); |
| 92 | + |
| 93 | + if (rect2Anchors_foldout) |
| 94 | + { |
| 95 | + GUILayout.BeginHorizontal(); |
| 96 | + GUILayout.BeginVertical(); |
| 97 | + if (GUILayout.Button("Top Left")) |
| 98 | + setRectValue("topLeft"); |
| 99 | + if (GUILayout.Button("Left")) |
| 100 | + setRectValue("left"); |
| 101 | + if (GUILayout.Button("Bottom Left")) |
| 102 | + setRectValue("bottomLeft"); |
| 103 | + GUILayout.EndVertical(); |
| 104 | + GUILayout.BeginVertical(); |
| 105 | + if (GUILayout.Button("Top")) |
| 106 | + setRectValue("top"); |
| 107 | + if (GUILayout.Button("All", centerButtonStyle)) |
| 108 | + setRectValue("all"); |
| 109 | + if (GUILayout.Button("Bottom")) |
| 110 | + setRectValue("bottom"); |
| 111 | + GUILayout.EndVertical(); |
| 112 | + GUILayout.BeginVertical(); |
| 113 | + if (GUILayout.Button("Top Right")) |
| 114 | + setRectValue("topRight"); |
| 115 | + if (GUILayout.Button("Right")) |
| 116 | + setRectValue("right"); |
| 117 | + if (GUILayout.Button("Bottom Right")) |
| 118 | + setRectValue("bottomRight"); |
| 119 | + GUILayout.EndVertical(); |
| 120 | + GUILayout.EndHorizontal(); |
| 121 | + } |
| 122 | + |
| 123 | + anchors2Rect_foldout = EditorGUILayout.Foldout(anchors2Rect_foldout, "Set Anchors to Rect"); |
| 124 | + |
| 125 | + if (anchors2Rect_foldout) |
| 126 | + { |
| 127 | + GUILayout.BeginHorizontal(); |
| 128 | + GUILayout.BeginVertical(); |
| 129 | + if (GUILayout.Button("Top Left")) |
| 130 | + setAnchorsToRect("topLeft"); |
| 131 | + if (GUILayout.Button("Left")) |
| 132 | + setAnchorsToRect("left"); |
| 133 | + if (GUILayout.Button("Bottom Left")) |
| 134 | + setAnchorsToRect("bottomLeft"); |
| 135 | + GUILayout.EndVertical(); |
| 136 | + GUILayout.BeginVertical(); |
| 137 | + if (GUILayout.Button("Top")) |
| 138 | + setAnchorsToRect("top"); |
| 139 | + if (GUILayout.Button("All", centerButtonStyle)) |
| 140 | + setAnchorsToRect("all"); |
| 141 | + if (GUILayout.Button("Bottom")) |
| 142 | + setAnchorsToRect("bottom"); |
| 143 | + GUILayout.EndVertical(); |
| 144 | + GUILayout.BeginVertical(); |
| 145 | + if (GUILayout.Button("Top Right")) |
| 146 | + setAnchorsToRect("topRight"); |
| 147 | + if (GUILayout.Button("Right")) |
| 148 | + setAnchorsToRect("right"); |
| 149 | + if (GUILayout.Button("Bottom Right")) |
| 150 | + setAnchorsToRect("bottomRight"); |
| 151 | + GUILayout.EndVertical(); |
| 152 | + GUILayout.EndHorizontal(); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + if (rect2Anchors_foldout != rect2Anchors__previousState) |
| 157 | + { |
| 158 | + rect2Anchors__previousState = rect2Anchors_foldout; |
| 159 | + PlayerPrefs.SetInt("giezi_tools_rect2Anchors_foldout_bool", rect2Anchors_foldout ? 1 : 0); |
| 160 | + } |
| 161 | + |
| 162 | + if (anchors2Rect_foldout != anchors2Rect_previousState) |
| 163 | + { |
| 164 | + anchors2Rect_previousState = anchors2Rect_foldout; |
| 165 | + PlayerPrefs.SetInt("giezi_tools_anchors2Rect_foldout_bool", anchors2Rect_foldout ? 1 : 0); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + private void OnSceneGUI() |
| 172 | + { |
| 173 | + MethodInfo onSceneGUI_Method = defaultEditor.GetType() |
| 174 | + .GetMethod("OnSceneGUI", BindingFlags.NonPublic | BindingFlags.Instance); |
| 175 | + onSceneGUI_Method.Invoke(defaultEditor, null); |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + private void setAnchorsToRect(string field) |
| 180 | + { |
| 181 | + Vector2 anchorMax = new Vector2(); |
| 182 | + Vector2 anchorMin = new Vector2(); |
| 183 | + var parent = rectTransform.parent; |
| 184 | + anchorMin.x = rectTransform.offsetMin.x / parent.GetComponent<RectTransform>().rect.size.x; |
| 185 | + anchorMin.y = rectTransform.offsetMin.y / parent.GetComponent<RectTransform>().rect.size.y; |
| 186 | + anchorMax.x = rectTransform.offsetMax.x / parent.GetComponent<RectTransform>().rect.size.x; |
| 187 | + anchorMax.y = rectTransform.offsetMax.y / parent.GetComponent<RectTransform>().rect.size.y; |
| 188 | + |
| 189 | + |
| 190 | + switch (field) |
| 191 | + { |
| 192 | + case "topLeft": |
| 193 | + anchorMax.x = 0; |
| 194 | + rectTransform.anchorMax += anchorMax; |
| 195 | + rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, 0); |
| 196 | + |
| 197 | + anchorMin.y = 0; |
| 198 | + rectTransform.anchorMin += anchorMin; |
| 199 | + rectTransform.offsetMin = new Vector2(0, rectTransform.offsetMin.y); |
| 200 | + break; |
| 201 | + case "top": |
| 202 | + anchorMax.x = 0; |
| 203 | + rectTransform.anchorMax += anchorMax; |
| 204 | + rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, 0); |
| 205 | + break; |
| 206 | + case "topRight": |
| 207 | + rectTransform.anchorMax += anchorMax; |
| 208 | + rectTransform.offsetMax = Vector2.zero; |
| 209 | + break; |
| 210 | + case "bottomLeft": |
| 211 | + rectTransform.anchorMin += anchorMin; |
| 212 | + rectTransform.offsetMin = Vector2.zero; |
| 213 | + break; |
| 214 | + case "bottom": |
| 215 | + anchorMin.x = 0; |
| 216 | + rectTransform.anchorMin += anchorMin; |
| 217 | + rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, 0); |
| 218 | + break; |
| 219 | + case "bottomRight": |
| 220 | + anchorMin.x = 0; |
| 221 | + rectTransform.anchorMin += anchorMin; |
| 222 | + rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, 0); |
| 223 | + anchorMax.y = 0; |
| 224 | + rectTransform.anchorMax += anchorMax; |
| 225 | + rectTransform.offsetMax = new Vector2(0, rectTransform.offsetMax.y); |
| 226 | + break; |
| 227 | + case "left": |
| 228 | + anchorMin.y = 0; |
| 229 | + rectTransform.anchorMin += anchorMin; |
| 230 | + rectTransform.offsetMin = new Vector2(0, rectTransform.offsetMin.y); |
| 231 | + break; |
| 232 | + case "right": |
| 233 | + anchorMax.y = 0; |
| 234 | + rectTransform.anchorMax += anchorMax; |
| 235 | + rectTransform.offsetMax = new Vector2(0, rectTransform.offsetMax.y); |
| 236 | + break; |
| 237 | + case "all": |
| 238 | + rectTransform.anchorMax += anchorMax; |
| 239 | + rectTransform.anchorMin += anchorMin; |
| 240 | + rectTransform.offsetMin = Vector2.zero; |
| 241 | + rectTransform.offsetMax = Vector2.zero; |
| 242 | + break; |
| 243 | + } |
| 244 | + |
| 245 | + handleChange(); |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + private void setRectValue(string field) |
| 250 | + { |
| 251 | + switch (field) |
| 252 | + { |
| 253 | + case "topLeft": |
| 254 | + rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, 0); |
| 255 | + rectTransform.offsetMin = new Vector2(0, rectTransform.offsetMin.y); |
| 256 | + break; |
| 257 | + case "top": |
| 258 | + rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, 0); |
| 259 | + break; |
| 260 | + case "topRight": |
| 261 | + rectTransform.offsetMax = Vector2.zero; |
| 262 | + break; |
| 263 | + case "bottomLeft": |
| 264 | + rectTransform.offsetMin = Vector2.zero; |
| 265 | + break; |
| 266 | + case "bottom": |
| 267 | + rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, 0); |
| 268 | + break; |
| 269 | + case "bottomRight": |
| 270 | + rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, 0); |
| 271 | + rectTransform.offsetMax = new Vector2(0, rectTransform.offsetMax.y); |
| 272 | + break; |
| 273 | + case "left": |
| 274 | + rectTransform.offsetMin = new Vector2(0, rectTransform.offsetMin.y); |
| 275 | + break; |
| 276 | + case "right": |
| 277 | + rectTransform.offsetMax = new Vector2(0, rectTransform.offsetMax.y); |
| 278 | + break; |
| 279 | + case "all": |
| 280 | + rectTransform.offsetMin = new Vector2(0, 0); |
| 281 | + rectTransform.offsetMax = new Vector2(0, 0); |
| 282 | + break; |
| 283 | + } |
| 284 | + |
| 285 | + handleChange(); |
| 286 | + } |
| 287 | + |
| 288 | + private void handleChange() |
| 289 | + { |
| 290 | + EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); |
| 291 | + } |
| 292 | + } |
| 293 | +} |
| 294 | +#endif |
0 commit comments