GxrScriptCustomOrderManager.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System;
  8. using UnityEditor;
  9. namespace GxrSdk.Editor
  10. {
  11. /// <summary>
  12. /// 脚本自定义次序管理类
  13. /// 该脚本在场景启动或更新时执行,以便确定自定义脚本的执行次序
  14. /// </summary>
  15. [InitializeOnLoad]
  16. public class GxrScriptCustomOrderManager
  17. {
  18. static GxrScriptCustomOrderManager()
  19. {
  20. MonoScript[] allScripts = MonoImporter.GetAllRuntimeMonoScripts();
  21. foreach (var script in allScripts)
  22. {
  23. Type type = script.GetClass();
  24. if (type != null)
  25. {
  26. Attribute[] attributes = Attribute.GetCustomAttributes(type, typeof(GxrScriptCustomOrder));
  27. foreach (var attribute in attributes)
  28. {
  29. int oldOrder = MonoImporter.GetExecutionOrder(script);
  30. int newOrder = ((GxrScriptCustomOrder)attribute).Order;
  31. if (oldOrder != newOrder)
  32. {
  33. MonoImporter.SetExecutionOrder(script, newOrder);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }