Search Unity

Question Localization key showing next entry text on button click

Discussion in 'Localization Tools' started by ratshayuu, Feb 15, 2023.

  1. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Hi, it's only been a year since I got into unity and I'm a slow learner.

    https://drive.google.com/file/d/1879qwc3rmkij9-kcdEylc699jo1Kfw2J/view?usp=share_link
    I don't know if there's an easier way but I've been trying to make a book system, shown above.
    It has two large pages, Left and Right. Each page has three TextMeshPro, called title, content, and page number.

    https://drive.google.com/file/d/1WQvFjDxXzIdpshIVoCyBCRPYzhidZY3D/view?usp=share_link
    There's an integer called "page index". Pressing the left page will subtract 1 from 'page index' and change the texts' string data to the previous entry, unless it is already at the 0th entry. Pressing the right page will add 1 to the page index and change the string data to the next entry, unless it's the last entry.

    https://drive.google.com/file/d/1879qwc3rmkij9-kcdEylc699jo1Kfw2J/view?usp=share_link
    I wanted to add Korean, so I have a string table each for title and content.
    So right now I want to change from using a struct data to using the entry keys in the string table. Whenever the left or right page buttons are clicked, I want the TextMeshPro texts to show the string data from the table according to the numbers set in the Key.

    Below is something I had before. FlipRight() isn't too different from FlipLeft().
    public void FlipLeft()
    {
    if (currentPage > sourceData.minPageNumber)
    //minPageNumber is 1. "currentPage" is the page number on the left page. "nextPage" is the page number for the right, which is always 1 bigger than 'currentPage'.
    {
    pageIndex -= 1;
    currentPage -= 2;
    nextPage -= 2;
    FlipPage();
    }
    }
    private void FlipPage()
    {
    sourceData.titleNameLeft.text = pageData[pageIndex].pageTitle;
    sourceData.titleNameRight.text = pageData[pageIndex].pageTitle;
    sourceData.contextExp.text = pageData[pageIndex].pageContextL;
    sourceData.contextSymp.text = pageData[pageIndex].pageContextR;
    sourceData.pageNumLeft.SetText("- " + currentPage + " -".ToString());
    sourceData.pageNumRight.SetText("- " + nextPage + " -".ToString());
    FlipSound();
    }

    For now I've added this at the top:
    public LocalizedStringTable bookNTable = new LocalizedStringTable("3.Book_Name");
    public LocalizedStringTable bookLTable = new LocalizedStringTable("4.Book_ContentL");
    public LocalizedStringTable bookRTable = new LocalizedStringTable("5.Book_ContentR");
    and am planning to delete the struct data and make a private set of TextMeshProUGUI instead, since I'm moving on to using the string table.

    There was a post from 8 years ago here that I think may have helped me, but I wasn't able to figure it out.
     
    Last edited: Feb 15, 2023
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    That seems fine. I would probably just go for a List of LocalizedStrings and use that to represent all of the pages.

    Code (csharp):
    1. public List<LocalizedString> pages
    Then you can just show the current page(left side) and the current page + 1(right side) on the screen.
     
    ratshayuu likes this.
  3. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Thank you! I got the grasp of it :D
    How can I show the entries' text on a TextMeshPro's text?
    I'm thinking of doing something like this:

    public List<LocalizedString> title;
    [SerializeField] private TextMeshProUGUI textTitle;
    public int pageIndex = 0;

    private void OnEnable()
    {
    textTitle.text = ""; // To clear the string data in TextMeshPro
    textTitle.text = title[pageIndex]; // This here says I can't convert the types for now
    }
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  5. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    It gave me back an error saying LocalizedString doesn't have a definition for GetLocalizedText.
    These are the ones I have so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using UnityEngine.Localization;
    7. using UnityEngine.Localization.Tables;
    8. using UnityEngine.Localization.Settings;
    9.  
    10. public class Book_Page : MonoBehaviour
    11. {
    12.     public List<LocalizedString> title;
    13.     public List<LocalizedString> contentL;
    14.     public List<LocalizedString> contentR;
    15.     [SerializeField] private TextMeshProUGUI textTitle, textContentL, textContentR, textPageNumL, textPageNumR;
    16.     public int pageIndex = 0, currentPage = 1, nextPage;
    17.     private int minPageNum = 1, maxPageNum = 112;
    18.     [SerializeField] private AudioClip[] clip;
    19.     [SerializeField] private AudioClip randomClip;
    20.     private AudioSource audioSource;
    21.     private bool isAudioPlaying;
    22.  
    23.     void OnEnable()
    24.     {
    25.         audioSource = GetComponent<AudioSource>();
    26.         nextPage = currentPage + 1;
    27.         textTitle.text = "";
    28.         textTitle.text = title[pageIndex].GetLocalizedText();
    29.         //titleR.text = bookNTable.SharedData.Entries[pageIndex]; // These are the ones I'll change later on
    30.         //contentL.text = bookLTable.SharedData.Entries[pageIndex]; // These are the ones I'll change later on
    31.         //contentR.text = bookRTable.SharedData.Entries[pageIndex]; // These are the ones I'll change later on
    32.         textPageNumL.SetText("- " + currentPage + " -".ToString());
    33.         textPageNumR.SetText("- " + nextPage + " -".ToString());
    34.     }
    35.  
    36.     public void ResetPage()
    37.     {
    38.         pageIndex = 0;
    39.         currentPage = 1;
    40.         nextPage = currentPage + 1;
    41.         FlipPage();
    42.     }
    43.  
    44.     public void SkipToPlants()
    45.     {
    46.         pageIndex = 18;
    47.         currentPage = 37;
    48.         nextPage = currentPage + 1;
    49.         FlipPage();
    50.     }
    51.  
    52.     public void SkipToMeds()
    53.     {
    54.         pageIndex = 38;
    55.         currentPage = 77;
    56.         nextPage = currentPage + 1;
    57.         FlipPage();
    58.     }
    59.  
    60.     public void FlipLeft()
    61.     {
    62.         if (currentPage > minPageNum)
    63.         {
    64.             pageIndex -= 1;
    65.             currentPage -= 2;
    66.             nextPage -= 2;
    67.             FlipPage();
    68.         }
    69.     }
    70.  
    71.     public void FlipRight()
    72.     {
    73.         if (currentPage < maxPageNum - 1)
    74.         {
    75.             pageIndex += 1;
    76.             currentPage += 2;
    77.             nextPage += 2;
    78.             FlipPage();
    79.         }
    80.     }
    81.  
    82.     private void FlipPage()
    83.     {
    84.         textPageNumL.SetText("- " + currentPage + " -".ToString());
    85.         textPageNumR.SetText("- " + nextPage + " -".ToString());
    86.         FlipSound();
    87.     }
    88.     private void FlipSound()
    89.     {
    90.         if (!isAudioPlaying)
    91.         {
    92.             int randomNumber = Random.Range(0, 6);
    93.             randomClip = clip[randomNumber];
    94.             audioSource.clip = randomClip;
    95.             audioSource.Play();
    96.             if (Input.GetButtonDown("Left") || Input.GetButtonDown("Right"))
    97.             {
    98.                 audioSource.Stop();
    99.                 isAudioPlaying = false;
    100.             }
    101.         }
    102.     }
    103. }
    104.  
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Sorry, it should be GetLocalizedString()
     
  7. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    oh my god it works! :D I've been stuck on this for weeks, thank you so much!
    May I add your name in the special thanks in the credits?
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yeah sure :)
    Glad it worked. With API issues like this it's good to check if the IDE has a suggestion that is close, visual studio probably would have figured it out. As you start to type the method name see if there's anything popping up that is close. https://visualstudio.microsoft.com/services/intellicode/
     
    Last edited: Feb 15, 2023
  9. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    I'll keep that in mind, thank you! :D
     
    karl_jones likes this.