using System.Collections.Generic;
using UnityEditor.XR.Interaction.Toolkit.Utilities;
using UnityEngine;
namespace UnityEditor.XR.Interaction.Toolkit
{
///
/// Base class for custom editors in the XR Interaction Toolkit.
///
public abstract class BaseInteractionEditor : Editor
{
///
/// The names of all fields
/// defined in the (including derived types).
///
///
protected string[] knownSerializedPropertyNames { get; set; }
bool m_InitializedKnownSerializedPropertyNames;
///
public override void OnInspectorGUI()
{
InitializeKnownSerializedPropertyNames();
serializedObject.Update();
DrawInspector();
serializedObject.ApplyModifiedProperties();
}
///
/// This method is automatically called by to
/// draw the custom inspector. Override this method to customize the
/// inspector as a whole.
///
protected abstract void DrawInspector();
///
/// This method is automatically called by to
/// initialize if necessary.
/// This is used together with to draw all unknown
/// serialized fields from derived classes.
///
///
protected virtual void InitializeKnownSerializedPropertyNames()
{
if (m_InitializedKnownSerializedPropertyNames)
return;
knownSerializedPropertyNames = GetDerivedSerializedPropertyNames().ToArray();
m_InitializedKnownSerializedPropertyNames = true;
}
///
/// Returns a list containing the names of all fields
/// defined in the (including derived types).
///
/// Returns a list of strings with property names.
protected virtual List GetDerivedSerializedPropertyNames()
{
return XRInteractionEditorUtility.GetDerivedSerializedPropertyNames(this);
}
///
/// This method is automatically called by to
/// draw the property fields of derived classes that are not explicitly defined in the .
///
///
/// This method is used to allow users to add a to a derived behavior
/// and have it automatically appear in the Inspector while still having the custom Editor
/// apply to that derived class.
///
/// When a derived class adds a ,
/// it will no longer automatically be drawn by this method. This is to allow users to customize
/// where the property is drawn in the Inspector window. The derived class
/// does not need to explicitly add the if the user is fine with
/// the default location of where it will be drawn.
///
///
protected virtual void DrawDerivedProperties()
{
DrawPropertiesExcluding(serializedObject, knownSerializedPropertyNames);
}
///
/// Draw the standard read-only Script property.
///
protected virtual void DrawScript()
{
using (new EditorGUI.DisabledScope(true))
{
if (target is MonoBehaviour behaviour)
EditorGUILayout.ObjectField(EditorGUIUtility.TrTempContent("Script"), MonoScript.FromMonoBehaviour(behaviour), typeof(MonoBehaviour), false);
else if (target is ScriptableObject scriptableObject)
EditorGUILayout.ObjectField(EditorGUIUtility.TrTempContent("Script"), MonoScript.FromScriptableObject(scriptableObject), typeof(ScriptableObject), false);
}
}
}
}