VR4Medical/ICI/Library/PackageCache/com.unity.learn.iet-framework@4bd5247958fc/Editor/Property Drawers/InstantiatePrefabCriterionDrawers.cs
2025-07-29 13:45:50 +03:00

47 lines
1.9 KiB
C#

using UnityEditor;
using UnityEngine;
using UnityObject = UnityEngine.Object;
namespace Unity.Tutorials.Core.Editor
{
class InstantiatePrefabCriterionDrawers
{
[CustomPropertyDrawer(typeof(InstantiatePrefabCriterion.FuturePrefabInstance))]
class FuturePrefabInstanceDrawer : PropertyDrawer
{
const string k_PrefabParentPropertyPath = "m_PrefabParent";
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var prefabParentProperty = property.FindPropertyRelative(k_PrefabParentPropertyPath);
return EditorGUI.GetPropertyHeight(prefabParentProperty);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position.height = GetPropertyHeight(property, label);
var prefabParentProperty = property.FindPropertyRelative(k_PrefabParentPropertyPath);
var obj = prefabParentProperty.objectReferenceValue;
EditorGUI.BeginProperty(position, GUIContent.none, prefabParentProperty);
EditorGUI.BeginChangeCheck();
var newObj = EditorGUI.ObjectField(position, obj, typeof(UnityObject), true);
if (EditorGUI.EndChangeCheck())
{
// Replace prefab instance with its prefab parent
if (newObj != null && PrefabUtility.GetPrefabInstanceStatus(newObj) != PrefabInstanceStatus.NotAPrefab)
newObj = PrefabUtilityShim.GetCorrespondingObjectFromSource(newObj);
prefabParentProperty.objectReferenceValue = newObj;
}
EditorGUI.EndProperty();
position.y += position.height + EditorGUIUtility.standardVerticalSpacing;
}
}
}
}