ca2d-unity-toolkit/Assets/Main/Scripts/UnityObjectWarp.cs
cardidi 0315862527 Add some basic features of this toolkit.
feat: Logg was almost done.
fix: Some impossible call was fixed in UnityObjectWarp.cs
feat: Boxing<T> was force to unbox by explicit cast operator.
2024-05-10 00:55:42 +08:00

40 lines
1.2 KiB
C#

using System;
using UnityEngine;
namespace Ca2d.Toolkit
{
/// <summary>
/// An utility which allows you to reference an <see cref="UnityEngine.Object"/> via it's interface.
/// </summary>
/// <typeparam name="T">The type of class which you are willing to reference as.</typeparam>
[Serializable]
public struct UnityObjectWarp<T> where T : class
{
[SerializeField] private UnityEngine.Object m_referencedObject;
/// <summary>
/// Is this warp reference to a valid target?
/// </summary>
public bool Is => m_referencedObject is T;
/// <summary>
/// Trying to get the actual object of this reference.
/// </summary>
public T As => m_referencedObject as T;
/// <summary>
/// Is this warp reference to a valid target? If this is valid, give the casting result.
/// </summary>
public bool IsAs(out T val)
{
if (m_referencedObject is T referenced)
{
val = referenced;
return true;
}
val = null;
return false;
}
}
}