Game Development Reference
In-Depth Information
However, C# properties will never be displayed by default, either in
Release
or
Debug
mode. As discussed in
Chapter 1
,
Unity C# Refresher
, C# properties act like
accessor functions to a variable. They essentially permit validation on each
get
and
set
operation because every
get
and
set
operation entails an internal function
call. However, regardless of Unity's limitation in the Object Inspector, it's possible
to write an editor extension that will show all properties for a class in the Object
Inspector, which allows you to get and set the values directly. This section considers
how in more detail. Again, we'll have reason to rely heavily on reflection.
More information on the
SerializeField
class can be found in
the online Unity documentation at
http://docs.unity3d.com/
Consider the following code sample 8-8 that features a few properties:
//----------------------------------------------
using UnityEngine;
using System.Collections;
//----------------------------------------------
[System.Serializable]
public class ClassWithProperties : System.Object
{
//Class with some properties
//----------------------------------------------
public int MyIntProperty
{
get{return _myIntProperty;}
//Performs some validation on values
set{if(value <= 10)_myIntProperty = value;else
_myIntProperty=0;}
}
//----------------------------------------------
public float MyFloatProperty
{
get{return _myFloatProperty;}
set{_myFloatProperty = value;}
}
//----------------------------------------------
public Color MyColorProperty
{
get{return _myColorProperty;}
set{_myColorProperty = value;}