Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsProgram/7069b7a6c40537921475 to your computer and use it in GitHub Desktop.
Save EsProgram/7069b7a6c40537921475 to your computer and use it in GitHub Desktop.
Explicit script exection order for Unity scripts
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());
}
}
}
}
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