Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Reflection;
- using Unity.Entities;
- using UnityEngine;
- using UnityEngine.LowLevel;
- using UnityEngine.PlayerLoop;
- public class WorldBootStrap : ICustomBootstrap
- {
- static MethodInfo insertManagerIntoSubsystemListMethod = typeof(ScriptBehaviourUpdateOrder).GetMethod("InsertManagerIntoSubsystemList", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
- public bool Initialize(string defaultWorldName)
- {
- Debug.Log("Executing World Bootstrap");
- var world = new World("Custom World");
- World.DefaultGameObjectInjectionWorld = world;
- var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
- DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
- UpdatePlayerLoop(world);
- return true;
- }
- //public struct Settings
- //{
- // public float DefaultFixedTimeStep;
- //}
- public static void UpdatePlayerLoop(World world)
- {
- var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
- if (ScriptBehaviourUpdateOrder.CurrentPlayerLoop.subSystemList != null)
- playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
- if (world != null)
- {
- for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
- {
- int subsystemListLength = playerLoop.subSystemList[i].subSystemList.Length;
- if (playerLoop.subSystemList[i].type == typeof(Update))
- {
- var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
- for (var j = 0; j < subsystemListLength; ++j)
- newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
- var mgr = world.GetOrCreateSystem<SimulationSystemGroup>();
- //string json = File.ReadAllText(Application.dataPath + "/settings.json");
- //Settings settings = JsonUtility.FromJson<Settings>(json);
- var temp = world.GetOrCreateSystem<FixedRateManagerSystem>();
- temp.EnableFixedRateWithCatchUp(mgr, (1f / 48f));
- Time.fixedDeltaTime = (1f / 48f);
- var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
- genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
- playerLoop.subSystemList[i].subSystemList = newSubsystemList;
- }
- else if (playerLoop.subSystemList[i].type == typeof(PreLateUpdate))
- {
- var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
- for (var j = 0; j < subsystemListLength; ++j)
- newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
- var mgr = world.GetOrCreateSystem<PresentationSystemGroup>();
- var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
- genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
- playerLoop.subSystemList[i].subSystemList = newSubsystemList;
- }
- else if (playerLoop.subSystemList[i].type == typeof(Initialization))
- {
- var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
- for (var j = 0; j < subsystemListLength; ++j)
- newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
- var mgr = world.GetOrCreateSystem<InitializationSystemGroup>();
- var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
- genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
- playerLoop.subSystemList[i].subSystemList = newSubsystemList;
- }
- }
- }
- ScriptBehaviourUpdateOrder.SetPlayerLoop(playerLoop);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement