Search Unity

C# Extension Methods

Discussion in 'Scripting' started by probbins, Dec 15, 2010.

  1. probbins

    probbins

    Joined:
    Oct 19, 2010
    Posts:
    216
    Hi Guys,

    Is it possible to do C# Extension methods in unity? :confused:

    What I am trying to do is extend GameObjects - as an example:

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public static class MyExtensions {
    7.    
    8.     public static void MyExtendedMethod(this GameObject gameObject) {
    9.        
    10.         // Do something
    11.  
    12.     }
    13. }
    14.  
    15.  
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class MyScript : MonoBehaviour {
    7.  
    8.     void Start () {
    9.        
    10.         GameObject.MyExtendedMethod();     
    11.        
    12.     }
    13.  
    14. }
    15.  

    Everything seems fine, monodevelop picks up my extended class when I go "GameObject" "." ->MyExtendedMethod() but gives a error message

    Code (csharp):
    1. error CS0117: `UnityEngine.GameObject' does not contain a definition for `MyExtendedMethod'
    Thanks in advance!
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    EDIT: nevermind.

    Not sure, maybe Unity doesn't factor those in.

    At the very least, you can definitly work around them if they aren't supported.
     
  3. probbins

    probbins

    Joined:
    Oct 19, 2010
    Posts:
    216
    Using a instance of a object works.

    Code (csharp):
    1. myGameObject.MyExtensionMethod();
    Which is great, but I want to also be able to do

    Code (csharp):
    1. GameObject.MyExtensionMethod();
    Any suggestions would be great!
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Oh woops, completely overlooked that.

    Static extension methods do not exist in C# (Unity or otherwise). This is by design.

    Your best workaround is to simply create your own custom GameObjectEx class:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public static class GameObjectEx
    6. {
    7.     public static void DoSomething(GameObject target)
    8.     {
    9.         // Do something
    10.     }
    11.    
    12.     public static void DoSomethingElse()
    13.     {
    14.         // Do something else
    15.     }
    16. }
    17.  
    18. //elsewhere
    19. GameObjectEx.DoSomething(myGameObject);
    20. GameObjectEx.DoSomethingElse();
    21.  
     
    Last edited: Dec 15, 2010
  5. probbins

    probbins

    Joined:
    Oct 19, 2010
    Posts:
    216
    Sorry I don't quite follow.

    I thought extensions have to be static.

    Code (csharp):
    1. CS1105: `MyExtensions.MyExtensionMethod(this GameObject, string)': Extension methods must be declared static
     
  6. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You define them with a "static" keyword, but they are accessed like instance-level methods. Notice the "this GameObject"; it's providing the context of "this". Static methods have no concept of "this"

    So it's possible to create an extension method that operates like this:
    myGameObjectInstance.MyExtensionMethod();

    But it is not possible to create an extension method that operates like this:
    GameObject.MyExtensionMethod();

    EDIT: there's more information here: http://msdn.microsoft.com/en-us/library/bb383977.aspx
    It touches on the static/instance nature of extension methods.
     
    Last edited: Dec 16, 2010
  7. probbins

    probbins

    Joined:
    Oct 19, 2010
    Posts:
    216
    Thanks