Game Development Reference
In-Depth Information
More information on polymorphism in C# can be found at http://
msdn.microsoft.com/en-GB/library/ms173152.aspx .
C# properties
When assigning values to class variables, such as MyClass.x = 10; , there are a
couple of important things to take care of. First, you'll typically want to validate
the value being assigned, ensuring that the variable is always valid. Typical cases
include clamping an integer between a minimum and maximum range or allowing
only a limited set of strings for a string variable. Second, you might need to detect
when a variable changes, initiating other dependent functions and behaviors. C#
properties let you achieve both these features. Refer to the following code sample
1-15, which limits an integer between 1 and 10 and prints a message to the console
whenever it changes:
01 using UnityEngine;
02 using System.Collections;
03 //------------------------------------------------------
04 //Sample class - can be attached to object as a component
05 public class Database : MonoBehaviour
06 {
07 //------------------------------------------------------
08 //Public property for private variable iMyNumber
09 //This is a public property to the variable iMyNumber
10 public int MyNumber
11 {
12 //Called when retrieving value
13 get
14 {
15 return iMyNumber; //Output iMyNumber
16 }
17
18 //Called when setting value
19 set
20 {
21 //If value is within 1-10, set number else ignore
22 if(value >= 1 && value <= 10)
23 {
24 //Update private variable
25 iMyNumber = value;
26
 
Search WWH ::




Custom Search