VisualEffectActivationBehaviourInspector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #if VFX_HAS_TIMELINE
  2. using System;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.Timeline;
  7. using UnityEngine.VFX;
  8. using UnityEditor;
  9. using UnityEditorInternal;
  10. using UnityEditor.VFX;
  11. using System.Collections.Generic;
  12. namespace UnityEditor.Experimental.VFX.Utility
  13. {
  14. [CustomEditor(typeof(VisualEffectActivationClip))]
  15. class VisualEffectActivationClipEditor : Editor
  16. {
  17. private SerializedProperty onClipEnterProperty;
  18. private SerializedProperty onClipExitProperty;
  19. private ReorderableList clipEnterAttributesPropertyList;
  20. private ReorderableList clipExitAttributesPropertyList;
  21. private void OnEnable()
  22. {
  23. Action<ReorderableList, SerializedProperty> fnAssetDropDown = delegate(ReorderableList list, SerializedProperty property)
  24. {
  25. var existingAttribute = new List<string>();
  26. for (int i = 0; i < property.arraySize; ++i)
  27. {
  28. existingAttribute.Add(property.GetArrayElementAtIndex(i).FindPropertyRelative("attribute.m_Name").stringValue);
  29. }
  30. var menu = new GenericMenu();
  31. foreach (var attributeName in VFXAttribute.AllIncludingVariadicReadWritable.Except(existingAttribute).OrderBy(o => o))
  32. {
  33. var attribute = VFXAttribute.Find(attributeName);
  34. menu.AddItem(new GUIContent(attribute.name), false, () =>
  35. {
  36. serializedObject.Update();
  37. property.arraySize++;
  38. var newElement = property.GetArrayElementAtIndex(property.arraySize - 1);
  39. newElement.FindPropertyRelative("attribute.m_Name").stringValue = attribute.name;
  40. newElement.FindPropertyRelative("type").intValue = (int)attribute.type;
  41. var size = VFXExpression.TypeToSize(attribute.type);
  42. var values = newElement.FindPropertyRelative("values");
  43. values.arraySize = size;
  44. var initialValues = new float[size];
  45. if (attribute.type == VFXValueType.Float)
  46. {
  47. initialValues[0] = attribute.value.Get<float>();
  48. }
  49. else if (attribute.type == VFXValueType.Float2)
  50. {
  51. var v = attribute.value.Get<Vector2>();
  52. initialValues[0] = v.x;
  53. initialValues[1] = v.y;
  54. }
  55. else if (attribute.type == VFXValueType.Float3)
  56. {
  57. var v = attribute.value.Get<Vector3>();
  58. initialValues[0] = v.x;
  59. initialValues[1] = v.y;
  60. initialValues[2] = v.z;
  61. }
  62. else if (attribute.type == VFXValueType.Float4)
  63. {
  64. var v = attribute.value.Get<Vector4>();
  65. initialValues[0] = v.x;
  66. initialValues[1] = v.y;
  67. initialValues[2] = v.z;
  68. initialValues[3] = v.w;
  69. }
  70. else if (attribute.type == VFXValueType.Int32)
  71. {
  72. initialValues[0] = attribute.value.Get<int>();
  73. }
  74. else if (attribute.type == VFXValueType.Uint32)
  75. {
  76. initialValues[0] = attribute.value.Get<uint>();
  77. }
  78. else if (attribute.type == VFXValueType.Boolean)
  79. {
  80. initialValues[0] = attribute.value.Get<bool>() ? 1.0f : 0.0f;
  81. }
  82. for (int i = 0; i < size; ++i)
  83. {
  84. values.GetArrayElementAtIndex(i).floatValue = initialValues[i];
  85. }
  86. serializedObject.ApplyModifiedProperties();
  87. });
  88. }
  89. menu.ShowAsContext();
  90. };
  91. Action<Rect, SerializedProperty, int> fnDrawElement = delegate(Rect r, SerializedProperty property, int index)
  92. {
  93. var element = property.GetArrayElementAtIndex(index);
  94. var label = element.FindPropertyRelative("attribute.m_Name").stringValue;
  95. var labelWidth = 110;//GUI.skin.label.CalcSize(new GUIContent(label)); //Should be maximized among all existing property, for now, angularVelocity is considered as maximum
  96. EditorGUI.LabelField(new Rect(r.x, r.y, labelWidth, EditorGUIUtility.singleLineHeight), label);
  97. var valueType = (VFXValueType)element.FindPropertyRelative("type").intValue;
  98. var valueSize = VFXExpression.TypeToSize(valueType);
  99. var fieldWidth = (r.width - labelWidth) / valueSize;
  100. var emptyGUIContent = new GUIContent(string.Empty);
  101. var valuesProperty = element.FindPropertyRelative("values");
  102. if (valueType == VFXValueType.Float
  103. || valueType == VFXValueType.Float2
  104. || valueType == VFXValueType.Float3
  105. || valueType == VFXValueType.Float4)
  106. {
  107. if (label.Contains("color") && valueType == VFXValueType.Float3)
  108. {
  109. var oldColor = new Color(valuesProperty.GetArrayElementAtIndex(0).floatValue,
  110. valuesProperty.GetArrayElementAtIndex(1).floatValue,
  111. valuesProperty.GetArrayElementAtIndex(2).floatValue);
  112. EditorGUI.BeginChangeCheck();
  113. var newColor = EditorGUI.ColorField(new Rect(r.x + labelWidth, r.y, fieldWidth * 3, EditorGUIUtility.singleLineHeight), oldColor);
  114. if (EditorGUI.EndChangeCheck())
  115. {
  116. valuesProperty.GetArrayElementAtIndex(0).floatValue = newColor.r;
  117. valuesProperty.GetArrayElementAtIndex(1).floatValue = newColor.g;
  118. valuesProperty.GetArrayElementAtIndex(2).floatValue = newColor.b;
  119. }
  120. }
  121. else
  122. {
  123. for (int i = 0; i < valueSize; ++i)
  124. {
  125. EditorGUI.PropertyField(new Rect(r.x + labelWidth + fieldWidth * i, r.y, fieldWidth, EditorGUIUtility.singleLineHeight), valuesProperty.GetArrayElementAtIndex(i), emptyGUIContent);
  126. }
  127. }
  128. }
  129. else if (valueType == VFXValueType.Int32
  130. || valueType == VFXValueType.Uint32
  131. || valueType == VFXValueType.Boolean)
  132. {
  133. var oldValue = valuesProperty.GetArrayElementAtIndex(0).floatValue;
  134. float newValue;
  135. var currentRect = new Rect(r.x + labelWidth, r.y, fieldWidth, EditorGUIUtility.singleLineHeight);
  136. EditorGUI.BeginChangeCheck();
  137. if (valueType == VFXValueType.Boolean)
  138. {
  139. newValue = EditorGUI.Toggle(currentRect, emptyGUIContent, oldValue != 0.0f) ? 1.0f : 0.0f;
  140. }
  141. else
  142. {
  143. newValue = (float)EditorGUI.LongField(currentRect, emptyGUIContent, (long)oldValue);
  144. newValue = newValue < 0.0f ? 0.0f : newValue;
  145. }
  146. if (EditorGUI.EndChangeCheck())
  147. {
  148. valuesProperty.GetArrayElementAtIndex(0).floatValue = newValue;
  149. serializedObject.ApplyModifiedProperties();
  150. }
  151. }
  152. };
  153. onClipEnterProperty = serializedObject.FindProperty("activationBehavior.onClipEnter.m_Name");
  154. onClipExitProperty = serializedObject.FindProperty("activationBehavior.onClipExit.m_Name");
  155. var clipEnterAttributesProperty = serializedObject.FindProperty("activationBehavior.clipEnterEventAttributes");
  156. var clipExitAttributesProperty = serializedObject.FindProperty("activationBehavior.clipExitEventAttributes");
  157. clipEnterAttributesPropertyList = new ReorderableList(serializedObject, clipEnterAttributesProperty, true, true, true, true);
  158. clipExitAttributesPropertyList = new ReorderableList(serializedObject, clipExitAttributesProperty, true, true, true, true);
  159. clipEnterAttributesPropertyList.drawHeaderCallback = (Rect r) => { EditorGUI.LabelField(r, "Enter Event Attributes"); };
  160. clipExitAttributesPropertyList.drawHeaderCallback = (Rect r) => { EditorGUI.LabelField(r, "Exit Event Attributes"); };
  161. clipEnterAttributesPropertyList.onAddDropdownCallback += (Rect buttonRect, ReorderableList list) => fnAssetDropDown(list, clipEnterAttributesProperty);
  162. clipExitAttributesPropertyList.onAddDropdownCallback += (Rect buttonRect, ReorderableList list) => fnAssetDropDown(list, clipExitAttributesProperty);
  163. clipEnterAttributesPropertyList.drawElementCallback = (Rect r, int index, bool active, bool focused) => fnDrawElement(r, clipEnterAttributesProperty, index);
  164. clipExitAttributesPropertyList.drawElementCallback = (Rect r, int index, bool active, bool focused) => fnDrawElement(r, clipExitAttributesProperty, index);
  165. }
  166. public override void OnInspectorGUI()
  167. {
  168. serializedObject.Update();
  169. if (serializedObject.isEditingMultipleObjects)
  170. return; //TODO
  171. EditorGUILayout.PropertyField(onClipEnterProperty);
  172. clipEnterAttributesPropertyList.DoLayoutList();
  173. EditorGUILayout.PropertyField(onClipExitProperty);
  174. clipExitAttributesPropertyList.DoLayoutList();
  175. serializedObject.ApplyModifiedProperties();
  176. }
  177. }
  178. }
  179. #endif