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

Loading Cards with different langauges

Discussion in '2D' started by moh05, Jul 12, 2017.

  1. moh05

    moh05

    Joined:
    Nov 26, 2015
    Posts:
    65
    Hello guys,

    So in my 2D card game, i have two set of cards ( differs in language).

    Right now, I directly fetch them using arrays:

    public Sprite[] CardsEN;
    public Sprite[] Cards;

    In the same class, i choose a card depending on the "Language" parameter like that:

    Sprite getCard( int index)
    {
    if ( Language == "EN")
    return CardsEN [index]
    else
    return Card [index]
    }

    I think this is not the best scenario as one set of card is on memory without being used. So how can i use memory in an efficient way here?

    Thank you.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    If possible, break the text away from the images so you're swapping text and using the same image, rather than having a different sprite per language.

    Instead of a ton of if/else, you could make a dictionary keyed by a language enum. So then you can lookup the right data using the language as the index.

    example of a card with one sprite per language
    Code (CSharp):
    1. public enum Language {
    2.     English,
    3.     Spanish
    4. }
    5.  
    6. public Dictionary<Language, Sprite> spritesByLanguage;
    7.  
    8. public Sprite getSprite(Language lang) {
    9.     return spritesByLanguage[lang];
    10. }
    Ideally you would have each translation as text, and then you would pick the right text from a list.

    Right now you have two lists containing all cards, but you can make it one list of Card type, where each Card class holds all the data about that card.
     
    fvde likes this.
  3. moh05

    moh05

    Joined:
    Nov 26, 2015
    Posts:
    65
    Hello,

    Thank you for your reply.

    For now, we can't change the sprites, so we are forced with two set of sprites.

    Using your code ( dictionary method) , the gameobject is still fetching all textures for both languages. Do you have another way to just fetch one set ?

    Thank you
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I'm not sure I fully understand what you mean by "fetching all textures", do you mean that it loads them into memory?

    As far as I know the most straightforward way to pick and choose assets without loading them all is to use the Resources folder, and getting the sprite using Resources.Load at runtime. Since you must get them using a string path, you'll have to create a folder structure to organize your sprites by language so that you can get them in an intuitive way.

    So say you have language folders in the root Resources folder like "Resources/English/" where all your english sprites are, you could try something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. public class Example : MonoBehaviour
    5. {
    6.     public enum Language
    7.     {
    8.         English,
    9.         Spanish
    10.     }
    11.  
    12.     public List<Sprite> sprites;
    13.     private Language language;
    14.  
    15.     public void setLanugage(Language language)
    16.     {
    17.         this.language = language;
    18.         loadCards();
    19.     }
    20.  
    21.     public Sprite getCard(int index)
    22.     {
    23.         if(sprites.Count > index)
    24.         {
    25.             return sprites[index];
    26.         }
    27.         else
    28.         {
    29.             return null;
    30.         }
    31.     }
    32.  
    33.     private void unloadCards()
    34.     {
    35.         foreach(Sprite sprite in sprites)
    36.         {
    37.             Resources.UnloadAsset(sprite);
    38.         }
    39.     }
    40.  
    41.     private void loadCards()
    42.     {
    43.         unloadCards();
    44.         string relativePath = language.ToString();
    45.         sprites = Resources.LoadAll<Sprite>(relativePath).ToList();
    46.     }
    47.  
    48. }
    https://docs.unity3d.com/ScriptReference/Resources.LoadAll.html
     
    Last edited: Jul 18, 2017