Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose some avatar root APIs #34

Merged
merged 9 commits into from
Oct 6, 2023
10 changes: 3 additions & 7 deletions Runtime/ApplyOnPlayGlobalActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using VRC.SDK3.Avatars.Components;

namespace nadena.dev.ndmf.runtime
{
Expand Down Expand Up @@ -76,13 +75,10 @@ private void Awake()
}
}

foreach (var root in gameObject.scene.GetRootGameObjects())
foreach (var avatar in RuntimeUtil.FindAvatarsInScene(gameObject.scene))
{
foreach (var avatar in root.GetComponentsInChildren<VRCAvatarDescriptor>())
{
// TODO: Check whether each avatar needs processing (activation components)
avatar.gameObject.GetOrAddComponent<AvatarActivator>().hideFlags = HideFlags.HideInInspector;
}
// TODO: Check whether each avatar needs processing (activation components)
avatar.gameObject.GetOrAddComponent<AvatarActivator>().hideFlags = HideFlags.HideInInspector;
}
}

Expand Down
48 changes: 43 additions & 5 deletions Runtime/RuntimeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.SceneManagement;

#if VRC_SDK_VRCSDK3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to add a version define like NDMF_VRCSDK3_AVATARS for "com.vrchat.avatars" to asmdef? (naming follows lilToon)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be fine I think.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think it's necessary - I don't think the defines auto-set by the VRCSDK are a stable API.

using VRC.SDK3.Avatars.Components;
#endif

namespace nadena.dev.ndmf.runtime
{
Expand Down Expand Up @@ -83,23 +87,57 @@ public static string AvatarRootPath(GameObject child)
return RelativePath(avatar.gameObject, child);
}

/// <summary>
/// Check whether the target component is the root of the avatar.
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public static bool IsAvatarRoot(Transform target)
{
#if VRC_SDK_VRCSDK3
return target.GetComponent<VRCAvatarDescriptor>();
#else
var an = target.GetComponent<Animator>();
if (!an) return false;
var parent = target.transform.parent;
return !(parent && parent.GetComponentInParent<Animator>());
#endif
}

/// <summary>
/// Returns the component marking the root of the avatar.
///
/// Internal for now as we need to refactor this to be less VRChat-specific.
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
internal static Transform FindAvatarInParents(Transform target)
public static Transform FindAvatarInParents(Transform target)
{
while (target != null)
{
var av = target.GetComponent<VRCAvatarDescriptor>();
if (av != null) return av.transform;
if (IsAvatarRoot(target)) return target;
target = target.parent;
}

return null;
}

/// <summary>
/// Returns the component marking the root of the avatar.
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
internal static IEnumerable<Transform> FindAvatarsInScene(Scene scene)
{
foreach (var root in scene.GetRootGameObjects())
{
#if VRC_SDK_VRCSDK3
foreach (var avatar in root.GetComponentsInChildren<VRCAvatarDescriptor>())
#else
foreach (var avatar in root.GetComponentsInChildren<Animator>())
#endif
{
if (IsAvatarRoot(avatar.transform)) yield return avatar.transform;
}
}
}
}
}
Loading