|
| 1 | +// Copyright 2014 Invex Games http://invexgames.com |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +// Unless required by applicable law or agreed to in writing, software |
| 6 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 7 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +// See the License for the specific language governing permissions and |
| 9 | +// limitations under the License. |
| 10 | + |
| 11 | +// Credit to the 'EventTriggerEditor' script at http://www.cratesmith.com/archives/221, which helped immensely |
| 12 | + |
| 13 | +// May the gods bestow the mightiest of luck upon any who find |
| 14 | +// themselves staring into the abyss that is this wretched code |
| 15 | +// (I'll be cleaning this up and improving on it in the near future) |
| 16 | + |
| 17 | +using UnityEngine; |
| 18 | +using UnityEditor; |
| 19 | +using System.Collections; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.Reflection; |
| 22 | + |
| 23 | +[CustomEditor(typeof(AnimTrigger))] |
| 24 | +public class AnimTriggerEditor : Editor |
| 25 | +{ |
| 26 | + private bool showDoc = false; |
| 27 | + private bool showAdv = false; |
| 28 | + public int variableType; |
| 29 | + public string valueType; |
| 30 | + |
| 31 | +// public enum AnimationType{Linear, EaseOutCubed, EaseOutQuint, EaseOutSept, EaseInCubed, EaseInQuint, EaseInSept, EaseInOutCubed, EaseInOutQuint, EaseInOutSept, SoftEaseOutCubed, SoftEaseOutQuint, SoftEaseOutSept}; |
| 32 | + public int animationType; |
| 33 | + |
| 34 | + string[] exclusions; |
| 35 | + string[] replacements; |
| 36 | + string[] replacementNames; |
| 37 | + string[] inclusions; |
| 38 | + string[] inclusionNames; |
| 39 | + |
| 40 | + private string[] FindComponentStrings(GameObject targetGameObject) |
| 41 | + { |
| 42 | + List<string> componentStrings = new List<string>(); |
| 43 | + Component[] components = targetGameObject.GetComponents<Component>(); |
| 44 | + bool isExcluded; |
| 45 | + foreach(Component component in components) |
| 46 | + { |
| 47 | + isExcluded = false; |
| 48 | + |
| 49 | + for(int i = 0; i < exclusions.Length; i++) |
| 50 | + { |
| 51 | + if (component.GetType().ToString() == exclusions[i]) |
| 52 | + { |
| 53 | + isExcluded = true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + for(int i = 0; i < replacements.Length; i++) |
| 58 | + { |
| 59 | + if (component.GetType().ToString() == replacements[i]) |
| 60 | + { |
| 61 | + isExcluded = true; |
| 62 | + componentStrings.Add(replacementNames[i]); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (!isExcluded) |
| 67 | + componentStrings.Add(component.GetType().ToString()); |
| 68 | + } |
| 69 | + |
| 70 | + componentStrings.Add("--NONE--"); |
| 71 | + return componentStrings.ToArray(); |
| 72 | + } |
| 73 | + |
| 74 | + private string[] FindVariableStrings(GameObject targetGameObject, string targetComponent) |
| 75 | + { |
| 76 | + List<string> variableStrings = new List<string>(); |
| 77 | + |
| 78 | + if (targetGameObject) |
| 79 | + { |
| 80 | + if (targetGameObject.GetComponent(targetComponent)) |
| 81 | + { |
| 82 | + Component realComponent = targetGameObject.GetComponent(targetComponent); |
| 83 | + System.Type behaviorType = realComponent.GetType (); |
| 84 | + |
| 85 | + if (variableType == 0) |
| 86 | + { |
| 87 | + FieldInfo[] fields = behaviorType.GetFields(); |
| 88 | + foreach(FieldInfo field in fields) |
| 89 | + { |
| 90 | + string fieldType = field.FieldType.ToString(); |
| 91 | + foreach (string inclusion in inclusions) |
| 92 | + { |
| 93 | + if (fieldType == inclusion) |
| 94 | + { |
| 95 | + variableStrings.Add(field.Name); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + else if (variableType == 1) |
| 101 | + { |
| 102 | + PropertyInfo[] properties = behaviorType.GetProperties(); |
| 103 | + foreach(PropertyInfo property in properties) |
| 104 | + { |
| 105 | + string propertyType = property.PropertyType.ToString(); |
| 106 | + |
| 107 | + foreach (string inclusion in inclusions) |
| 108 | + { |
| 109 | + if (propertyType == inclusion && property.CanWrite) |
| 110 | + { |
| 111 | + variableStrings.Add(property.Name); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + variableStrings.Add("--NONE--"); |
| 120 | + return variableStrings.ToArray(); |
| 121 | + } |
| 122 | + |
| 123 | + public override void OnInspectorGUI() |
| 124 | + { |
| 125 | + OnInspectorGUI_Settings(); |
| 126 | + OnInspectorGUI_Advanced (); |
| 127 | + } |
| 128 | + |
| 129 | + public void OnInspectorGUI_Settings() |
| 130 | + { |
| 131 | + AnimTrigger trigger = (AnimTrigger)target; |
| 132 | + GameObject targetObject = trigger.target; |
| 133 | + |
| 134 | + exclusions = new string[] {"UnityEngine.MeshFilter", "UnityEngine.BoxCollider"}; |
| 135 | + replacements = new string[] {"UnityEngine.Transform", "UnityEngine.RectTransform", "UnityEngine.UI.Image", "UnityEngine.CharacterController", "UnityEngine.Animation", "UnityEngine.MeshRenderer"}; |
| 136 | + replacementNames = new string[] {"Transform", "RectTransform", "Image", "CharacterController", "Animation", "Renderer"}; |
| 137 | + |
| 138 | + inclusions = new string[] {"System.Single", "System.Int32", "UnityEngine.Vector2", "UnityEngine.Vector3", "UnityEngine.Rect", "UnityEngine.Color", "UnityEngine.Material"}; |
| 139 | + |
| 140 | + inclusionNames = new string[] {"Float", "Int", "Vector2", "Vector3", "Rect", "Color", "Material"}; |
| 141 | + |
| 142 | + trigger.target = (GameObject)EditorGUILayout.ObjectField("Target Object", (Object)trigger.target, typeof(GameObject)); |
| 143 | + |
| 144 | + string[] compStrings = targetObject!=null ? FindComponentStrings(targetObject):new string[] {"No object selected"}; |
| 145 | + |
| 146 | + SerializedObject m_object = new SerializedObject(target); |
| 147 | + SerializedProperty prop; |
| 148 | + |
| 149 | + m_object.Update(); |
| 150 | + |
| 151 | + trigger.targetComponent = StringPopup("Component", compStrings, trigger.targetComponent); |
| 152 | + |
| 153 | + prop = m_object.FindProperty ("variableType"); |
| 154 | + |
| 155 | + EditorGUILayout.PropertyField (prop, true); |
| 156 | + variableType = prop.enumValueIndex; |
| 157 | + |
| 158 | + string[] strings = targetObject!=null ? FindVariableStrings(targetObject, trigger.targetComponent):new string[] {"No object selected"}; |
| 159 | + |
| 160 | + trigger.targetVariable = StringPopup("Variable", strings, trigger.targetVariable); |
| 161 | + |
| 162 | + EditorGUILayout.Space (); |
| 163 | + |
| 164 | + if (variableType == 0) |
| 165 | + { |
| 166 | + if (trigger.target != null && trigger.targetComponent != null) |
| 167 | + { |
| 168 | + if (trigger.target.GetComponent(trigger.targetComponent)) |
| 169 | + { |
| 170 | + if (trigger.target.GetComponent (trigger.targetComponent).GetType ().GetField (trigger.targetVariable) != null) |
| 171 | + { |
| 172 | + FieldInfo realField = trigger.target.GetComponent (trigger.targetComponent).GetType ().GetField (trigger.targetVariable); |
| 173 | + |
| 174 | + for (int i = 0; i < inclusions.Length; i++) |
| 175 | + { |
| 176 | +// Debug.Log(realField.FieldType.ToString ()); |
| 177 | + if (realField.FieldType.ToString() == inclusions[i]) |
| 178 | + { |
| 179 | + EditorGUILayout.LabelField ("Variable is of type " + inclusionNames[i]); |
| 180 | + trigger.valueType = inclusions[i]; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + else |
| 185 | + trigger.valueType = ""; |
| 186 | + } |
| 187 | + else |
| 188 | + trigger.valueType = ""; |
| 189 | + } |
| 190 | + else |
| 191 | + trigger.valueType = ""; |
| 192 | + } |
| 193 | + else if (variableType == 1) |
| 194 | + { |
| 195 | + if (trigger.target != null && trigger.targetComponent != null) |
| 196 | + { |
| 197 | + if (trigger.target.GetComponent(trigger.targetComponent)) |
| 198 | + { |
| 199 | + if (trigger.target.GetComponent (trigger.targetComponent).GetType ().GetProperty (trigger.targetVariable) != null) |
| 200 | + { |
| 201 | + PropertyInfo realField = trigger.target.GetComponent (trigger.targetComponent).GetType ().GetProperty (trigger.targetVariable); |
| 202 | + |
| 203 | + for (int i = 0; i < inclusions.Length; i++) |
| 204 | + { |
| 205 | + if (realField.PropertyType.ToString() == inclusions[i]) |
| 206 | + { |
| 207 | + EditorGUILayout.LabelField ("Variable is of type " + inclusionNames[i]); |
| 208 | + trigger.valueType = inclusions[i]; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + else |
| 213 | + trigger.valueType = ""; |
| 214 | + } |
| 215 | + else |
| 216 | + trigger.valueType = ""; |
| 217 | + } |
| 218 | + else |
| 219 | + trigger.valueType = ""; |
| 220 | + } |
| 221 | + |
| 222 | + EditorGUILayout.Space (); |
| 223 | + |
| 224 | + valueType = trigger.valueType; |
| 225 | + |
| 226 | + if (valueType == "System.Single") |
| 227 | + { |
| 228 | + prop = m_object.FindProperty ("targetFloat"); |
| 229 | + EditorGUILayout.PropertyField (prop, true); |
| 230 | + } |
| 231 | + else if (valueType == "System.Int32") |
| 232 | + { |
| 233 | + prop = m_object.FindProperty ("targetInt"); |
| 234 | + EditorGUILayout.PropertyField (prop, true); |
| 235 | + } |
| 236 | + else if (valueType == "UnityEngine.Vector2") |
| 237 | + { |
| 238 | + prop = m_object.FindProperty ("modifyVec2X"); |
| 239 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify x value"), true); |
| 240 | + |
| 241 | + prop = m_object.FindProperty ("modifyVec2Y"); |
| 242 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify y value"), true); |
| 243 | + |
| 244 | + prop = m_object.FindProperty ("targetVec2"); |
| 245 | + EditorGUILayout.PropertyField (prop, true); |
| 246 | + } |
| 247 | + else if (valueType == "UnityEngine.Vector3") |
| 248 | + { |
| 249 | + prop = m_object.FindProperty ("modifyVec3X"); |
| 250 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify x value"), true); |
| 251 | + |
| 252 | + prop = m_object.FindProperty ("modifyVec3Y"); |
| 253 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify y value"), true); |
| 254 | + |
| 255 | + prop = m_object.FindProperty ("modifyVec3Z"); |
| 256 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify z value"), true); |
| 257 | + |
| 258 | + prop = m_object.FindProperty ("targetVec3"); |
| 259 | + EditorGUILayout.PropertyField (prop, true); |
| 260 | + } |
| 261 | + else if (valueType == "UnityEngine.Rect") |
| 262 | + { |
| 263 | + prop = m_object.FindProperty ("modifyRectWidth"); |
| 264 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify width value"), true); |
| 265 | + |
| 266 | + prop = m_object.FindProperty ("modifyRectHeight"); |
| 267 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify height value"), true); |
| 268 | + |
| 269 | + prop = m_object.FindProperty ("modifyRectX"); |
| 270 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify x value"), true); |
| 271 | + |
| 272 | + prop = m_object.FindProperty ("modifyRectY"); |
| 273 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify y value"), true); |
| 274 | + |
| 275 | + prop = m_object.FindProperty ("targetRect"); |
| 276 | + EditorGUILayout.PropertyField (prop, true); |
| 277 | + } |
| 278 | + else if (valueType == "UnityEngine.Color") |
| 279 | + { |
| 280 | + prop = m_object.FindProperty ("modifyColorR"); |
| 281 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify red value"), true); |
| 282 | + |
| 283 | + prop = m_object.FindProperty ("modifyColorG"); |
| 284 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify green value"), true); |
| 285 | + |
| 286 | + prop = m_object.FindProperty ("modifyColorB"); |
| 287 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify blue value"), true); |
| 288 | + |
| 289 | + prop = m_object.FindProperty ("modifyColorA"); |
| 290 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify alpha value"), true); |
| 291 | + |
| 292 | + prop = m_object.FindProperty ("targetColor"); |
| 293 | + EditorGUILayout.PropertyField (prop, true); |
| 294 | + } |
| 295 | + else if (valueType == "UnityEngine.Material") |
| 296 | + { |
| 297 | + prop = m_object.FindProperty ("modifyMatColorR"); |
| 298 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify red value"), true); |
| 299 | + |
| 300 | + prop = m_object.FindProperty ("modifyMatColorG"); |
| 301 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify green value"), true); |
| 302 | + |
| 303 | + prop = m_object.FindProperty ("modifyMatColorB"); |
| 304 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify blue value"), true); |
| 305 | + |
| 306 | + prop = m_object.FindProperty ("modifyMatColorA"); |
| 307 | + EditorGUILayout.PropertyField (prop, new GUIContent("Modify alpha value"), true); |
| 308 | + |
| 309 | + prop = m_object.FindProperty ("targetMatColor"); |
| 310 | + EditorGUILayout.PropertyField (prop, new GUIContent("Target Material Color"), true); |
| 311 | + } |
| 312 | + |
| 313 | + EditorGUILayout.Space (); |
| 314 | + |
| 315 | + prop = m_object.FindProperty ("animationType"); |
| 316 | + EditorGUILayout.PropertyField (prop, true); |
| 317 | + variableType = prop.enumValueIndex; |
| 318 | + |
| 319 | + prop = m_object.FindProperty ("duration"); |
| 320 | + EditorGUILayout.PropertyField (prop, true); |
| 321 | + |
| 322 | + EditorGUILayout.Space (); |
| 323 | + |
| 324 | + m_object.ApplyModifiedProperties(); |
| 325 | + } |
| 326 | + |
| 327 | + public void OnInspectorGUI_Advanced () |
| 328 | + { |
| 329 | + showAdv = EditorGUILayout.Foldout(showAdv, "Advanced"); |
| 330 | + if(showAdv) |
| 331 | + DrawDefaultInspector(); |
| 332 | + } |
| 333 | + |
| 334 | + private static string StringPopup (string label, string[] strings, string str) |
| 335 | + { |
| 336 | + int strId = System.Array.IndexOf(strings,str); |
| 337 | + |
| 338 | + if(strId==-1) |
| 339 | + strId = strings.Length-1; |
| 340 | + |
| 341 | + strId = EditorGUILayout.Popup(label, strId, strings, "popup"); |
| 342 | + |
| 343 | + if(strId==-1 || strId==strings.Length-1) |
| 344 | + return ""; |
| 345 | + |
| 346 | + return strings[strId]; |
| 347 | + } |
| 348 | +} |
0 commit comments