chore: workaround IEditorOnly objects being deleted too soon

This commit is contained in:
bd_ 2023-04-15 19:17:30 +09:00
parent a910fa5c05
commit cc1d11a06c
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using VRC.SDKBase;
using VRC.SDKBase.Editor.BuildPipeline;
namespace nadena.dev.modular_avatar.core.editor
{
/// <summary>
/// See https://feedback.vrchat.com/sdk-bug-reports/p/ieditoronly-components-should-be-destroyed-late-in-the-build-process
/// </summary>
[InitializeOnLoad]
internal static class PreventStripTagObjects
{
static PreventStripTagObjects()
{
EditorApplication.delayCall += () =>
{
var f_callbacks = typeof(VRCBuildPipelineCallbacks).GetField("_preprocessAvatarCallbacks",
BindingFlags.Static | BindingFlags.NonPublic);
var callbacks = (List<IVRCSDKPreprocessAvatarCallback>) f_callbacks.GetValue(null);
var filteredCallbacks = callbacks.Where(c => !(c is RemoveAvatarEditorOnly)).ToList();
f_callbacks.SetValue(null, filteredCallbacks);
};
}
}
internal class ReplacementRemoveAvatarEditorOnly : IVRCSDKPreprocessAvatarCallback
{
public int callbackOrder => -1024;
public bool OnPreprocessAvatar(GameObject avatarGameObject)
{
foreach (var xform in avatarGameObject.GetComponentsInChildren<Transform>(true))
{
if (xform != null && xform.CompareTag("EditorOnly"))
{
Object.DestroyImmediate(xform.gameObject);
}
}
foreach (var editoronly in avatarGameObject.GetComponentsInChildren<IEditorOnly>(true))
{
if (editoronly == null || editoronly is AvatarTagComponent)
{
continue;
}
Object.DestroyImmediate((Component) editoronly);
}
return true;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eb8f842fbf1e4efdaf940eaf12cbbbb1
timeCreated: 1681553175