unity hideininspector
HideInInspector
is a Unity attribute that can be used in your scripts to hide public fields in the inspector. It’s helpful when you want to prevent editing of certain fields in the Unity editor but still need them to be accessible from other scripts.
The Unity inspector is a panel in the Unity editor that allows you to adjust properties of game objects, components, and scripts. When you create a public field in a script that is attached to a game object, that field will be visible in the inspector and you can edit its value directly there.
Sometimes, however, you might want to create a public field (because you want it accessible to other scripts), but you don’t want that field to be visible or editable in the inspector. That’s when you use HideInInspector
.
Here’s a simple example:
Let’s say you want a rectangle class that can calculate the area and the perimeter of a rectangle. You need the length and width of the rectangle for these calculations, but you don’t want those fields to be visible or editable in the Unity inspector. Here’s how you might do it:
using UnityEngine; public class Rectangle : MonoBehaviour { [HideInInspector] public float length = 5f; [HideInInspector] public float width = 4f; // method to calculate area public float CalculateArea() { return length * width; } // method to calculate perimeter public float CalculatePerimeter() { return 2 * (length + width); } }
In this script, length
and width
are public fields, which means they would usually be visible and editable in the Unity inspector. However, because we’ve used the HideInInspector
attribute, they will not appear in the inspector.
The CalculateArea
and CalculatePerimeter
methods are also public, which means they can be called from other scripts. For example, if you had another script that needed to calculate the area or perimeter of a rectangle, you could use these methods.
However, remember that since length
and width
are public fields, even though they are hidden in the Unity inspector, they can still be accessed and changed from other scripts.
Example 2
This time, let’s consider a simple geometry scenario involving a 3D object, such as a Sphere, in Unity.
We’ll create a script that calculates the volume and surface area of a sphere. We’ll make the radius of the sphere a public field, but we’ll use HideInInspector
so that it’s not visible or editable in the Unity inspector.
Here’s how you might write the script:
using UnityEngine; public class SphereProperties : MonoBehaviour { [HideInInspector] public float radius = 1f; public float CalculateVolume() { // Formula for the volume of a sphere is 4/3 * pi * radius^3 return 4f / 3f * Mathf.PI * Mathf.Pow(radius, 3); } public float CalculateSurfaceArea() { // Formula for the surface area of a sphere is 4 * pi * radius^2 return 4f * Mathf.PI * Mathf.Pow(radius, 2); } }
In this script, radius
is a public field, but we’ve used the HideInInspector
attribute, so it will not be visible or editable in the Unity inspector.
The CalculateVolume
and CalculateSurfaceArea
methods are used to calculate the volume and surface area of the sphere, respectively. They can be accessed from other scripts because they are public methods.
Remember that radius
, being a public field, can still be accessed and modified from other scripts even though it is hidden in the inspector.
Example 3
For this example, let’s consider a simple scenario where we have a “Range Indicator” script. This script will draw a sphere in the scene view to indicate a certain range around the game object it’s attached to. However, we don’t want the radius of this sphere to be editable directly from the inspector, so we’ll use the HideInInspector
attribute.
using UnityEngine; public class RangeIndicator : MonoBehaviour { [HideInInspector] public float range = 5f; private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, range); } }
Gizmos are a handy feature in Unity that can be used to give visual cues in the scene view. They can be used to draw simple 3D shapes, lines, and other graphics.
In this script, range
is a public float which represents the radius of the sphere that will be drawn as a gizmo. But because of the HideInInspector
attribute, it won’t be visible or editable in the Unity inspector.
The OnDrawGizmos
method is a special method in Unity that is called to draw gizmos. In this method, we first set the color of the gizmos to red. Then, we use Gizmos.DrawWireSphere
to draw a wireframe sphere at the position of the game object with a radius defined by range
.
Remember that, although range
is hidden in the inspector, it can still be accessed and modified from other scripts. You could, for instance, have another script that changes the range based on game events.
This is a simple but powerful way to use gizmos for visual debugging and game design. You can see the range directly in the scene view, even when the game is not running, which can be very helpful when setting up your scenes.
Responses