Game Development Reference
In-Depth Information
Using attributes to customize inspector display
So how do attributes work? In short, attributes are a form of metadata; they work
like tags. Programmers can attach an attribute to a class, variable, or a method to
associate data with it, which is known to the compiler. The attribute itself is entirely
descriptive because it does nothing; it's simply data. The importance of attributes
arises because all the code based on .NET (or Mono) has the ability to step outside
itself and become self-conscious, that is, the ability to look at all the classes and data
types and instances contained inside the program. For each object in the program,
its metadata (attributes) can be queried and examined. This ability of a program to
"look at itself from the outside" is known as reflection as it's like looking in a mirror.
Of course, the program does not see itself in reverse or in distorted terms but rather
as it truly is, including all its metadata. To give a quick example of reflection, try out
the following code sample 8-2. This code will cycle through all the custom-made
classes in your Unity application across all source files. Notice it doesn't just list all
the instances of classes in the scene but all classes themselves (that is, the blueprints,
metaphorically speaking):
01 using UnityEngine;
02 using System.Collections;
03 using System.Reflection;
04 using System;
05
06 public class MyTestScript : MonoBehaviour
07 {
08 // Use this for initialization
09 void Start ()
10 {
11 //List all classes in assembly
 
Search WWH ::




Custom Search