Game Development Reference
In-Depth Information
static class and the static method, which need to have the first parameter set to this fol-
lowed by its class type and the class name, as shown in the following code:
using UnityEngine;
using System.Collections;
public static class ExtensionMethods {
public static void ResetTransform ( this Transform t )
{
t.position = Vector3.zero;
t.rotation = Quaternion.identity;
t.localScale = Vector3.one;
}
}
To use it, we can just call the transform.ResetTransform() function from the
game object.
This extension method can only be created in C#. So, how can we use it if we use Unity
JavaScript? We can use the extension method by reordering the script complier to compile
the ExtensionMethods script before other scripts. This is because all the Unity
JavaScript will get executed before the C# script. To solve this problem, we can either put
the ExtensionMethods script in the Standard Assets folder or navigate to Edit | Pro-
ject Settings | Script Execution Order and add the script that we want to reorder.
Note
For more details, visit the following links:
http://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html
http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html
Search WWH ::




Custom Search