There’s a way to force scripts to run even if the game is not running in the Editor:
In Javascript, simply put this on the top of the script:
@script ExecuteInEditMode()
And for C#:
[ExecuteInEditMode()]
There’s a way to force scripts to run even if the game is not running in the Editor:
In Javascript, simply put this on the top of the script:
@script ExecuteInEditMode()
And for C#:
[ExecuteInEditMode()]
2 Comments
Interesting,
1) How do you trigger the script?
The only way I found so far was to drag it onto a GameObject in the Hierarchy window. To re-activate it,I used the "Remove Component" popup from the Inspector, then re-dragged the script from the Project list back onto the GameObject
2)can you post a useful example ?
Mike
the script starts running as soon as it's done compiling, just put your code in Start or Update…
I sometimes use this for building debug GUI with UnityGUI. Here's a simple script I use:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class DEBUGDistanceBetween : MonoBehaviour {
public Transform t1;
public Transform t2;
void OnGUI () {
if(t1 && t2) GUI.Label(new Rect(20, 20, 200, 50), (t1.position – t2.position).magnitude.ToString("#.00"));
}
}