Skip to content

Instantly share code, notes, and snippets.

@RimuruDev
Forked from olegmrzv/PlayerLoopCleaner.cs
Created May 20, 2024 08:59
Show Gist options
  • Save RimuruDev/d8a5f78def483d5bc28f9019b162251c to your computer and use it in GitHub Desktop.
Save RimuruDev/d8a5f78def483d5bc28f9019b162251c to your computer and use it in GitHub Desktop.
PlayerLoop Disable Unity Modules
using System;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;
public static class PlayerLoopCleaner
{
private static readonly Type[] typesToRemove = new Type[] {
typeof(EarlyUpdate.Physics2DEarlyUpdate),
// Physics 2D
#if UNITY_2022_2_OR_NEWER
typeof(EarlyUpdate.Physics2DEarlyUpdate),
#endif
typeof(FixedUpdate.Physics2DFixedUpdate),
typeof(PreUpdate.Physics2DUpdate),
typeof(PreLateUpdate.Physics2DLateUpdate),
// Physics 3D
#if UNITY_2022_2_OR_NEWER
typeof(EarlyUpdate.PhysicsResetInterpolatedTransformPosition),
#endif
typeof(FixedUpdate.PhysicsFixedUpdate),
typeof(PreUpdate.PhysicsUpdate),
typeof(PreLateUpdate.PhysicsLateUpdate),
// Director
typeof(Initialization.DirectorSampleTime),
typeof(FixedUpdate.DirectorFixedSampleTime),
typeof(FixedUpdate.DirectorFixedUpdate),
typeof(FixedUpdate.DirectorFixedUpdatePostPhysics),
typeof(Update.DirectorUpdate),
typeof(PreLateUpdate.DirectorUpdateAnimationBegin),
typeof(PreLateUpdate.DirectorUpdateAnimationEnd),
typeof(PreLateUpdate.DirectorDeferredEvaluate),
typeof(PostLateUpdate.DirectorLateUpdate),
typeof(PostLateUpdate.DirectorRenderImage),
// AI
typeof(PreUpdate.AIUpdate),
typeof(PreLateUpdate.AIUpdatePostScript),
// Wind
typeof(PreUpdate.WindUpdate),
// Cloth
typeof(PostLateUpdate.PhysicsSkinnedClothBeginUpdate),
typeof(PostLateUpdate.PhysicsSkinnedClothFinishUpdate),
// Old network system
typeof(PreLateUpdate.UpdateNetworkManager),
// Other
typeof(PreUpdate.SendMouseEvents),
typeof(Initialization.UpdateCameraMotionVectors),
typeof(Initialization.XREarlyUpdate),
typeof(Initialization.AsyncUploadTimeSlicedUpdate),
typeof(EarlyUpdate.PresentBeforeUpdate),
typeof(EarlyUpdate.PollPlayerConnection),
typeof(EarlyUpdate.GpuTimestamp),
typeof(EarlyUpdate.AnalyticsCoreStatsUpdate),
typeof(EarlyUpdate.UnityWebRequestUpdate),
typeof(EarlyUpdate.ExecuteMainThreadJobs),
typeof(EarlyUpdate.ExecuteMainThreadJobs),
typeof(EarlyUpdate.ProcessMouseInWindow),
typeof(EarlyUpdate.ClearIntermediateRenderers),
typeof(EarlyUpdate.ClearLines),
typeof(EarlyUpdate.ResetFrameStatsAfterPresent),
typeof(EarlyUpdate.UpdateAsyncInstantiate),
typeof(EarlyUpdate.UpdateAsyncReadbackManager),
typeof(EarlyUpdate.UpdateStreamingManager),
typeof(EarlyUpdate.UpdateTextureStreamingManager),
typeof(EarlyUpdate.UpdatePreloading),
typeof(EarlyUpdate.UpdateContentLoading),
typeof(EarlyUpdate.RendererNotifyInvisible),
typeof(EarlyUpdate.PlayerCleanupCachedData),
typeof(EarlyUpdate.UpdateMainGameViewRect),
typeof(EarlyUpdate.UpdateCanvasRectTransform),
typeof(EarlyUpdate.XRUpdate),
typeof(EarlyUpdate.UpdateInputManager),
typeof(EarlyUpdate.ProcessRemoteInput),
typeof(EarlyUpdate.ScriptRunDelayedStartupFrame),
typeof(EarlyUpdate.DispatchEventQueueEvents),
typeof(EarlyUpdate.SpriteAtlasManagerUpdate),
typeof(EarlyUpdate.PerformanceAnalyticsUpdate),
typeof(PreUpdate.CheckTexFieldInput),
typeof(PreUpdate.IMGUISendQueuedEvents),
typeof(PreUpdate.NewInputUpdate),
typeof(PreUpdate.UpdateVideo),
typeof(Update.ScriptRunDelayedTasks),
typeof(Update.ScriptRunDelayedDynamicFrameRate),
typeof(PreLateUpdate.LegacyAnimationUpdate),
typeof(PreLateUpdate.ConstraintManagerUpdate),
typeof(PreLateUpdate.ParticleSystemBeginUpdateAll),
typeof(PreLateUpdate.ScriptRunBehaviourLateUpdate),
typeof(PreLateUpdate.EndGraphicsJobsAfterScriptUpdate),
typeof(PostLateUpdate.UpdateAllRenderers),
typeof(PostLateUpdate.EndGraphicsJobsAfterScriptLateUpdate),
typeof(PostLateUpdate.ScriptRunDelayedDynamicFrameRate),
typeof(PostLateUpdate.UpdateRectTransform),
typeof(PostLateUpdate.PlayerUpdateCanvases),
typeof(PostLateUpdate.UpdateAudio),
typeof(PostLateUpdate.VFXUpdate),
typeof(PostLateUpdate.ParticleSystemEndUpdateAll),
typeof(PostLateUpdate.UpdateCustomRenderTextures),
typeof(PostLateUpdate.XRPostLateUpdate),
typeof(PostLateUpdate.ResetInputAxis),
typeof(PostLateUpdate.ShaderHandleErrors),
typeof(PostLateUpdate.GUIClearEvents),
typeof(PostLateUpdate.TriggerEndOfFrameCallbacks),
typeof(PostLateUpdate.InputEndFrame),
typeof(PostLateUpdate.UpdateCaptureScreenshot),
typeof(PostLateUpdate.BatchModeUpdate),
typeof(PostLateUpdate.UpdateVideo),
typeof(PostLateUpdate.UpdateVideoTextures),
typeof(PostLateUpdate.SortingGroupsUpdate),
typeof(PostLateUpdate.UpdateAllSkinnedMeshes),
typeof(PostLateUpdate.XRPreEndFrame),
typeof(PostLateUpdate.MemoryFrameMaintenance),
typeof(PostLateUpdate.UpdateResolution),
typeof(PostLateUpdate.EnlightenRuntimeUpdate),
typeof(PostLateUpdate.PlayerEmitCanvasGeometry),
typeof(PostLateUpdate.PlayerSendFrameStarted),
typeof(PostLateUpdate.PlayerSendFrameComplete),
typeof(PostLateUpdate.PlayerSendFramePostPresent),
//typeof(PostLateUpdate.FinishFrameRendering),
//typeof(TimeUpdate.WaitForLastPresentationAndUpdateTime),
};
private static readonly string[] typesToRemoveAsString = new string[] {
// Cloth internal types
"UnityEngine.PlayerLoop.FixedUpdate+PhysicsClothFixedUpdate",
"UnityEngine.PlayerLoop.PreUpdate+PhysicsClothUpdate"
};
[RuntimeInitializeOnLoadMethod]
private static void RemoveUnusedPackageFromPlayerLoop()
{
PlayerLoopSystem playerLoop = PlayerLoop.GetCurrentPlayerLoop();
//DisplayRecursivly(0, playerLoop);
foreach(Type type in typesToRemove)
{
TryRemoveTypeFrom(ref playerLoop, type);
}
foreach (string type in typesToRemoveAsString) {
TryRemoveTypeFrom(ref playerLoop, type);
}
//DisplayRecursivly(10, playerLoop);
PlayerLoop.SetPlayerLoop(playerLoop);
}
private static void DisplayRecursivly(int level, PlayerLoopSystem system)
{
Debug.Log(level + " " + system.type);
if (system.subSystemList != null)
{
Debug.Log(level + " subSystemList.Length: " + system.subSystemList.Length);
foreach (PlayerLoopSystem sys in system.subSystemList)
{
DisplayRecursivly(level + 1, sys);
}
}
}
/// <summary>
/// From https://github.com/Baste-RainGames/PlayerLoopInterface/blob/a4c15199ecb5f88b8a5009d0c391b967d768068c/Runtime/PlayerLoopInterface.cs#L136
/// </summary>
/// <param name="currentSystem"></param>
/// <param name="type"></param>
/// <returns></returns>
private static bool TryRemoveTypeFrom(ref PlayerLoopSystem currentSystem, Type type)
{
PlayerLoopSystem[] subSystems = currentSystem.subSystemList;
if (subSystems == null)
{
return false;
}
for (int i = 0; i < subSystems.Length; i++)
{
if (subSystems[i].type == type)
{
PlayerLoopSystem[] newSubSystems = new PlayerLoopSystem[subSystems.Length - 1];
Array.Copy(subSystems, newSubSystems, i);
Array.Copy(subSystems, i + 1, newSubSystems, i, subSystems.Length - i - 1);
currentSystem.subSystemList = newSubSystems;
return true;
}
if (TryRemoveTypeFrom(ref subSystems[i], type))
{
return true;
}
}
return false;
}
private static bool TryRemoveTypeFrom(ref PlayerLoopSystem currentSystem, string type)
{
PlayerLoopSystem[] subSystems = currentSystem.subSystemList;
if (subSystems == null) {
return false;
}
for (int i = 0; i < subSystems.Length; i++) {
if (subSystems[i].type.ToString() == type) {
PlayerLoopSystem[] newSubSystems = new PlayerLoopSystem[subSystems.Length - 1];
Array.Copy(subSystems, newSubSystems, i);
Array.Copy(subSystems, i + 1, newSubSystems, i, subSystems.Length - i - 1);
currentSystem.subSystemList = newSubSystems;
return true;
}
if (TryRemoveTypeFrom(ref subSystems[i], type)) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment