Game Development Reference
In-Depth Information
C#:
class MyClass {
public int myVar = 1;
void MyClass() {
Debug.Log("Hello World!");
}
}
If you are inheriing from
MonoBehaviour
, you should not
use constructors or destructors. Instead, use the event handler
funcions
Start
,
Awake
, and
OnEnabled
.
Limited interface support
While Unity's JavaScript does support inheritance and interfaces, it has very limiing caveat
that you can either inherit your class from an exising class, or declare one interface.
JavaScript (only one allowed):
class MyClass extends MyObject {…}
C#:
class MyClass : MonoBehaviour, IMyObject, IMyItem {…}
Generics
The C# syntax supports generics that allows you to use classes and methods, which do
not specifically declare a type. Instead, the type is passed as a parameter when calling the
method or instaniaing the class at runime.
.Net comes with some useful generic classes, such as the
List
and
Dictionary
, and
Unity's own API has some generic funcions, which remove the need for some of the verbose
casing that would otherwise be necessary in C#.
JavaScript:
//Automatically cast the correct type
var someScript : MyScript = GetComponent(MyScript);
//or using the Generic version in Javascript
var someScript : MyScript = GetComponent.<MyScript>();