2022-10-02 17:37:50 -07:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace net.fushizen.modular_avatar.core
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2022-10-02 18:16:58 -07:00
|
|
|
|
public struct AvatarObjectReference
|
2022-10-02 17:37:50 -07:00
|
|
|
|
{
|
2022-10-02 20:34:39 -07:00
|
|
|
|
public static string AVATAR_ROOT = "$$$AVATAR_ROOT$$$";
|
2022-10-02 18:16:58 -07:00
|
|
|
|
public string referencePath;
|
2022-10-02 17:37:50 -07:00
|
|
|
|
|
2022-10-02 18:16:58 -07:00
|
|
|
|
private bool _cacheValid;
|
|
|
|
|
private string _cachedPath;
|
2022-10-02 17:37:50 -07:00
|
|
|
|
private GameObject _cachedReference;
|
|
|
|
|
|
|
|
|
|
public GameObject Get(Component container)
|
|
|
|
|
{
|
2022-10-02 20:34:39 -07:00
|
|
|
|
if (_cacheValid && _cachedPath == referencePath) return _cachedReference;
|
2022-10-02 17:37:50 -07:00
|
|
|
|
|
|
|
|
|
_cacheValid = true;
|
|
|
|
|
_cachedPath = referencePath;
|
|
|
|
|
|
2022-10-04 17:54:03 -07:00
|
|
|
|
if (string.IsNullOrEmpty(referencePath))
|
2022-10-02 17:37:50 -07: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-02 20:34:39 -07:00
|
|
|
|
if (referencePath == AVATAR_ROOT)
|
|
|
|
|
{
|
|
|
|
|
_cachedReference = avatar.gameObject;
|
|
|
|
|
return _cachedReference;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 17:37:50 -07:00
|
|
|
|
return (_cachedReference = avatar.transform.Find(referencePath)?.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InvalidateCache()
|
|
|
|
|
{
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged -= InvalidateCache;
|
|
|
|
|
_cacheValid = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|