Search Unity

Handy Abbreviations

Discussion in 'General Discussion' started by littlepolygon, Dec 28, 2015.

  1. littlepolygon

    littlepolygon

    Joined:
    Jul 7, 2013
    Posts:
    16
    I've noticed how my friends and I have a bunch of pithy abbreviation functions that we end up copying from project to project, so I started a public-domain repo where we can collect them all in one place to be more helpful to everyone: https://github.com/maxattack/Unity-Abbrev
     
    Kiwasi likes this.
  2. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    Code (CSharp):
    1. public static Vector2 yx(this Vector2 v) { return new Vector2(v.y, v.x); }
    This is the C# way of doing C++ macros? It doesn't generate any extra method calls?
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    This is called extension method. C# doesn't HAVE macros, so it probably generates function call. But then again, you can never be sure about anything with compiled language. This thing could - in theory - end up being inlined.

    By the way.
    Unity api has Vector3.forward, Vector3.up and such.
    It might make sense to use those instead of calling "new Vector3" every time.
    Stuff like "new Vector3(0.0f, 0.0f, 1.0f)" goes against usual recommendation to avoid using magic numbers.
     
    00christian00 likes this.
  4. littlepolygon

    littlepolygon

    Joined:
    Jul 7, 2013
    Posts:
    16
    Sure, but it doesn't have Vector3.backward, Vector3.left, Vector3.down, etc -- since I named all six variations it made the impl more consistent to just use literal constructors across them all.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes it does. And even if it didn't, you would just do -Vector3.forward for Vector3.back.

    --Eric
     
    Kiwasi likes this.
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    http://docs.unity3d.com/ScriptReference/Vector3-back.html
    http://docs.unity3d.com/ScriptReference/Vector3-left.html
    http://docs.unity3d.com/ScriptReference/Vector3-down.html

    Either way, I meant to say that you can redirect to Vector3 forward/right/up and use those instead of calling new Vector3 every time.
     
  7. littlepolygon

    littlepolygon

    Joined:
    Jul 7, 2013
    Posts:
    16
    We can see in MonoDevelop's assembly browser that those getters just call the same constructor literal. "Six eggs or a half dozen."

     
    jpthek9 likes this.
  8. littlepolygon

    littlepolygon

    Joined:
    Jul 7, 2013
    Posts:
    16
    But thanks for the tip about .back, etc :) I guess I assumed it wasn't there because UnityEngine.Transform doesn't have names for the negative unit axes.
     
  9. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    This. It's honestly personal preference for 90% of cases but returning a new Vector3 makes for 1 less instance of copying around a struct. For a library targeted for use by multiple people, why not go the extra meter with optimization?