C# Generics, Statics

C# Generics provide a way to write code that can be used with a variety of different data-types, you'll recognize code that uses Generics when you see this type of code< T >. The T represents a place-holder for a data-type that is specified when you write the code, depending on the type of data used in your code.

UnityEngine: Example: GetComponent< T > All script components we write inherit from MonoBehaviour, and MonoBehaviour inherits from the Component Class. This allows us to use methods from the Component class without specifying an object to execute the method. See Generics: Unity Manual

Unity Example: GetComponent Method returns an object of the specified placeholder datatypes T tvariableName = GetComponent< T >( ); Animator animator = GetComponent< Animator >( ); MyComponent mc = GetComponent< MyComponent >( ); //custom component

C# Data-structures are classes defined in the C# language. C# provides many Data-Structures that use Generics: We'll work with the following Data-structures that implement C# Generics in this course, in all cases, our code will specify the datatype that replaces the type placeholder T C# Examples: List< T >, Dictionary< Tkey, Tvalue > , Queue< T > List< GameObject > list = new List< GameObject >( ); //list of GameObjects

UnityEngine Foundational Classes: Script API When we write code, we'll primarily work with code from the UnityEngine Library, where 3 of the main base classes we'll use are listed below with links to the Scripting API Object: Base class for all objects Unity can reference. GameObject: Base class for all entities in Unity Scenes. Component: Base class for everything attached to GameObjects.

C# Static Methods, Variables

When a method or variable is specified as static, it means that there's no need to create an object instance of that class in order to use the method. Instead, a method can be executed using the ClassName and dot notation to execute a method. If a class is specified as static, then all variables and methods within the class must be specified as static. When variables, methods, or classes are specified as static, the access modifier is usually specified as public. The example below shows that the Mathf library has a static method: Abs( float x ) We can see that to call the Abs( ) method, we can use the name of the class itself. Example: bool isWalking = Mathf.Abs( inputX ); //static method Normally, to call a method, you must have initialized a object, then the object executes the method. Example: Zombie myZombie = new Zombie( ); //initialize an object using constructor myZombie.dance( "samba"); //object uses dot notation to execute method We can see this used in Unity with the GameObject class static methods shown below. A static variable is associated with the class itself. It is not associated with an object instance of the class. This means that there is only one of instance of a static variable in existence when a program is executing, the variable is associated with the class rather than an object instance. Static variables will be used when creating a Singleton gameObjects. Unity Example: Static Method: GameObject.Find( ); Static GameObject Methods: GameObject.Find( "someGameObjectName" ) GameObject.FindWithTag( "SomeTag" ) GameObject.FindGameObjectsWithTags( "SomeTag" )

Last updated