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?
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 ..
}
}
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: