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

chore: apply on play for newly activated avatars #263

Merged
merged 2 commits into from
Jul 15, 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
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- `#263`

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- Support newly activated avatars in play mode for apply on play `#263`

### Deprecated

Expand Down
25 changes: 23 additions & 2 deletions Internal/ApplyOnPlay/Editor/ApplyOnPlayCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@
using UnityEngine.SceneManagement;
using VRC.SDK3.Avatars.Components;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;

namespace Anatawa12.ApplyOnPlay
{
[InitializeOnLoad]
internal class ApplyOnPlayCaller : IProcessSceneWithReport
{
public int callbackOrder => 0;

public const string SkipApplyingIfInactiveName = "com.anatawa12.apply-on-play.skip-if-inactive";

static ApplyOnPlayCaller()
{
ApplyOnPlayActivator.processAvatar = activator =>
{
var component = activator.gameObject.GetComponent<VRCAvatarDescriptor>();
Object.DestroyImmediate(activator);
if (component)
ProcessAvatar(component.gameObject, ApplyReason.EnteringPlayMode,
ApplyOnPlayCallbackRegistry.GetCallbacks());
};
}

public static bool SkipApplyingIfInactive
{
get => EditorPrefs.GetBool(SkipApplyingIfInactiveName, true);
Expand All @@ -40,8 +54,15 @@ public void OnProcessScene(Scene scene, BuildReport report)

foreach (var vrcAvatarDescriptor in components)
{
if (skipIfInactive && !vrcAvatarDescriptor.gameObject.activeInHierarchy) continue;
ProcessAvatar(vrcAvatarDescriptor.gameObject, ApplyReason.EnteringPlayMode, callbacks);
if (skipIfInactive && !vrcAvatarDescriptor.gameObject.activeInHierarchy)
{
var activator = vrcAvatarDescriptor.gameObject.AddComponent<ApplyOnPlayActivator>();
activator.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
else
{
ProcessAvatar(vrcAvatarDescriptor.gameObject, ApplyReason.EnteringPlayMode, callbacks);
}
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "com.anatawa12.avatar-optimizer.internal.apply-on-play.editor",
"references": [
"GUID:b1529a8c1c7946769969aca7e2ce5034",
"GUID:295ffbe0b63504ae3a7879cf089501ba",
"GUID:5ce33783346c3124990afbe7b0390a06",
"GUID:fc900867c0f47cd49b6e2ae4ef907300"
Expand Down
3 changes: 3 additions & 0 deletions Internal/ApplyOnPlay/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions Internal/ApplyOnPlay/Runtime/ApplyOnPlayActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine;

[assembly:InternalsVisibleTo("com.anatawa12.avatar-optimizer.internal.apply-on-play.editor")]

namespace Anatawa12.ApplyOnPlay
{
[DefaultExecutionOrder(-100000)]
[ExecuteAlways]
internal class ApplyOnPlayActivator : MonoBehaviour
{
#if UNITY_EDITOR
internal static Action<ApplyOnPlayActivator> processAvatar;

private void Awake()
{
UnityEditor.EditorApplication.delayCall += () => DestroyIfNeeded();
}

private void Start()
{
if (!this) return;
if (DestroyIfNeeded()) return;
processAvatar?.Invoke(this);
}

private bool DestroyIfNeeded()
{
var shouldDestroy = !UnityEditor.EditorApplication.isPlaying ||
UnityEditor.EditorUtility.IsPersistent(this);
if (!shouldDestroy) return false;

Debug.Log("Destroying ApplyOnPlayActivator in Start");
DestroyImmediate(this);
return true;
}
#endif
}
}
3 changes: 3 additions & 0 deletions Internal/ApplyOnPlay/Runtime/ApplyOnPlayActivator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "com.anatawa12.avatar-optimizer.internal.apply-on-play.runtime",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.