Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created August 11, 2024 07:27
Show Gist options
  • Save kyubuns/5ab72fb8db500dedc8581d5236e7994a to your computer and use it in GitHub Desktop.
Save kyubuns/5ab72fb8db500dedc8581d5236e7994a to your computer and use it in GitHub Desktop.
// https://discussions.unity.com/t/multiplayer-playmode-does-not-update-assembly-properly/922976
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
internal class ForceSyncVirtualPlayer : AssetPostprocessor
{
private const string SessionStateKey = "ForceSyncVirtualPlayer.CachedTotalCount";
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
var totalCount = importedAssets.Length + deletedAssets.Length + movedAssets.Length + movedFromAssetPaths.Length;
if (!didDomainReload)
{
SessionState.SetInt(SessionStateKey, totalCount);
return;
}
var cachedTotalCount = SessionState.GetInt(SessionStateKey, 0);
if (cachedTotalCount == 0) return;
_targetType ??= Type.GetType("Unity.Multiplayer.Playmode.VirtualProjects.Editor.AssetDatabaseCallbacks, Unity.Multiplayer.Playmode.VirtualProjects.Editor");
if (_targetType == null)
{
Debug.LogError("Target type not found.");
return;
}
_eventField ??= _targetType.GetField("OnPostprocessAllAssetsCallback", BindingFlags.Static | BindingFlags.NonPublic);
if (_eventField == null)
{
Debug.LogError("Event field not found.");
return;
}
_eventDelegate = (Delegate) _eventField.GetValue(null);
if (_eventDelegate == null)
{
Debug.LogError("Event delegate not found.");
return;
}
_eventDelegate.DynamicInvoke(true, cachedTotalCount);
SessionState.EraseInt(SessionStateKey);
}
private static Type _targetType;
private static FieldInfo _eventField;
private static Delegate _eventDelegate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment