- Home /
Is there a scene setup call or update for editor scripts?
Problem: I have an editor script that needs to perform checks within the scene, creating/deleting objects in some cases, but I am only able to get the script to execute when the object is selected (OnInspectorGUI). This is problematic however in that it requires selecting the object. I need the script to update automatically regardless of what object is selected.
Question: Is there a method or a hook I can register that will execute my script, for example upon opening a new scene? Or is there an equivalent of the Update() method for editor scripts?
Keep in mind this is all within the editor, not during runtime.
EDIT: Re-opening Question... There's one fatal flaw with using ExecuteInEditMode, and that is that when I go to build the game (for iOS in my case) I get a bunch of "Unknown Identifier" errors for EditorUtility and AssetDatabase. These are necessary for the setup I need to perform. If I move all the code into my editor script, it works great, but there's no hooks for it to be called except when the object is selected. Does anybody know a way around this?
Answer by Jesus_Freak · Jan 09, 2011 at 05:18 PM
at the top of any javascript,
@script ExecuteInEditMode()
and that will tell the script to run like the game was playing. http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html
I thought this was the answer at first, but because I need to use EditorUtility and AssetDatabase, it isn't a complete solution. Trying to use these classes, even in code that is never executed during runtime, causes "$$anonymous$$ Identifier" build errors. As far as I know there's no type of preprocessor tricks in Javascript to get around this.
Answer by Steven-Walker · Jan 14, 2011 at 12:02 AM
I was able to work-around this problem using a C# utility file as an interface to the editor functions I need. Since C# supports the preprocessor and Javascript does not, I used it to create functions that strip out functionality at build time.
Below is a simplified version of the script I created:
using System; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif using System.Collections;
public class EditorSetupUtility {
public static UnityEngine.Object LoadAssetAtPath(string assetPath, Type type)
{
UnityEngine.Object asset = null;
if UNITY_EDITOR
asset = AssetDatabase.LoadAssetAtPath(assetPath, type);
endif
return asset;
}
}
And then in my runtime script I'd do something like this:
function Update() {
if(Application.isEditor && Application.isPlaying) {
var framePrefab : GameObject = EditorSetupUtility.LoadAssetAtPath("Assets/Prefabs/Frame.prefab", typeof(GameObject)) as GameObject;
// .. do other stuff ..
}
}
Answer by Vermelhu · Jun 12, 2011 at 11:56 AM
You can use the #if UNITY_EDITOR preprocessor tags in javascript, in case anyone's wondering :)Here's some code I wrote to allow resizing over cover points in the inspector in realtime:
@script ExecuteInEditMode()
...
...
#if UNITY_EDITOR
function Update() {
if (gameObject.transform.localScale.x != width)
gameObject.transform.localScale.x = width;
if (lowCover && gameObject.transform.localScale.y != lowCoverHeight) {
gameObject.transform.localScale.y = lowCoverHeight;
gameObject.transform.position.y =
gameObject.transform.parent.position.y + (lowCoverHeight/2);
}
else if (!lowCover && gameObject.transform.localScale.y != highCoverHeight) {
gameObject.transform.localScale.y = highCoverHeight;
gameObject.transform.position.y =
gameObject.transform.parent.position.y + (highCoverHeight/2);
}
}
#endif
Your answer
Follow this Question
Related Questions
RectTransform.rect.width/height Incorrect On Editor Startup 1 Answer
How to assign multiple EditorApplication.update delegates in JS? 2 Answers
When is Update called in edit mode? 3 Answers
reconnect programmatically instantiated prefab 1 Answer
How do I create a new object in the Editor as a child of another object? 1 Answer