Created
October 17, 2024 16:57
-
-
Save TapGhoul/c8a8b59c495f546187517d964b953b75 to your computer and use it in GitHub Desktop.
VRC ClientSim serialization shim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using VRC.SDKBase; | |
| using VRC.Udon; | |
| using VRC.Udon.Common; | |
| [DefaultExecutionOrder(-1)] | |
| public class VRCEditorSyncFix : MonoBehaviour, IDisposable | |
| { | |
| private HashSet<GameObject> _queuedUpdates = new HashSet<GameObject>(); | |
| private bool _hasTickedThisFrame = false; | |
| #region Object Lifecycle | |
| [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
| public static void CreateInstance() | |
| { | |
| var obj = new GameObject(nameof(VRCEditorSyncFix)); | |
| var manager = obj.AddComponent<VRCEditorSyncFix>(); | |
| manager.Register(); | |
| } | |
| private void Register() | |
| { | |
| UdonBehaviour.RequestSerializationHook += OnRequestSerialization; | |
| } | |
| private void Awake() | |
| { | |
| DontDestroyOnLoad(this); | |
| } | |
| public void Dispose() | |
| { | |
| UdonBehaviour.RequestSerializationHook -= OnRequestSerialization; | |
| } | |
| private void OnRequestSerialization(UdonBehaviour src) | |
| { | |
| _queuedUpdates.Add(src.gameObject); | |
| } | |
| #endregion | |
| #region Unity Lifecycle | |
| private void FixedUpdate() | |
| { | |
| if (_hasTickedThisFrame) return; | |
| _hasTickedThisFrame = true; | |
| ProcessUpdateRequests(); | |
| } | |
| private void Update() | |
| { | |
| if (_hasTickedThisFrame) | |
| { | |
| _hasTickedThisFrame = false; | |
| } | |
| else | |
| { | |
| ProcessUpdateRequests(); | |
| } | |
| } | |
| #endregion | |
| private void ProcessUpdateRequests() | |
| { | |
| foreach (var obj in _queuedUpdates) | |
| { | |
| var behaviors = obj.GetComponents<UdonBehaviour>(); | |
| if (Networking.IsOwner(obj)) | |
| { | |
| foreach (var behavior in behaviors) | |
| { | |
| behavior.OnPreSerialization(); | |
| } | |
| foreach (var behavior in behaviors) | |
| { | |
| behavior.OnPostSerialization(new SerializationResult(true, 0)); | |
| } | |
| } | |
| else | |
| { | |
| foreach (var behavior in behaviors) | |
| { | |
| behavior.OnDeserialization(new DeserializationResult(Time.time, Time.time, false)); | |
| } | |
| } | |
| } | |
| _queuedUpdates.Clear(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment