Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created February 1, 2021 15:33
Show Gist options
  • Save karljj1/9e22dbae2aea5981baabb96a4c133700 to your computer and use it in GitHub Desktop.
Save karljj1/9e22dbae2aea5981baabb96a4c133700 to your computer and use it in GitHub Desktop.
Change the particle system update so that LateUpdate is taken into account
using System.Linq;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;
using static UnityEngine.PlayerLoop.PreLateUpdate;
public class ChangePlayerLoop : MonoBehaviour
{
public bool defaultLoop = true;
// Start is called before the first frame update
void Start()
{
if (defaultLoop)
{
PlayerLoop.SetPlayerLoop(PlayerLoop.GetDefaultPlayerLoop());
return;
}
var loop = PlayerLoop.GetCurrentPlayerLoop();
PlayerLoopSystem? particleUpdate = null;
// Find the particle system update
for (int i = 0; i < loop.subSystemList.Length; ++i)
{
if (loop.subSystemList[i].type == typeof(PreLateUpdate))
{
var preLateUpdate = loop.subSystemList[i];
for (int j = 0; j < preLateUpdate.subSystemList.Length; ++j)
{
if (preLateUpdate.subSystemList[j].type == typeof(ParticleSystemBeginUpdateAll))
{
// Remove particle system update
particleUpdate = preLateUpdate.subSystemList[j];
var list = preLateUpdate.subSystemList.ToList();
list.RemoveAt(j);
preLateUpdate.subSystemList = list.ToArray();
}
}
}
}
if (!particleUpdate.HasValue)
return;
// Move it so it is updated after LateUpdate
for (int i = 0; i < loop.subSystemList.Length; ++i)
{
if (loop.subSystemList[i].type == typeof(PostLateUpdate))
{
var system = loop.subSystemList[i];
var list = system.subSystemList.ToList();
list.Add(particleUpdate.Value);
system.subSystemList = list.ToArray();
}
}
PlayerLoop.SetPlayerLoop(loop);
}
}
@karljj1
Copy link
Author

karljj1 commented Feb 1, 2021

This moves the update to come after LateUpdate
A is before with an example script and B is after changing the loop
A
B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment