Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Accessing all the images of a sprite

Discussion in '2D' started by Wrekk, Jan 13, 2014.

  1. Wrekk

    Wrekk

    Joined:
    Jul 16, 2012
    Posts:
    51
    Hi everyone, I'd like to have access to all the images in a sprite in my code.
    How can I do this? I wish I could like define a sprite variable and use it like aSprite[imageNumber].

    Thanks. :)
     
  2. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Well a sprite can only have one image. If you are talking about a GameObject with many sprite children, then it is possible.

    Code (csharp):
    1.  
    2. transform.Find ("spriteName").gameObject.GetComponent<SpriteRenderer>();
    3.  
    If you had say a bunch of sprite children, labeled sprite0 to sprite99 then you could get them into an array:

    Code (csharp):
    1.  
    2.  
    3. List<SpriteRenderer>sprites;
    4.  
    5. for (int i = 0; i < 100; i++)
    6. {
    7.    SpriteRenderer spr = transform.Find ("sprite" + i).gameObject.GetComponent<SpriteRenderer>();
    8.  
    9.    sprites.add(spr);
    10. }
    11.  
    12.  
    Then you could access them in the manor you were hoping for.
     
unityunity