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

Slow entering play mode #262

Merged
merged 4 commits into from
Jul 14, 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 @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog].

### Fixed
- Name of failed ApplyOnPlayCallback is not included in error message `#260`
- Entering play mode can be extremely slow if you have many avatar on the scene `#262`

### Security

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

### Fixed
- Name of failed ApplyOnPlayCallback is not included in error message `#260`
- Entering play mode can be extremely slow if you have many avatar on the scene `#262`

### Security

Expand Down
29 changes: 27 additions & 2 deletions Internal/ApplyOnPlay/Editor/ApplyOnPlayCaller.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.SceneManagement;
using VRC.SDK3.Avatars.Components;
using Debug = UnityEngine.Debug;

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

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

public static bool SkipApplyingIfInactive
{
get => EditorPrefs.GetBool(SkipApplyingIfInactiveName, true);
set => EditorPrefs.SetBool(SkipApplyingIfInactiveName, value);
}

public void OnProcessScene(Scene scene, BuildReport report)
{
var components = scene.GetRootGameObjects()
.SelectMany(x => x.GetComponentsInChildren<VRCAvatarDescriptor>(true)).ToArray();

var callbacks = ApplyOnPlayCallbackRegistry.GetCallbacks();

foreach (var vrcAvatarDescriptor in components)
var skipIfInactive = SkipApplyingIfInactive;

var stopwatch = Stopwatch.StartNew();
try
{
AssetDatabase.StartAssetEditing();

foreach (var vrcAvatarDescriptor in components)
{
if (skipIfInactive && !vrcAvatarDescriptor.gameObject.activeInHierarchy) continue;
ProcessAvatar(vrcAvatarDescriptor.gameObject, ApplyReason.EnteringPlayMode, callbacks);
}
}
finally
{
ProcessAvatar(vrcAvatarDescriptor.gameObject, ApplyReason.EnteringPlayMode, callbacks);
AssetDatabase.StopAssetEditing();
Debug.Log($"time to process apply on play: {stopwatch.Elapsed}");
}
}

Expand Down
5 changes: 5 additions & 0 deletions Internal/ApplyOnPlay/Editor/ApplyOnPlayConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ private void OnEnable()
private void OnGUI()
{
CL4EE.DrawLanguagePicker();

ApplyOnPlayCaller.SkipApplyingIfInactive
= EditorGUILayout.ToggleLeft(CL4EE.Tr("skip applying if avtar is not active"),
ApplyOnPlayCaller.SkipApplyingIfInactive);

EditorGUILayout.HelpBox(CL4EE.Tr("window description"), MessageType.None);

_scroll = EditorGUILayout.BeginScrollView(_scroll);
Expand Down
3 changes: 3 additions & 0 deletions Internal/ApplyOnPlay/Editor/ModularAvatarSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Reflection;
using nadena.dev.modular_avatar.core.editor;
using nadena.dev.modular_avatar.core;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -38,6 +39,8 @@ public ModularAvatarSupport(AvatarProcessor processor)

public bool ApplyOnPlay(GameObject avatarGameObject, ApplyReason reason)
{
if (!avatarGameObject.GetComponentInChildren<AvatarTagComponent>())
return true; // skip avatar without MA components
try
{
return _processor.OnPreprocessAvatar(avatarGameObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "com.anatawa12.avatar-optimizer.internal.apply-on-play.editor",
"references": [
"GUID:295ffbe0b63504ae3a7879cf089501ba",
"GUID:5ce33783346c3124990afbe7b0390a06"
"GUID:5ce33783346c3124990afbe7b0390a06",
"GUID:fc900867c0f47cd49b6e2ae4ef907300"
],
"includePlatforms": [
"Editor"
Expand Down
3 changes: 3 additions & 0 deletions Internal/ApplyOnPlay/Localization/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ msgstr ""
"You can disable applying non-destructive avatar modification tools on play.\n"
"Checked means enabled and not checked means disabled.\n"
"This configuration is shared between projects."

msgid "skip applying if avtar is not active"
msgstr "skip applying if avtar is not active"
3 changes: 3 additions & 0 deletions Internal/ApplyOnPlay/Localization/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ msgstr ""
"非破壊アバター改変ツールのプレイ時の適用を無効にすることができます。\n"
"チェックが入っていると有効、チェックが入っていないと無効になります。\n"
"この設定はプロジェクト間で共有されます。"

msgid "skip applying if avtar is not active"
msgstr "activeでないアバターではapplyしないようにする"