同步
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user