Last active
June 29, 2023 11:20
-
-
Save Skybladev2/d0d029716b1651cc1947 to your computer and use it in GitHub Desktop.
Explicit script exection order for Unity scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEditor; | |
[InitializeOnLoad] | |
public class ExecutionOrderManager : Editor | |
{ | |
static ExecutionOrderManager() | |
{ | |
foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) | |
{ | |
Type type = monoScript.GetClass(); | |
if (type == null) | |
{ | |
continue; | |
} | |
object[] attributes = type.GetCustomAttributes(typeof(ScriptExecutionOrderAttribute), true); | |
if (attributes.Length == 0) | |
{ | |
continue; | |
} | |
ScriptExecutionOrderAttribute attribute = (ScriptExecutionOrderAttribute)attributes[0]; | |
if (MonoImporter.GetExecutionOrder(monoScript) != attribute.GetOrder()) | |
{ | |
MonoImporter.SetExecutionOrder(monoScript, attribute.GetOrder()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
[AttributeUsage(AttributeTargets.Class)] | |
public class ScriptExecutionOrderAttribute : Attribute | |
{ | |
private int order = 0; | |
public ScriptExecutionOrderAttribute(int order) | |
{ | |
this.order = order; | |
} | |
public int GetOrder() | |
{ | |
return order; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment