Search Unity

Script Execution Order manipulation

Discussion in 'Scripting' started by Nems, Apr 4, 2012.

  1. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
    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):
    1.  
    2. SetScriptOrder(MonoBehavior component, ScriptOrder order)
    3.  
    With ScriptOrder possible values : BeforeDefaultTime, DefaultTime, AfterDefaultTime.

    Thanks in advance.
     
  2. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    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
     
  3. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
    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.
     
  4. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
    Any idea to achieve that ?
     
  5. Stickworm

    Stickworm

    Joined:
    Apr 24, 2011
    Posts:
    67
    *BUMP*

    Yes I'd also like to know how to do this, any clues anyone?
     
  6. LI LONG

    LI LONG

    Joined:
    Feb 28, 2013
    Posts:
    55
    Yes I'd also like to know how to do this, any clues anyone?
     
  7. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,319
    You can use MonoScript and MonoImporter for that, note that it only works in Unity4.

    Code (csharp):
    1.  
    2.  
    3. // First you get the MonoScript of your MonoBehaviour
    4. MonoScript monoScript = MonoScript.FromMonoBehaviour(yourMonoBehaviour);
    5.  
    6. // Getting the current execution order of that MonoScript
    7. int currentExecutionOrder = MonoImporter.GetExecutionOrder(monoScript);
    8.  
    9. // Changing the MonoScript's execution order
    10. MonoImporter.SetExecutionOrder(monoScript, x);
    11.  
    12.  

    You will of course need an Editor class for this.

    Cheers,
    Pärtel
     
  8. FlyingOstriche

    FlyingOstriche

    Joined:
    Jul 13, 2012
    Posts:
    11
    I wrote an attribute which does this.
    Code (CSharp):
    1.  
    2. [ScriptOrder(-100)]
    3.  
    ScriptOrder.cs
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScriptOrder:Attribute {
    6.     public int order;
    7.  
    8.     public ScriptOrder(int order) {
    9.         this.order = order;
    10.     }
    11. }
    12.  
    ScriptOrderManager.cs
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [InitializeOnLoad]
    7. public class ScriptOrderManager {
    8.  
    9.     static ScriptOrderManager() {
    10.         foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) {
    11.             if (monoScript.GetClass() != null) {
    12.                 foreach (var a in Attribute.GetCustomAttributes(monoScript.GetClass(), typeof(ScriptOrder))) {
    13.                     var currentOrder = MonoImporter.GetExecutionOrder(monoScript);
    14.                     var newOrder = ((ScriptOrder)a).order;
    15.                     if (currentOrder != newOrder)
    16.                         MonoImporter.SetExecutionOrder(monoScript, newOrder);
    17.                 }
    18.             }
    19.         }
    20.     }
    21. }
     
  9. rossstyantsemteq

    rossstyantsemteq

    Joined:
    Apr 1, 2019
    Posts:
    2
    That's awesome. Thanks
     
  10. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,234
unityunity