Search Unity

how to show sprites images in unity

Discussion in 'Scripting' started by blesstc, Oct 21, 2017.

  1. blesstc

    blesstc

    Joined:
    Sep 25, 2017
    Posts:
    10
    I am creating a card game (52).I have a single sprite in my Unity and Its having all playing card in a single image and I adjusted through sprite editor and it shows different values (card_1,card_2 etc) for each card.

    I have 8 slots ( is layout group is best ? or some other way ? )in the game area and I need to display any 8 cards into these slots using c#. In order to happen this , I have started with array and I find difficult to read from sprite and show each cards in slot.

    Can any one guide me best practice and how to do ?
     
  2. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    To access a Sprite use GetComponent <Image> ().sprite.
    Code (CSharp):
    1. public Sprite mySprite;
    2.  
    3. public void Awake ()
    4. {
    5. gameObject.GetComponent <Image> ().sprite = mySprite;
    6. }
    Not too sure if this is what you are after exactly, or if it's the arrays you are struggling with?
     
    uncoded_coder likes this.
  3. blesstc

    blesstc

    Joined:
    Sep 25, 2017
    Posts:
    10
    Thanks for your reply. I have a large sprite as I have mentioned it has sub images ( card_0,card_1 etc). It can be seen from unity by pointing out to my large sprite. I need to load all sub images to array and need to display in Game. I am not getting an idea how to pass these images and also which image system I should choose in design area ( please note that I should be able to move this image from Game). I hope as it is Sprite I can move these images when its show in design area.
     
  4. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    I think you might need to start with the learning section to get familiar with some general concepts of unity.

    As for your current issue, it sounds like you imported a Sprite sheet to unity and sliced it up to "individual images". If you expose a Sprite array "public Sprite[] mySpriteArray" in script, you could drag and drop all the individual sprites there. Use the little lock in the inspector, so the inspector does not change when you change the selection