Search Unity

Country Flags Best practise.

Discussion in 'General Discussion' started by WirealTalha, Aug 19, 2019.

  1. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    hi, I'm creating a 2g game in which i have put 270 flags(each taking 27 kb in memory) in the resource folder and im using them for the leaderboard. (each entry is Player profile, country flag, name and highscore).

    I just want to know is this the best way to go about? putting all flags textures in the resource folder and using them.
    if there is a better method kindly let me know.

    thanks
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Depending on your approach, one of many options, is to create atlas, or multiple atlases, which each is collection of flags. Then you access flag icon, by setting relevant UV offsets.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That's less than 8 megabytes for all of them. Unless you're dealing with an extremely restrictive platform, it is a rather trivial amount of data nowadays. The resources folder would be fine, but whenever you call Resources.Load you might notice a slight lag.

    I'd probably just reference them all directly in the scene, taking care of loading them all at the time the scene first loads, unless your target platform can't spare the memory. Then I'd compare both my scene load times, and the performance and memory usage in game, between both ways of doing it and decide which to go with.
     
    WirealTalha, Ony and SparrowGS like this.
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What Joe said, but if you really wanna optimize it - making an atlas with corresponding UVs is the best I can think of too.
     
    WirealTalha, Ony and Joe-Censored like this.
  5. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Thankyou all for replying. Atlas it is. This has been really helpful.
     
  6. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Hello Guys, I have studied about Atlases, Now I know their use and all.

    here's a technical question. We are sacrificing memory over cpu uasge right?

    the whole atlas will take some 7 mbs in this case.

    Is this 7mbs as a whole thorughout game??


    or is it 7mb memory for each picture,(i.e each time i retrieve any picture from the atlas)? I dont understand that.
     
  7. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436

    Atlas sounds like the way to go if you always need many different flags and have the memory to spare. You can't really calculate the size of the atlas as "number of flags * size of individual flag" because it doesn't take into account the wasted space that might occur on both individual sprites and the atlas, or possible memory savings due to using texture compression on one big atlas vs. many small sprites.

    The atlas texture will be stored in memory when at least one thing uses it, but will only be stored once. So no matter if you use 1 flag or 10000 flags, the memory footprint for the texture will be the same.

    P.S.: It's worth looking into how the 2D sprite systems work, it might by creating dynamic atlasses at runtime, making it potentially easier for you to access the sprites if you can keep them as individual files at design-time.
     
    Last edited: Sep 1, 2019
    WirealTalha likes this.
  8. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Thankyou, I've now got everything I need.
     
  9. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    HI, All, I've hit my first problem. Using spirte atlas does not reduce draw calls. The batches value remains the same if i load images from resources folder or SpriteAtlas and saved by batches value is always zero. what could be the issue? am i missing something??

    This is the line that fetches the image for me

    entry1 has {ProfilePic, Flag,Name,Highscore} gameobjects. Flag has a UI Image Component.

    Entry1.transform.GetChild(1).GetComponent<Image>().sprite = myatlas.GetSprite("Albania");
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    How exactly did you implement an atlas?
    I'm not sure we're talking about the same thing, by your code it looks liks a LUT for materials.
     
  11. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Mate i
    mate i followed this video.



    but instead of did not create enum and instead of using sprite renderer i used Image.
     
  12. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142

    Code (CSharp):
    1. public class LeaderboardxEntries : MonoBehaviour
    2. {
    3.  
    4.     public SpriteAtlas myatlas;
    5.  
    6.     public GameObject Entry1;
    7.    public GameObject Entry2;
    8.     public GameObject Entry3;
    9.    public GameObject Entry4;
    10.  
    11.     void Start()
    12.     {
    13.        
    14.  
    15.      
    16.  
    17.         Entry1.transform.GetChild(1).GetComponent<Image>().sprite = myatlas.GetSprite("Albania");
    18.  
    19.           Entry2.transform.GetChild(1).GetComponent<Image>().sprite = myatlas.GetSprite("Algeria");
    20.           Entry3.transform.GetChild(1).GetComponent<Image>().sprite = myatlas.GetSprite("Australia");
    21.           Entry4.transform.GetChild(1).GetComponent<Image>().sprite = myatlas.GetSprite("Australia");
    22.  
    23.    
    24.     }
    this is exactly what the code is, right now just testing the water with atlases
     
  13. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Oh I didn't know unity has its own atlas thingy, I've always done it myself.. the hours wasted..

    anyway, how about a screenshot of how the atlas is setup and a screenshot of the game running with the stats window open.
     
  14. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    I can relate on the Wasted hours part bro.
    right the setup is exactly like shown in the video
    here are the stats Untitled.png