Hi everyone, is there a way to set script execution order trought an editor script ? I plan to make an editor that simplify user's job by adding scripts automatically... But I also need to set those scipts in a specific order, especially befor default time. I am looking for something like : Code (csharp): SetScriptOrder(MonoBehavior component, ScriptOrder order) With ScriptOrder possible values : BeforeDefaultTime, DefaultTime, AfterDefaultTime. Thanks in advance.
You can set the iniatialise order in the editor but i think Update order is determined by the order they are added to the object? You could just have an array of scripts and tick the array yourself instead of letting unity do it with the Update function. Karl
There is a built-in editor menu to edit script execution order. The influence is on all the MonoBehavior functions : Awake, Start, Update, OnGUI..., LateUpdate, ... I would like to be able to set it by script trought a function or a class attribute.
You can use MonoScript and MonoImporter for that, note that it only works in Unity4. Code (csharp): // First you get the MonoScript of your MonoBehaviour MonoScript monoScript = MonoScript.FromMonoBehaviour(yourMonoBehaviour); // Getting the current execution order of that MonoScript int currentExecutionOrder = MonoImporter.GetExecutionOrder(monoScript); // Changing the MonoScript's execution order MonoImporter.SetExecutionOrder(monoScript, x); You will of course need an Editor class for this. Cheers, Pärtel
I wrote an attribute which does this. Code (CSharp): [ScriptOrder(-100)] ScriptOrder.cs Code (CSharp): using System; using System.Collections.Generic; using UnityEngine; public class ScriptOrder:Attribute { public int order; public ScriptOrder(int order) { this.order = order; } } ScriptOrderManager.cs Code (CSharp): using System; using System.Collections.Generic; using UnityEngine; using UnityEditor; [InitializeOnLoad] public class ScriptOrderManager { static ScriptOrderManager() { foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) { if (monoScript.GetClass() != null) { foreach (var a in Attribute.GetCustomAttributes(monoScript.GetClass(), typeof(ScriptOrder))) { var currentOrder = MonoImporter.GetExecutionOrder(monoScript); var newOrder = ((ScriptOrder)a).order; if (currentOrder != newOrder) MonoImporter.SetExecutionOrder(monoScript, newOrder); } } } } }
Five years later, @FlyingOstriche & @Partel-Lang solution is still the only way to do this by code. THANK YOU!