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?

2 Answers

2

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 ?

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.

Thanks, we are using gizmos to draw those now :)