// Serialization // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
// Shared File Last Modified: 2023-08-12.
namespace Animancer.Editor
// namespace InspectorGadgets.Editor
{
/// [Editor-Only] Various serialization utilities.
public partial class Serialization
{
/// [Editor-Only] A serializable reference to a .
[Serializable]
public class PropertyReference
{
/************************************************************************************************************************/
[SerializeField] private ObjectReference[] _TargetObjects;
/// [] The .
public ObjectReference TargetObject
{
get
{
return _TargetObjects != null && _TargetObjects.Length > 0 ?
_TargetObjects[0] : null;
}
}
/// [] The .
public ObjectReference[] TargetObjects => _TargetObjects;
/************************************************************************************************************************/
[SerializeField] private ObjectReference _Context;
/// [] The .
public ObjectReference Context => _Context;
/************************************************************************************************************************/
[SerializeField] private string _PropertyPath;
/// [] The .
public string PropertyPath => _PropertyPath;
/************************************************************************************************************************/
[NonSerialized] private bool _IsInitialized;
/// Indicates whether the has been accessed.
public bool IsInitialized => _IsInitialized;
/************************************************************************************************************************/
[NonSerialized] private SerializedProperty _Property;
/// [] The referenced .
public SerializedProperty Property
{
get
{
Initialize();
return _Property;
}
}
/************************************************************************************************************************/
///
/// Creates a new which wraps the specified `property`.
///
public PropertyReference(SerializedProperty property)
{
_TargetObjects = ObjectReference.Convert(property.serializedObject.targetObjects);
_Context = property.serializedObject.context;
_PropertyPath = property.propertyPath;
// Don't set the _Property. If it gets accessed we want to create out own instance.
}
/************************************************************************************************************************/
///
/// Creates a new which wraps the specified `property`.
///
public static implicit operator PropertyReference(SerializedProperty property)
=> new(property);
///
/// Returns the target .
///
public static implicit operator SerializedProperty(PropertyReference reference)
=> reference.Property;
/************************************************************************************************************************/
private void Initialize()
{
if (_IsInitialized)
{
if (!TargetsExist)
Dispose();
return;
}
_IsInitialized = true;
if (string.IsNullOrEmpty(_PropertyPath) ||
!TargetsExist)
return;
var targetObjects = ObjectReference.Convert(_TargetObjects);
var serializedObject = new SerializedObject(targetObjects, _Context);
_Property = serializedObject.FindProperty(_PropertyPath);
}
/************************************************************************************************************************/
/// Do the specified `property` and `targetObjects` match the targets of this reference?
public bool IsTarget(SerializedProperty property, Object[] targetObjects)
{
if (_Property == null ||
_Property.propertyPath != property.propertyPath ||
_TargetObjects == null ||
_TargetObjects.Length != targetObjects.Length)
return false;
for (int i = 0; i < _TargetObjects.Length; i++)
{
if (_TargetObjects[i] != targetObjects[i])
return false;
}
return true;
}
/************************************************************************************************************************/
/// Is there is at least one target and none of them are null?
private bool TargetsExist
{
get
{
if (_TargetObjects == null ||
_TargetObjects.Length == 0)
return false;
for (int i = 0; i < _TargetObjects.Length; i++)
{
if (_TargetObjects[i].Object == null)
return false;
}
return true;
}
}
/************************************************************************************************************************/
///
/// Calls if the has been initialized.
///
public void Update()
{
if (_Property == null)
return;
if (!TargetsExist)
{
Dispose();
return;
}
_Property.serializedObject.Update();
}
///
/// Calls if the has been initialized.
///
public void ApplyModifiedProperties()
{
if (_Property == null)
return;
if (!TargetsExist)
{
Dispose();
return;
}
_Property.serializedObject.ApplyModifiedProperties();
}
///
/// Calls if the has been initialized.
///
public void Dispose()
{
if (_Property != null)
{
_Property.serializedObject.Dispose();
_Property = null;
}
}
/************************************************************************************************************************/
/// Gets the height needed to draw the target property.
public float GetPropertyHeight()
{
if (_Property == null)
return 0;
return EditorGUI.GetPropertyHeight(_Property, _Property.isExpanded);
}
/************************************************************************************************************************/
/// Draws the target object within the specified `area`.
public void DoTargetGUI(Rect area)
{
area.height = EditorGUIUtility.singleLineHeight;
Initialize();
if (_Property == null)
{
GUI.Label(area, "Missing " + this);
return;
}
var targets = _Property.serializedObject.targetObjects;
using (new EditorGUI.DisabledScope(true))
{
var showMixedValue = EditorGUI.showMixedValue;
EditorGUI.showMixedValue = targets.Length > 1;
var target = targets.Length > 0 ? targets[0] : null;
EditorGUI.ObjectField(area, target, typeof(Object), true);
EditorGUI.showMixedValue = showMixedValue;
}
}
/************************************************************************************************************************/
/// Draws the target property within the specified `area`.
public void DoPropertyGUI(Rect area)
{
Initialize();
if (_Property == null)
return;
_Property.serializedObject.Update();
GUI.BeginGroup(area);
area.x = area.y = 0;
EditorGUI.PropertyField(area, _Property, _Property.isExpanded);
GUI.EndGroup();
_Property.serializedObject.ApplyModifiedProperties();
}
/************************************************************************************************************************/
}
/************************************************************************************************************************/
/// Returns true if the `reference` and are not null.
public static bool IsValid(this PropertyReference reference) => reference?.Property != null;
/************************************************************************************************************************/
}
}
#endif