- Home /
Custom Inspector - Update() alternative?
Hi! I'm trying to move the "preview" functionality from a separate window (which is created by clicking a button in my custom inspector) to the SceneView itself. The problem is, that the class "Editor" has no Update() function to override. From what I've seen there is no function at all that gets called as frequent as I need it. This is bad since while doing the "preview" I have to calculate and update variables very often.
I thought about simply creating an invisible EditorWindow and using it's Update() to do everything but maybe there is a more elegant solution.
On a sidenote - is there a way to manipulate the SceneCamera's cullingMask so I only get to see the objects I like while doing the "preview"? (by code I mean)
Thanks in advance,
delstrega
The editor class doesn't have a "Update()", it has a "OnInspectorGUI()"
http://docs.unity3d.com/Documentation/ScriptReference/Editor.html
Yeah I know. That's exactly my problem. OnInspectorGUI() doesn't get called frequently but only if the inspector needs a redraw. There must be something I can do... :(
I tried calling this.Repaint() and must say - yeah it works. OnInspectorGUI() gets called like crazy. The downside is, that everything I have in there naturally gets executed very often now - even if it's time consu$$anonymous$$g or just not necessary per se - like GUI stuff :D Well, I bit the bullet and just created an invisible EditorWindow and do the stuff in it's Update. This way I get the benefit of only doing the stuff I want in Update and keep the redraws to a limit. Thanks though for the great idea - it's just not practical with how I setup my custom inspector. :) Repost your comment as an answer please so I can close this thing:)
Answer by scanzy · Jul 24, 2016 at 10:00 PM
You can use EditorApplication.update
and bind it to your Update
function
public class YourInspector : Editor
{
void OnEnable() { EditorApplication.update += Update; }
void OnDisable() { EditorApplication.update -= Update; }
void Update()
{
//your code here...
}
}
Note this will call Update function only if you see the inspector: If you want the function to be called also when you are not seeing the Inspector (e.g. you have selected another object) use this instead:
public class YourInspector : Editor
{
[InitializeOnLoadMethod]
public static void InitUpdate() { EditorApplication.update += Update; }
static void Update()
{
//your code here...
}
}
EditorApplication.update -= Update;
Does this line even work? from what I read, it should not.
Uhm, yes it should. Where did you read it doesn't? You know how multicast delegates work?
When doing this:
EditorApplication.update += Update
You actually do
EditorApplication.update = Delegate.Combine(EditorApplication.update, Update);
$$anonymous$$ulticast delegates are actually implemented as linked list but the implementation detail is irrelevant. The point is that you can combine several delegates into a single one that way.
When you do this
EditorApplication.update -= Update
You actually do
EditorApplication.update = Delegate.Remove(EditorApplication.update, Update);
The Remove implementation will search through the list of delegates and will remove the delegate with the same target object and method reference.
It doesn't work because is being called within a Static method. For that to work, Update should be static aswell.