- Home /
Question by
L96416D18C552SCOPE · Oct 12, 2021 at 09:40 AM ·
programmingloopcustom
Unity custom player loop
I'm trying to register a custom loop just for fun. But the registered custom player loop is also called when I do something(save, edit something, etc...) on Editor while the game isn't running
Here's the code
internal class CustomPlayerLoop : MonoBehaviour {
[RuntimeInitializeOnLoadMethod]
internal static void OnApplicationAwake()
{
var defaultSystems = PlayerLoop.GetCurrentPlayerLoop();
var customUpdate = CustomUpdate.CreateNewLoopSystem(typeof(CustomPlayerLoopLab));
if (AddNewLoopSystem(ref defaultSystems, customUpdate, typeof(Update.ScriptRunBehaviourUpdate), LoopSystemAddMode.Begin))
{
PlayerLoop.SetPlayerLoop(defaultSystems);
}
}
internal void Update()
{
Debug.Log("Default Update Loop!");
}
internal static bool AddNewLoopSystem(ref PlayerLoopSystem playerLoopSystem, PlayerLoopSystem newSystem, Type targetLoopSystemType, LoopSystemAddMode mode)
{
List<PlayerLoopSystem> newPlayerLoopSystem = playerLoopSystem.subSystemList.ToList<PlayerLoopSystem>();
List<PlayerLoopSystem> childSubLoopSystem = null;
int insertPosition = -1;
int childSubLoopSystemPosition = -1;
for (int i = 0; i < newPlayerLoopSystem.Count; i++)
{
List<PlayerLoopSystem> subLoopSystem = newPlayerLoopSystem[i].subSystemList.ToList<PlayerLoopSystem>();
for (int j = 0; j < subLoopSystem.Count; j++)
{
if (subLoopSystem[j].type == targetLoopSystemType)
{
childSubLoopSystemPosition = i;
childSubLoopSystem = subLoopSystem;
if (mode == LoopSystemAddMode.Begin)
insertPosition = j;
else if (mode == LoopSystemAddMode.End)
insertPosition = j + 1;
else return false;
}
}
}
if (childSubLoopSystem == null || insertPosition < 0 || childSubLoopSystemPosition < 0)
return false;
childSubLoopSystem.Insert(insertPosition, newSystem);
PlayerLoopSystem loopSys = newPlayerLoopSystem[childSubLoopSystemPosition];
loopSys.subSystemList = childSubLoopSystem.ToArray();
newPlayerLoopSystem[childSubLoopSystemPosition] = loopSys;
playerLoopSystem.subSystemList = newPlayerLoopSystem.ToArray();
return true;
}
internal enum LoopSystemAddMode
{
Begin,
End
}
internal struct CustomUpdate
{
internal static PlayerLoopSystem CreateNewLoopSystem(Type ownerType)
{
return new PlayerLoopSystem()
{
type = ownerType,
updateDelegate = () => Debug.Log("Custom Update Loop!")
};
}
}
Comment
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Coroutine For Loop Will Not End 1 Answer
Difference between while loop and dowhileloop? 1 Answer
Unity or Custom Engine? 1 Answer
Programming custom objects instead of using GameObjects 0 Answers