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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Method to assign to desired property within loop (lambda?)

Discussion in 'Scripting' started by HiddenMonk, Jun 7, 2015.

  1. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Lets say you have a vector2 array and you want to loop through them all to change only the x or y values. How would you do this?

    For example (this doesnt work)..
    Code (CSharp):
    1.     void MoveUVs(Action<Vector2> delegateAction, int targetSpace)
    2.     {
    3.         for(int i = 0; i < uvs.Length; i++)
    4.         {
    5.             delegateAction(uvs[i]) += targetSpace;
    6.         }
    7.     }
    An action returns void, and using a func doesnt seem to work either. The func could return a float, but that float is not part of the original vector anymore.
    I want to basically be able to do this...
    Code (CSharp):
    1. //move x values in the vector
    2. MoveUVs((v) => v.x, 5);
    3.  
    4. //move y values in the vector
    5. MoveUVs((v) => v.y, 5);
    Right now I can do something like this
    Code (CSharp):
    1. MoveUVs((v, i, t) => v[i].x += t, targetSpace);
    2.  
    3. void MoveUVs(Action<Vector2[], int, int> delegateAction, int targetSpace)
    4. {
    5.     for(int i = 0; i < uvs.Length; i++)
    6.     {
    7.             delegateAction(uvs, i, targetSpace);
    8.     }
    9. }
    But I dont like that.
     
    Last edited: Jun 7, 2015
  2. Mistale

    Mistale

    Joined:
    Apr 18, 2012
    Posts:
    173
    Responding from my iPad, so sorry if the answer is rather incomplete.
    I would use masks for selecting the component to affect.

    Like so:

    class VectorMask
    {
    public const Vector2 x = new Vector2(1f, 0f);
    public const Vector2 y = new Vector2(0f, 1f);
    public const Vector2 xy = new Vector2(1f, 1f);
    }

    MoveUVs(VectorMask.x, 5f);
    MoveUVs(VectorMask.xy, 2f);

    void MoveUVs(Vector2 mask, float targetSpace)
    {
    Vector2 offset = Vector2.Scale(new Vector2(targetSpace, targetSpace), mask);

    // Loop through all uvs and just add offset to each.
    }

    Since I use Vector2.Scale(), any component which has 0f as value in the mask will be zeroed out from the offset and therefore wont affect the uvs.
     
  3. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Following KISS principle:

    Code (csharp):
    1.  
    2. Vector2[] Vectors;
    3.  
    4. Vectors
    5.     .ToList()
    6.     .ForEach(v => v += new Vector2(5, 0));
    7.  
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I'm more interested in being able to grab any property value I want and being able to pass it and assign to it directly within another method. I guess this isn't really possible? The property will lose its reference.

    Your answers are more so workarounds such as the answer I gave.