VcamTargetPropertyDrawer.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(VcamTargetPropertyAttribute))]
  7. internal sealed class VcamTargetPropertyDrawer : PropertyDrawer
  8. {
  9. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  10. {
  11. const float hSpace = 2;
  12. float iconSize = rect.height + 4;
  13. rect.width -= iconSize + hSpace;
  14. EditorGUI.PropertyField(rect, property, label);
  15. rect.x += rect.width + hSpace; rect.width = iconSize;
  16. var oldEnabled = GUI.enabled;
  17. var target = property.objectReferenceValue as Transform;
  18. if (target == null || target.GetComponent<CinemachineTargetGroup>() != null)
  19. GUI.enabled = false;
  20. if (GUI.Button(rect, EditorGUIUtility.IconContent("_Popup"), GUI.skin.label))
  21. {
  22. GenericMenu menu = new GenericMenu();
  23. menu.AddItem(new GUIContent("Convert to TargetGroup"), false, () =>
  24. {
  25. GameObject go = InspectorUtility.CreateGameObject(
  26. CinemachineMenu.GenerateUniqueObjectName(
  27. typeof(CinemachineTargetGroup), "CM TargetGroup"),
  28. typeof(CinemachineTargetGroup));
  29. var group = go.GetComponent<CinemachineTargetGroup>();
  30. Undo.RegisterCreatedObjectUndo(go, "convert to TargetGroup");
  31. group.m_RotationMode = CinemachineTargetGroup.RotationMode.GroupAverage;
  32. group.AddMember(target, 1, 1);
  33. property.objectReferenceValue = group.Transform;
  34. property.serializedObject.ApplyModifiedProperties();
  35. });
  36. menu.ShowAsContext();
  37. }
  38. GUI.enabled = oldEnabled;
  39. }
  40. }
  41. }