Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Extension Methods

Discussion in 'Scripting' started by merlin981, Sep 30, 2014.

  1. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    I'm working on a (free) asset package for Unity, which is basically a collection of extension methods (C#) to make life easier / faster for us devs.

    I'm asking the community for any extension methods you'd like to see included.

    If you have a common method in Unity (for example, setting the X value of a transform, or recursively setting a layer on an object and its children), please post here and I'll be happy to add it to the package, along with all appropriate acknowledgements.

    Thanks,
    Philip
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Vector3.Add(Vector3 v); //Adds v to Vector3
    Vector3.Add(float x,float y,float z) // Adda XYZ as a vector to Vector3.

    The following 3 could be put into a "FileFormatIO" class or similar
    A way to load WAV files from disk ( I have code for this if you need )
    A way to load JPG files from disk ( I have code for this if you need )
    A way to load DDS files from disk ( I have code for this if you need )
     
  3. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Well if you are working on a general solution here's a few that might be handy to have.

    - Method that finds transform(s) by name from transform hierarchy. Meaning if you have complex transform like car with ton of child transforms you'd be able to find all the transforms named "wheel" or just one.
    - Method that does the above but returns the specified component(s) e.g all WheelBehaviors from wheels.

    we have in-house methods for these and they have been really handy especially for user interface development where you are want to find certain labels, textboxes which are children of UI windows.

    Getting the distance between transforms quickly might be handy as well. Fairly simple to implement but fitting for general solution.
    - float transform.DistanceTo(Transform t);

    Doesn't the Vector3.Scale already do this?
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Vector3.Scale scales it, not add :p
     
  5. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Doesn't the vector class already work with + operator?

    I mean I find myself often using

    Code (CSharp):
    1. transform.position += direction * Time.deltaTime;
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I think I was trying + and not += .
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Here are a couple I wrote which I use a lot

    Code (CSharp):
    1.         public static T GetInterface<T>(this GameObject go) where T : class
    2.         {
    3.  
    4.             if(!typeof(T).IsInterface)
    5.             {
    6.                 Debug.LogError(typeof(T).ToString() + " is not an interface");
    7.                 return null;
    8.             }
    9.  
    10.             return go.GetComponents<Component>().OfType<T>().FirstOrDefault();
    11.         }
    12.  
    13.         public static T GetInterfaceInChildren<T>(this GameObject go) where T : class
    14.         {
    15.            
    16.             if(!typeof(T).IsInterface)
    17.             {
    18.                 Debug.LogError(typeof(T).ToString() + " is not an interface");
    19.                 return null;
    20.             }
    21.            
    22.             return go.GetComponentsInChildren<Component>().OfType<T>().FirstOrDefault();
    23.         }
    24.  
    25.         public static IEnumerable<T> GetInterfaces<T>(this GameObject go) where T : class
    26.         {
    27.  
    28.             if(!typeof(T).IsInterface)
    29.             {
    30.                 Debug.LogError(typeof(T).ToString() + " is not an interface");
    31.                 return Enumerable.Empty<T>();
    32.             }
    33.  
    34.             return go.GetComponents<Component>().OfType<T>();
    35.         }
    36.  
    37.         public static IEnumerable<T> GetInterfacesInChildren<T>(this GameObject go) where T : class
    38.         {
    39.             if(!typeof(T).IsInterface)
    40.             {
    41.                 Debug.LogError(typeof(T).ToString() + " is not an interface");
    42.                 return Enumerable.Empty<T>();
    43.             }
    44.  
    45.             return go.GetComponentsInChildren<Component>(true).OfType<T>();
    46.         }
     
    Vipsu likes this.
  8. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Great. I have the V3 adds already.

    If you'd like to send me the FileFormatIO code via PM, I'll include it in the project.

    Thanks :D
     
  9. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    Great ideas. Thank you. I will add them as well
     
  10. merlin981

    merlin981

    Joined:
    Apr 16, 2012
    Posts:
    305
    That's very useful. I just had a hundred ideas of how to use this, immediately after seeing your code!

    I'm going to add this, too.

    Thank you