x


Execute code only when object is selected in editor or scene view

I have a script to use only inside the editor. How do I make code only run on a object that is selected / being moved around in the scene?

more ▼

asked Mar 24 '12 at 06:53 PM

Bluestrike gravatar image

Bluestrike
426 27 39 48

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Ok got it figured out, OnSceneGUI() in a editor script can be used to do things when the specific object is selected in the scene.

more ▼

answered Mar 25 '12 at 06:57 PM

Bluestrike gravatar image

Bluestrike
426 27 39 48

You didn't specify what you want to do when the object is selected. OnSceneGUI is a gui callback that allows you to render GUI elements inside the SceneView. OnSceneGUI is like OnGUI called for different events. For a general purpose you should use Update().

Mar 26 '12 at 01:35 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

You should look into the examples and demos of extending the editor and the Editor class. I'm not that familiar with UnityScript, but a custom editor for one of your class should look like this:

// MyClassEditor.js
@CustomEditor (MyClass)
class MyClassEditor extends Editor
{
    function OnInspectorGUI ()
    {
        DrawDefaultInspector();
    }
    function Update()
    {
        // ...
    }
}

Inside this class you have the target variable which holds the reference to the classinstance you've selected.

Note that Update isn't called periodically. It is called when something changed (you moved the camera, an object). If you need a periodically event There's also a delegate in EditorApplication

more ▼

answered Mar 25 '12 at 12:16 PM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

I doesn't appear that Update is a valid callback on Editor classes. In the docs, it refers to using Update on the actual MonoBehavior, along with @script ExecuteInEditMode().

Apr 01 '12 at 11:24 PM Ipsquiggle

Well i mixed up the different Update callbacks. EditorWindow has an Update callback: EditorWindow.Update.

However editor funcionality shouldn't be used in MonoBehaviour components since they go into the build and can't contain Editor functions unless they are surrouned with #IF UNITY_EDITOR and #ENDIF

You can still use the update delegate in any editor classes. Use OnEnable and OnDisable to attach / detach the delegate.

Apr 02 '12 at 01:08 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

I think you'd have to use another script

in C#:

if (Editor.target == ObjWithScript)
    ObjWithScript.GetComponent<ScriptName>().enabled = true;
more ▼

answered Mar 25 '12 at 02:06 AM

cowlinator gravatar image

cowlinator
159 2 4 6

I can't add this code anywhere in JS without having a compile error. tried my update function, tried the editor script for my script.

if(Editor.target == transform)

An instance of type 'UnityEditor.Editor' is required to access non static member 'target'.

Mar 25 '12 at 07:53 AM Bluestrike

This example is in C# and not in UnityScript(JS). I can't find any hint in your two sentences that you're using UnityScript.

Furthermore this fragment indeed doesn't make much sense to me ;)

Mar 25 '12 at 11:16 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x1673

asked: Mar 24 '12 at 06:53 PM

Seen: 1119 times

Last Updated: Apr 02 '12 at 01:08 AM