Files
ichni_Official/Packages/dev.yarnspinner.unity/Runtime/Utility/InterfaceContainer.cs
SoulliesOfficial 021e76efe7 同步
2026-06-09 11:21:59 -04:00

67 lines
1.6 KiB
C#

/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using UnityEngine;
#nullable enable
namespace Yarn.Unity
{
[System.Serializable]
public class InterfaceContainer<TContainedType> : ISerializationCallbackReceiver where TContainedType : class
{
public UnityEngine.Object? targetObject;
public TContainedType? Interface
{
get
{
return targetObject as TContainedType;
}
}
public static implicit operator TContainedType?(InterfaceContainer<TContainedType> value)
{
return value.Interface;
}
// basically if we find a component that is our interface we override the target to be that
// and otherwise we null it out, also wiping the connection in the inspector
void OnValidate()
{
if (targetObject == null)
{
return;
}
if (this.Interface != null)
{
return;
}
if (targetObject is GameObject gameObject)
{
foreach (var component in gameObject.GetComponents<Component>())
{
if (component is TContainedType)
{
targetObject = component;
return;
}
}
}
targetObject = null;
}
public void OnBeforeSerialize()
{
OnValidate();
}
public void OnAfterDeserialize()
{
return;
}
}
}