Skip to content

Instantly share code, notes, and snippets.

@Skybladev2
Last active June 29, 2023 11:20
Show Gist options
  • Save Skybladev2/d0d029716b1651cc1947 to your computer and use it in GitHub Desktop.
Save Skybladev2/d0d029716b1651cc1947 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