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.
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.
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.
// 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);
using System;
using System.Collections.Generic;
using UnityEngine;
public class ScriptOrder:Attribute {
public int order;
public ScriptOrder(int order) {
this.order = order;
}
}
ScriptOrderManager.cs
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);
}
}
}
}
}