CinemachineImpulseChannelPropertyDrawer.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace Cinemachine.Editor
  4. {
  5. [CustomPropertyDrawer(typeof(CinemachineImpulseChannelPropertyAttribute))]
  6. internal sealed class CinemachineImpulseChannelPropertyDrawer : PropertyDrawer
  7. {
  8. const float hSpace = 2;
  9. GUIContent mAddLabel = new GUIContent("Edit...", "Add, remove, or rename channels");
  10. string[] mLayerList = null;
  11. void UpdateLayerList()
  12. {
  13. CinemachineImpulseChannels settings = CinemachineImpulseChannels.InstanceIfExists;
  14. int numLayers = 0;
  15. if (settings != null && settings.ImpulseChannels != null)
  16. numLayers = settings.ImpulseChannels.Length;
  17. numLayers = Mathf.Clamp(numLayers, 1, 31);
  18. if (mLayerList == null || mLayerList.Length != numLayers)
  19. mLayerList = new string[numLayers];
  20. for (int i = 0; i < numLayers; ++i)
  21. {
  22. mLayerList[i] = string.Format(
  23. "{0}: {1}", i,
  24. (settings == null || settings.ImpulseChannels.Length <= i)
  25. ? "default" : settings.ImpulseChannels[i]);
  26. }
  27. }
  28. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  29. {
  30. UpdateLayerList();
  31. float addWidth = GUI.skin.button.CalcSize(mAddLabel).x;
  32. rect.width -= addWidth + hSpace;
  33. EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
  34. EditorGUI.BeginChangeCheck();
  35. int value = EditorGUI.MaskField(rect, label, property.intValue, mLayerList);
  36. if (EditorGUI.EndChangeCheck())
  37. {
  38. property.intValue = value;
  39. property.serializedObject.ApplyModifiedProperties();
  40. }
  41. EditorGUI.showMixedValue = false;
  42. rect.x += rect.width + hSpace; rect.width = addWidth; rect.height -= 1;
  43. if (GUI.Button(rect, mAddLabel))
  44. if (CinemachineImpulseChannels.Instance != null)
  45. Selection.activeObject = CinemachineImpulseChannels.Instance;
  46. }
  47. }
  48. }