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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

### Added
- Exposed APIs for finding avatar roots in RuntimeUtil (#34)

### Fixed

### Changed

### Removed

### Security

## [1.1.0] - [2023-10-05]

### Added
- Added toplevel menu for manual bake avatar, even when MA is also installed (#35)
- Added support for multiple ExportsPlugin declarations (#40)
Expand Down
5 changes: 5 additions & 0 deletions Editor/nadena.dev.ndmf.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"name": "nadena.dev.modular-avatar",
"expression": "[1.7.0,99999.0.0]",
"define": "MODULAR_AVATAR"
},
{
"name": "com.vrchat.avatars",
"expression": "",
"define": "NDMF_VRCSDK3_AVATARS"
}
],
"noEngineReferences": false
Expand Down
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 @@ -72,13 +71,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 NDMF_VRCSDK3_AVATARS
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 NDMF_VRCSDK3_AVATARS
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 NDMF_VRCSDK3_AVATARS
foreach (var avatar in root.GetComponentsInChildren<VRCAvatarDescriptor>())
#else
foreach (var avatar in root.GetComponentsInChildren<Animator>())
#endif
{
if (IsAvatarRoot(avatar.transform)) yield return avatar.transform;
}
}
}
}
}
8 changes: 7 additions & 1 deletion Runtime/nadena.dev.ndmf.runtime.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "com.vrchat.avatars",
"expression": "",
"define": "NDMF_VRCSDK3_AVATARS"
}
],
"noEngineReferences": false
}
Loading