Hiding helper geometry during runtime
I'm trying to use a collection of "helper geometries" inside the editor. These helpers mainly provide location and direction reference for my scripts for interactable game objects such as furniture, doors, etc. and are assigned to my prefabs. Upon interaction with one of those objects, the player character will locate the helper geometry and move to it before initiating the interaction animation. Furthermore they serve as a visual aid so I don't accidentally end up blocking any of these game objects. While being visible in the Unity editor these helpers need to be invisible during game mode.
My current implementation is to place all of these "helper geometries" on a seperate layer which is excluded from being rendered by the camera. But maybe there is a more practical way to deal with this?
Answer by tanoshimi · Jun 25, 2016 at 08:27 AM
To visualise locations, directions, or volumes in scene view only, you should use gizmos.
Thanks, that helped.
Here is my current script if any artist like me ever needs a reference to start from.
public class InteractionPoints : $$anonymous$$onoBehaviour {
public $$anonymous$$esh Interaction$$anonymous$$esh; //Custom mesh goes in here
// Use this for initialization
public void OnDrawGizmos () {
Gizmos.color = Color.green;
Gizmos.DrawWire$$anonymous$$esh (Interaction$$anonymous$$esh, transform.position, transform.rotation, transform.localScale);
}
}
Answer by Invertex · Jun 25, 2016 at 03:01 AM
Have you tried using Platform Dependent Compilation?
http://docs.unity3d.com/Manual/PlatformDependentCompilation.html
#if UNITY_EDITOR
//Unity Editor only code here
#endif
is what you want to wrap those display helper object operations in.
Answer by -_-_-_-_-_ · Jun 25, 2016 at 07:43 PM
@Privertex Try This:
using UnityEngine;
using System.Collections;
public class thing : MonoBehaviour {
void SetVisibility (bool Visibility, bool Collidable, GameObject target) {
MeshRenderer renderer = target.GetComponent <MeshRenderer> ();
Collider collider = target.GetComponent <Collider> ();
if (renderer)
renderer.enabled = Visibility;
if (collider)
collider.enabled = Collidable;
}
void SetVisibility (bool Visibility, GameObject target) {
MeshRenderer renderer = target.GetComponent <MeshRenderer> ();
if (renderer)
renderer.enabled = Visibility;
}
}
This code disabled the mesh renderer to make it invisible for camera, in additionally, this also sets the collider so nothing collides into it.
The if statements detect if there is no certain coponent on the gameObject, it will not attempt to set the enabled property of the component and therefore will not throw an exeption.(Give an error in the Console)
P.S So, if you want to a GameObject disappear, disable the mesh renderer, which essentially visualizes the $$anonymous$$esh to a Camera. The Collider essentially adds a solid collision to it, so if you want to disable collision, disable the collider.
Answer by Plattinator · Jun 25, 2016 at 07:43 PM
I put together a little script that you can put on any game object to exclude it from the build and also destroy the game object that it's attached to when testing in the editor.
using UnityEngine;
/// <summary>
/// Causes the attached game object and its children to only exist while in edit mode. The game object will not be included
/// in the build, so there is no performance hit for using this on objects that are purely visual aids in the editor.
/// </summary>
[ExecuteInEditMode] // So that OnDestroy will be called if the component is removed while in edit mode.
public class EditorOnlyGameObject : MonoBehaviour
{
#if UNITY_EDITOR
// Called when this component is attached to a game object.
private void Reset()
{
// This will make it so the game object this is attached to won't even be included in the build.
// Make sure the game object and all of its children are not needed for gameplay and are purely visual helpers in the editor.
gameObject.hideFlags = HideFlags.DontSaveInBuild;
}
private void Awake()
{
if (UnityEditor.EditorApplication.isPlaying)
{
// Destroying the game object will help catch any errors where something was referencing an editor visual helper,
// since this game object won't even exist in a build.
Destroy(gameObject);
}
}
private void OnDestroy()
{
// If this gets removed from a game object while in edit mode...
if (!UnityEditor.EditorApplication.isPlaying)
{
// Reset the game object back to normal so that the game object will be included when making a build.
gameObject.hideFlags = HideFlags.None;
}
}
[UnityEditor.CustomEditor(typeof(EditorOnlyGameObject))]
private class Editor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
UnityEditor.EditorGUILayout.HelpBox(
"This game object and its children only exists while in edit mode!", UnityEditor.MessageType.Warning, true);
}
}
#endif
}
You can also use the game object icon feature (https://docs.unity3d.com/351/Documentation/Manual/GizmoandIconVisibility.html) to stick an icon on any game object. It will only be visible in the scene view of the editor.