2022-10-03 08:37:50 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace net.fushizen.modular_avatar.core
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2022-10-03 09:16:58 +08:00
|
|
|
|
public struct AvatarObjectReference
|
2022-10-03 08:37:50 +08:00
|
|
|
|
{
|
2022-10-03 11:34:39 +08:00
|
|
|
|
public static string AVATAR_ROOT = "$$$AVATAR_ROOT$$$";
|
2022-10-03 09:16:58 +08:00
|
|
|
|
public string referencePath;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-10-03 09:16:58 +08:00
|
|
|
|
private bool _cacheValid;
|
|
|
|
|
private string _cachedPath;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
private GameObject _cachedReference;
|
|
|
|
|
|
|
|
|
|
public GameObject Get(Component container)
|
|
|
|
|
{
|
2022-10-03 11:34:39 +08:00
|
|
|
|
if (_cacheValid && _cachedPath == referencePath) return _cachedReference;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
|
|
|
|
_cacheValid = true;
|
|
|
|
|
_cachedPath = referencePath;
|
|
|
|
|
|
2022-10-03 11:34:39 +08:00
|
|
|
|
if (referencePath == "")
|
2022-10-03 08:37:50 +08:00
|
|
|
|
{
|
|
|
|
|
_cachedReference = null;
|
|
|
|
|
return _cachedReference;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged -= InvalidateCache;
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged += InvalidateCache;
|
|
|
|
|
|
|
|
|
|
var avatar = RuntimeUtil.FindAvatarInParents(container.transform);
|
|
|
|
|
if (avatar == null) return (_cachedReference = null);
|
|
|
|
|
|
2022-10-03 11:34:39 +08:00
|
|
|
|
if (referencePath == AVATAR_ROOT)
|
|
|
|
|
{
|
|
|
|
|
_cachedReference = avatar.gameObject;
|
|
|
|
|
return _cachedReference;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 08:37:50 +08:00
|
|
|
|
return (_cachedReference = avatar.transform.Find(referencePath)?.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InvalidateCache()
|
|
|
|
|
{
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged -= InvalidateCache;
|
|
|
|
|
_cacheValid = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|