Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SOLVED : ########### Add Methods to Vector2R ###########

Discussion in 'Scripting' started by Quatum1000, Apr 10, 2022.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Hi everyone,

    I like to extend my own variable Vector2R by a bool and methods
    For example:

    Code (CSharp):
    1. struct Vector2R
    2.     {
    3.         public float x;
    4.         public float y;
    5.         public bool selected;
    6.  
    7.        public float X { get; set; }
    8.        public float Y { get; set; }
    9.        public bool Selected { get; set; }
    10.  
    11.     }
    How can I add the methods .Normalize() and .Zero to the variable?
    Eg:

    Vector2R v2_A = Vector2R.Zero;
    v2_A.Normalize(); // where only x,y, took into account

    Vector2R v2_B = new Vector2R(); // should initialize x=0, y=0, selected=false;

    Thank you!
     
    Last edited: Apr 10, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You do know that the properties you define are not related to those fields you defined right? The X property getter/setter won't get/set the field x. The are completely independent.

    Asking how to add a method is a slightly odd question but that's all you do, add a normal method and modify your x/y fields. structs can have methods, properties and fields just like classes so:

    Code (CSharp):
    1. struct Vector2R
    2. {
    3.     public void Normalize()
    4.     {
    5.        // do the XY stuff here.
    6.     }
    7.     ...
    8. }
    The "Zero" would be a static, readonly field.

    I think the best way to answer you is to perhaps point you to the Vector2.cs reference here.

    In that case, it's jus two float fields x & y and no getters, they're not needed. There's zero, normalize etc.
     
    Quatum1000 and Kurt-Dekker like this.
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Code (CSharp):
    1. public struct Vector2R
    2.     {
    3.         public Vector2R(float X, float Y, bool Selected)
    4.         {
    5.             x = X;
    6.             y = Y;
    7.             selected = Selected;
    8.         }
    9.         public float x { get; set; }
    10.         public float y { get; set; }
    11.         public bool selected { get; set; }
    12.         public float magnitude { get { return (float)Math.Sqrt(x * x + y * y); } }
    13.         public const float kEpsilon = 0.00001F;
    14.         static readonly Vector2 zeroVector = new Vector2(0F, 0F);
    15.         public Vector2 zero { get { return zeroVector; } }
    16.         public void Normalize()
    17.         {
    18.             float mag = magnitude;
    19.             if (mag > kEpsilon)
    20.             {
    21.                 this.x = this.x / mag;
    22.                 this.y = this.y / mag;
    23.             }
    24.             else
    25.             {
    26.                 this.x = 0;
    27.                 this.y = 0;
    28.             }
    29.         }
    30.     }
    31.  
    32. void Update() {
    33.        Vector2R z = new Vector2R(0.5f, 0.3f, true);
    34.        Vector2 zv = new Vector2(0.5f, 0.3f);
    35.  
    36.        zv.Normalize();
    37.        z.Normalize();
    38.  
    39.        Debug.Log(z.x + " x " + zv.x);
    40.        Debug.Log(z.y + " y " + zv.y);
    41. }
    42.  
     
  4. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Thanks a lot!

    * It's faster without getters ?
    * Can I multiply the Vector2R(x,y) by a single value in one line? eg:
    Vector2R v2r = Vector2R.zero;
    v2r *= offset;
     
    Last edited: Apr 10, 2022
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    A getter is a method so it's not as fast as just directly accessing a field albeit not slow by any means. Without any logic here, it's a simple data-type; I'd just expose the x & y field directly as per the Vector2 code link I gave you.

    Yes, you need to overload the * operator (again, same as the Vector2 link): https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading