I want to disable game objects, which I will enable later in the game during gameplay. When I disabled objects, they are completely invisible the scene view, which makes it hard to work this way. I would like to disabled game objects, but still see them in the scene view, so I can easily know where they are and make modifications.
For example I place enemies, which will be activated when the player enters a trigger. I want to test the level, stop play mode and quickly adjust enemy positions without having to select the objects from the hierarchy, I just want to click on the enemies in the scene view as if they were enabled.
I don’t know a good way. I have two ideas.
-
Have your enemy GameObjects enabled in Edit Mode. If you want to hide them at first in the gameplay, hide them by script. For example you may have a GameManager script that hides all the enemies on Start or after the level is loaded.
-
Some editor scripting might help. I tried to write one.
Create a script called LetMeSeeYouGuys with the following content.
using UnityEngine;
[ExecuteInEditMode]
public class LetMeSeeYouGuys : MonoBehaviour
{
public GameObject[] guys;
public Vector2 buttonSize = new Vector2(100, 25);
public Vector2 buttonOffset = new Vector2(0, 40);
public Color buttonGUIColor = new Color(1, 1, 1, 0.8f);
void Awake()
{
#if !UNITY_EDITOR
Destroy(gameObject);
#endif
}
}
Create another script called LetMeSeeYouGuysEditor with the following content. Put it in the same folder as LetMeSeeYouGuys.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(LetMeSeeYouGuys))]
public class LetMeSeeYouGuysEditor : Editor
{
new private LetMeSeeYouGuys target;
void OnEnable()
{
target = (LetMeSeeYouGuys)base.target;
}
void OnSceneGUI()
{
if (target.guys == null || target.guys.Length == 0)
return;
Camera sceneCam = SceneView.currentDrawingSceneView.camera;
Handles.BeginGUI();
Color colorBackup = GUI.color;
GUI.color = target.buttonGUIColor;
foreach (GameObject guy in target.guys)
{
Vector3 screenPos = sceneCam.WorldToScreenPoint(guy.transform.position);
Vector2 guiPos = new Vector2(screenPos.x, sceneCam.pixelHeight - screenPos.y);
Rect buttonRect = new Rect(guiPos - target.buttonSize / 2, target.buttonSize);
buttonRect.x += target.buttonOffset.x;
buttonRect.y -= target.buttonOffset.y;
if (GUI.Button(buttonRect, guy.name))
{
// Debug.Log("Clicked!");
Selection.activeObject = guy;
}
}
GUI.color = colorBackup;
Handles.EndGUI();
}
// [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
// static void DrawGizmoForMyScript(LetMeSeeYouGuys target, GizmoType gizmoType)
// {
// // Cannot detect mouse click here...
// }
}
#endif
// Reference:
// https://forum.unity.com/threads/button-in-scene-view.259913/
// https://answers.unity.com/questions/19321/onscenegui-called-without-any-object-selected.html
In your scene
- Create a GameObject and attach the script
LetMeSeeYouGuys.
- Drag your disabled enemies into the
Guys list.
- Turn on Gizmos in the Scene view. Now you should see some Gizmo buttons in the Scene view. Click a button to select the enemy GameObject.
Currently the Gizmo buttons only show when the LetMeSeeYouGuys object is selected. Not very handy and I don’t know how to solve it. There is a workaround. You can add another Inspector tab.
Now you have another Inspector tab. Select the LetMeSeeYouGuys GameObject and then lock the tab. You can shrink it very small and keep working on the project like usual.