Firstborn/Library/PackageCache/com.unity.cinemachine@2.8.9/Editor/PropertyDrawers/VcamTargetPropertyDrawer.cs

44 lines
1.9 KiB
C#
Raw Normal View History

2023-03-28 13:24:16 -04:00
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace Cinemachine.Editor
{
[CustomPropertyDrawer(typeof(VcamTargetPropertyAttribute))]
internal sealed class VcamTargetPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
const float hSpace = 2;
float iconSize = rect.height + 4;
rect.width -= iconSize + hSpace;
EditorGUI.PropertyField(rect, property, EditorGUI.BeginProperty(rect, label, property));
rect.x += rect.width + hSpace; rect.width = iconSize;
var oldEnabled = GUI.enabled;
var target = property.objectReferenceValue as Transform;
if (target == null || target.GetComponent<CinemachineTargetGroup>() != null)
GUI.enabled = false;
if (GUI.Button(rect, EditorGUIUtility.IconContent("_Popup"), GUI.skin.label))
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Convert to TargetGroup"), false, () =>
{
GameObject go = InspectorUtility.CreateGameObject(
CinemachineMenu.GenerateUniqueObjectName(
typeof(CinemachineTargetGroup), "CM TargetGroup"),
typeof(CinemachineTargetGroup));
var group = go.GetComponent<CinemachineTargetGroup>();
Undo.RegisterCreatedObjectUndo(go, "convert to TargetGroup");
group.m_RotationMode = CinemachineTargetGroup.RotationMode.GroupAverage;
group.AddMember(target, 1, 1);
property.objectReferenceValue = group.Transform;
property.serializedObject.ApplyModifiedProperties();
});
menu.ShowAsContext();
}
GUI.enabled = oldEnabled;
}
}
}