DMeville

Untitled

May 2nd, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. [InitializeOnLoad]
  7. public static class BoltExecutionOrderManager {
  8.     static BoltExecutionOrderManager() {
  9.         //return;
  10.         Dictionary<string, MonoScript> monoScripts = new Dictionary<string, MonoScript>();
  11.  
  12.         foreach(MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) {
  13.             try {
  14.                 switch(monoScript.name) {
  15.                     case "BoltPoll":
  16.                         SetExecutionOrder(monoScript, -10000);
  17.                         break;
  18.  
  19.                     case "BoltSend":
  20.                         SetExecutionOrder(monoScript, +10000);
  21.                         break;
  22.  
  23.                     case "BoltEntity":
  24.                         SetExecutionOrder(monoScript, -2500);
  25.                         break;
  26.  
  27.                     default:
  28.                         monoScripts.Add(monoScript.name, monoScript);
  29.                         break;
  30.                 }
  31.             } catch { }
  32.         }
  33.  
  34.         foreach(var asm in AppDomain.CurrentDomain.GetAssemblies()) {
  35.             bool exc = false;
  36.             try {
  37.                 asm.GetTypes();
  38.             } catch(System.Reflection.ReflectionTypeLoadException e) {
  39.                 Debug.Log("Exception thrown on assembly: " + asm.ToString());
  40.                 Debug.LogException(e);
  41.                 exc = true;
  42.             }
  43.  
  44.             if(!exc) {
  45.                 foreach(var type in asm.GetTypes()) {
  46.                     if(monoScripts.ContainsKey(type.Name)) {
  47.                         try {
  48.                             foreach(BoltExecutionOrderAttribute attribute in type.GetCustomAttributes(typeof(BoltExecutionOrderAttribute), false)) {
  49.                                 SetExecutionOrder(monoScripts[type.Name], attribute.executionOrder);
  50.                             }
  51.                         } catch(Exception exn) {
  52.                             Debug.LogException(exn);
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     static void SetExecutionOrder(MonoScript script, Int32 executionOrder) {
  61.         if(MonoImporter.GetExecutionOrder(script) != executionOrder) {
  62.             MonoImporter.SetExecutionOrder(script, executionOrder);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment