Extension Methods are a feature that adding new methods to an existing class, which class is non-generic but static class. Similar to the extension concept in Swift.
Features of Extension methods:
this
using UnityEngine;
using System.Collections;
public static class ExtensionMethods
{
//Notice that the first
//parameter has the 'this' keyword followed by a Transform
//variable. This variable denotes which class the extension
//method becomes a part of.
public static void ResetTransformation(this Transform trans)
{
trans.position = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = new Vector3(1, 1, 1);
}
}
To use above extension methods:
using UnityEngine;
using System.Collections;
public class SomeClass : MonoBehaviour
{
void Start () {
transform.ResetTransformation();
}
}
Join the newsletter to receive the latest updates in your inbox.