// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
using System;
using UnityEngine;
namespace Animancer
{
/// An object which wraps a object.
/// https://kybernetik.com.au/animancer/api/Animancer/IWrapper
public interface IWrapper
{
/************************************************************************************************************************/
/// The wrapped object.
///
/// Use
/// in case the is also an .
///
object WrappedObject { get; }
/************************************************************************************************************************/
}
/// https://kybernetik.com.au/animancer/api/Animancer/AnimancerUtilities
public partial class AnimancerUtilities
{
/************************************************************************************************************************/
///
/// Returns the last
/// which is a , including the `wrapper` itself.
///
public static bool TryGetWrappedObject(
object wrapper,
out T wrapped,
bool logException = false)
where T : class
{
wrapped = default;
while (true)
{
if (wrapper is T t)
wrapped = t;
if (wrapper is IWrapper targetWrapper)
{
try
{
wrapper = targetWrapper.WrappedObject;
}
catch (Exception exception)
{
if (logException)
Debug.LogException(exception);
break;
}
}
else
{
break;
}
}
return wrapped != null;
}
/************************************************************************************************************************/
}
}