2022-10-03 08:37:50 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-11-11 12:39:58 +08:00
|
|
|
|
namespace nadena.dev.modular_avatar.core
|
2022-10-03 08:37:50 +08:00
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2022-10-20 10:42:33 +08:00
|
|
|
|
public class AvatarObjectReference
|
2022-10-03 08:37:50 +08:00
|
|
|
|
{
|
2023-08-05 14:47:03 +08:00
|
|
|
|
private long ReferencesLockedAtFrame = long.MinValue;
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-08-05 14:47:03 +08:00
|
|
|
|
bool cacheValid = _cacheValid || ReferencesLockedAtFrame == Time.frameCount;
|
|
|
|
|
|
|
|
|
|
if (cacheValid && _cachedPath == referencePath && _cachedReference != null) return _cachedReference;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
|
|
|
|
_cacheValid = true;
|
|
|
|
|
_cachedPath = referencePath;
|
|
|
|
|
|
2022-10-05 08:54:03 +08:00
|
|
|
|
if (string.IsNullOrEmpty(referencePath))
|
2022-10-03 08:37:50 +08:00
|
|
|
|
{
|
|
|
|
|
_cachedReference = null;
|
|
|
|
|
return _cachedReference;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged -= InvalidateCache;
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged += InvalidateCache;
|
|
|
|
|
|
2023-10-15 17:44:53 +08:00
|
|
|
|
var avatarTransform = RuntimeUtil.FindAvatarTransformInParents(container.transform);
|
|
|
|
|
if (avatarTransform == null) return (_cachedReference = null);
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-10-03 11:34:39 +08:00
|
|
|
|
if (referencePath == AVATAR_ROOT)
|
|
|
|
|
{
|
2023-10-15 17:44:53 +08:00
|
|
|
|
_cachedReference = avatarTransform.gameObject;
|
2022-10-03 11:34:39 +08:00
|
|
|
|
return _cachedReference;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-15 17:44:53 +08:00
|
|
|
|
_cachedReference = avatarTransform.Find(referencePath)?.gameObject;
|
2023-09-15 20:00:08 +08:00
|
|
|
|
if (_cachedReference == null) return null;
|
|
|
|
|
|
|
|
|
|
// https://github.com/bdunderscore/modular-avatar/issues/308
|
|
|
|
|
// Some avatars have multiple "Armature" objects in order to confuse VRChat into changing the avatar eye
|
|
|
|
|
// position. We need to be smarter than VRChat and find the "true" armature in this case.
|
|
|
|
|
var targetName = _cachedReference.name;
|
|
|
|
|
var parent = _cachedReference.transform.parent;
|
|
|
|
|
if (targetName == "Armature" && parent != null && _cachedReference.transform.childCount == 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (Transform possibleTarget in parent)
|
|
|
|
|
{
|
|
|
|
|
if (possibleTarget.gameObject.name == targetName && possibleTarget.childCount > 0)
|
|
|
|
|
{
|
|
|
|
|
_cachedReference = possibleTarget.gameObject;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _cachedReference;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-23 13:22:25 +08:00
|
|
|
|
public void Set(GameObject target)
|
|
|
|
|
{
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
referencePath = "";
|
|
|
|
|
}
|
2023-10-15 17:44:53 +08:00
|
|
|
|
else if (RuntimeUtil.IsAvatarRoot(target.transform))
|
2023-07-23 13:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
referencePath = AVATAR_ROOT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
referencePath = RuntimeUtil.AvatarRootPath(target);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-05 14:47:03 +08:00
|
|
|
|
_cachedReference = target;
|
|
|
|
|
_cacheValid = true;
|
2023-07-23 13:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 08:37:50 +08:00
|
|
|
|
private void InvalidateCache()
|
|
|
|
|
{
|
|
|
|
|
RuntimeUtil.OnHierarchyChanged -= InvalidateCache;
|
|
|
|
|
_cacheValid = false;
|
|
|
|
|
}
|
2022-10-20 10:42:33 +08:00
|
|
|
|
|
|
|
|
|
protected bool Equals(AvatarObjectReference other)
|
|
|
|
|
{
|
|
|
|
|
return referencePath == other.referencePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(null, obj)) return false;
|
|
|
|
|
if (ReferenceEquals(this, obj)) return true;
|
|
|
|
|
if (obj.GetType() != this.GetType()) return false;
|
|
|
|
|
return Equals((AvatarObjectReference) obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return (referencePath != null ? referencePath.GetHashCode() : 0);
|
|
|
|
|
}
|
2022-10-03 08:37:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|