Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 KB | None | 0 0
  1. using System.Reflection;
  2. using Unity.Entities;
  3. using UnityEngine;
  4. using UnityEngine.LowLevel;
  5. using UnityEngine.PlayerLoop;
  6.  
  7. public class WorldBootStrap : ICustomBootstrap
  8. {
  9.  
  10.     static MethodInfo insertManagerIntoSubsystemListMethod = typeof(ScriptBehaviourUpdateOrder).GetMethod("InsertManagerIntoSubsystemList", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  11.  
  12.     public bool Initialize(string defaultWorldName)
  13.     {
  14.         Debug.Log("Executing World Bootstrap");
  15.         var world = new World("Custom World");
  16.         World.DefaultGameObjectInjectionWorld = world;
  17.         var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
  18.         DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
  19.         UpdatePlayerLoop(world);
  20.         return true;
  21.     }
  22.  
  23.     //public struct Settings
  24.     //{
  25.     //    public float DefaultFixedTimeStep;
  26.     //}
  27.  
  28.     public static void UpdatePlayerLoop(World world)
  29.     {
  30.         var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
  31.         if (ScriptBehaviourUpdateOrder.CurrentPlayerLoop.subSystemList != null)
  32.             playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
  33.         if (world != null)
  34.         {
  35.             for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
  36.             {
  37.                 int subsystemListLength = playerLoop.subSystemList[i].subSystemList.Length;
  38.                 if (playerLoop.subSystemList[i].type == typeof(Update))
  39.                 {
  40.                     var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
  41.                     for (var j = 0; j < subsystemListLength; ++j)
  42.                         newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
  43.  
  44.                     var mgr = world.GetOrCreateSystem<SimulationSystemGroup>();
  45.                     //string json = File.ReadAllText(Application.dataPath + "/settings.json");
  46.                     //Settings settings = JsonUtility.FromJson<Settings>(json);
  47.                     var temp = world.GetOrCreateSystem<FixedRateManagerSystem>();
  48.                     temp.EnableFixedRateWithCatchUp(mgr, (1f / 48f));
  49.                     Time.fixedDeltaTime = (1f / 48f);
  50.                     var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
  51.                     genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
  52.                     playerLoop.subSystemList[i].subSystemList = newSubsystemList;
  53.                 }
  54.                 else if (playerLoop.subSystemList[i].type == typeof(PreLateUpdate))
  55.                 {
  56.                     var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
  57.                     for (var j = 0; j < subsystemListLength; ++j)
  58.                         newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
  59.                     var mgr = world.GetOrCreateSystem<PresentationSystemGroup>();
  60.                     var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
  61.                     genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
  62.                     playerLoop.subSystemList[i].subSystemList = newSubsystemList;
  63.                 }
  64.                 else if (playerLoop.subSystemList[i].type == typeof(Initialization))
  65.                 {
  66.                     var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
  67.                     for (var j = 0; j < subsystemListLength; ++j)
  68.                         newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
  69.                     var mgr = world.GetOrCreateSystem<InitializationSystemGroup>();
  70.                     var genericMethod = insertManagerIntoSubsystemListMethod.MakeGenericMethod(mgr.GetType());
  71.                     genericMethod.Invoke(null, new object[] { newSubsystemList, subsystemListLength + 0, mgr });
  72.                     playerLoop.subSystemList[i].subSystemList = newSubsystemList;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         ScriptBehaviourUpdateOrder.SetPlayerLoop(playerLoop);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement