- Home /
Keep my custom handle visible even if object is not selected?
I coded some custom handles for my component today, using UnityEditor.Handles
and I realized that the handles are not visible if the object has not been selected. Is there a way to keep the handles visible even if there is no object selected?
Answer by whydoidoit · Mar 03, 2014 at 01:52 PM
Handles are not necessarily invisible when the object is not selected, but Gizmos are. If you want to draw something in the scene view at any time use the SceneView onSceneViewGUI delegate:
SceneView.onSceneGUIDelegate += YourSceneGUIFunction;
This function can contain Handles etc.
is deprecated. Use SceneView.duringSceneGui
ins$$anonymous$$d
Full example on how to render an handle (wire cube) below: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public class CameraTrigger : $$anonymous$$onoBehaviour { public Color handleColor; } #if UNITY_EDITOR [CustomEditor(typeof(CameraTrigger))] public class WireBox : Editor { private CameraTrigger myTarget; private Transform targetTransform; private void OnEnable() { myTarget = (CameraTrigger)target; SceneView.duringSceneGui -= OnScene; SceneView.duringSceneGui += OnScene; } private void OnScene(SceneView sceneview) { if (myTarget == null) { return; } Handles.color = myTarget.handleColor; targetTransform = myTarget.transform; Handles.DrawWireCube(targetTransform.position, targetTransform.localScale); } } #endif
Answer by mightyboat · Nov 08, 2017 at 08:21 PM
I had this same issue, and tried multiple methods, but couldn't get it to work.
Just so this thread is more complete, I thought I'd share what eventually worked for me.
This is a solution that uses an Editor script:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ItemSpawnPoint),true)]
[InitializeOnLoad]
public class ItemSpawnPointEditor : Editor {
// This function is called for each instance of "spawnPoint" in the scene.
// Make sure to pass the correct class in the first argument. In this case ItemSpawnPoint
// Make sure it is a "static" function
// name it whatever you want
[DrawGizmo(GizmoType.InSelectionHierarchy | GizmoType.NotInSelectionHierarchy)]
static void DrawHandles(ItemSpawnPoint spawnPoint, GizmoType gizmoType)
{
GUIStyle style = new GUIStyle(); // This is optional
style.normal.textColor = Color.yellow;
Handles.Label(spawnPoint.transform.position, spawnPoint.name + "(ItemSpawnPoint)", style); // you can remove the "style" if you don't want it
}
}
ItemSpawnPoint is the component I want to have a label in the editor
Credits go to Unity Corgi Engine creator for this solution.
Thank you, i've spent a lot of time to find your answer and it is working perfectly!
Thanks! This is the best way I've found to do this. Works perfectly and super easy to implement!
Did worked for me too. BTW to check if it's selected add this in the top of the function:
bool selected = gizmoType == (GizmoType.Active | GizmoType.InSelectionHierarchy | GizmoType.Selected);
Hey, I would like to add a button to this label (I would like, when I click on the ItemSpawnPoint, to select the gameObject). I have tryed to use Handles.Button, but it don't work inside this function... any suggestion ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613135243im_/https://answers.unity.com/themes/thub/images/avi.jpg)