Jump to content

How to trap or know when Gizmo selected in Editor


Recommended Posts

On 5/11/2016 at 10:55 PM, linuxgurugamer said:

I found the answer: To know if a gizmo is active, use the following:

if (HighLogic.FindObjectsOfType<EditorGizmos.GizmoOffset> ().Length > 0) 

 

This is a very expensive call, especially if called every frame. So I'd like to be able to get a callback whenever either the translate or rotate gizmo is clicked.

I haven't found any way yet, anybody have any ideas?

Thanks in advance

 

Link to comment
Share on other sites

FindObjectsOfType is a just a method Unity exposes, you can cache the result so:

EditorGizmos.GizmoOffest myGizmo = FindObjectsOfType<EditorGizmos.GizmoOffset>......

Then just refer to myGizmo instead of using the FindObejctsOfType call again.

D.

Link to comment
Share on other sites

Just now, Diazo said:

FindObjectsOfType is a just a method Unity exposes, you can cache the result so:

EditorGizmos.GizmoOffest myGizmo = FindObjectsOfType<EditorGizmos.GizmoOffset>......

Then just refer to myGizmo instead of using the FindObejctsOfType call again.

D.

The problem is that the gizmo object is only there when it's selected.  So when you select another gizmo, it goes away and the caching is stale.

I'm already doing that, have managed to reduce the calls a lot, but would like to get rid of them totally.

Link to comment
Share on other sites

6 hours ago, xEvilReeperx said:

You could add a little script to the gizmo prefabs that fires an event when they're created. You can avoid any FindObjectsOfType calls entirely that way

That would work.

How?

Edited by linuxgurugamer
Link to comment
Share on other sites

So, would it be something like this (not exactly sure about the names, not at my dev system now):

GizmoOffset ego;

ego.AddComponent("localscriptname");

or this:

EditorGizmos.GizmoOffset.Attach(.....)

 

Edited by linuxgurugamer
Link to comment
Share on other sites

You essentially have the right idea on the first one. The idea is to add a script onto prefab the game will use for each gizmo so that when the game clones it, your script will get cloned too. Then the usual MonoBehaviour methods will start running which is your chance to fire an event, add the gizmo to a list of opened gizmos, or whatever you want to do with it. Here's a self-contained example in the style of GameEvents:

[KSPAddon(KSPAddon.Startup.EditorAny, true)]
    class GizmoEvents : MonoBehaviour
    {
        public static readonly EventData<GizmoRotate> onRotateGizmoSpawned = new EventData<GizmoRotate>("onRotateGizmoSpawned");
        public static readonly EventData<GizmoOffset> onOffsetGizmoSpawned = new EventData<GizmoOffset>("onOffsetGizmoSpawned");

        class GizmoCreationListener : MonoBehaviour
        {
            private void Start()
                // I use Start instead of Awake because whatever setup the editor does to the gizmo won't be done yet
            {
                GizmoRotate rotate = null;
                GizmoOffset offset = null;

                if (gameObject.GetComponentCached(ref rotate) != null)
                {
                    onRotateGizmoSpawned.Fire(rotate);
                }
                else if (gameObject.GetComponentCached(ref offset) != null)
                {
                    onOffsetGizmoSpawned.Fire(offset);
                }
                else Debug.LogError("Didn't find a gizmo on this GameObject -- something has broken");

                // could destroy this MB now, unless you wanted to use OnDestroy to sent an event
            }

            private void OnDestroy()
            {
                // could also send an event on despawn here
            }
        }

 
        private void Awake()
        {
            AddListenerToGizmo("RotateGizmo");
            AddListenerToGizmo("OffsetGizmo");

            Destroy(gameObject);
        }


        private static void AddListenerToGizmo(string prefabName)
        {
            var prefab = AssetBase.GetPrefab(prefabName);

            if (prefab == null)
            {
                Debug.LogError("Couldn't find gizmo '" + prefabName + "'");
                return;
            }

            prefab.AddOrGetComponent<GizmoCreationListener>();

#if DEBUG
            Debug.Log("Added listener to " + prefabName);
#endif
        }
    }

    [KSPAddon(KSPAddon.Startup.EditorAny, false)]
    class TestGizmoEvents : MonoBehaviour
    {
        private void Start()
        {
            GizmoEvents.onRotateGizmoSpawned.Add(RotateGizmoSpawned);
            GizmoEvents.onOffsetGizmoSpawned.Add(OffsetGizmoSpawned);
        }


        private void OnDestroy()
        {
            GizmoEvents.onRotateGizmoSpawned.Remove(RotateGizmoSpawned);
            GizmoEvents.onOffsetGizmoSpawned.Remove(OffsetGizmoSpawned);
        }


        private void RotateGizmoSpawned(GizmoRotate data)
        {
            print("Rotate gizmo was spawned");
        }


        private void OffsetGizmoSpawned(GizmoOffset data)
        {
            print("Offset gizmo was spawned");
        }
    }

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...