Importing Translated Text into Custom Script
Importing Translated Text into Custom Scripts
Overview
Example: Displaying Translated Error Messages
Step-by-Step Breakdown
Script Example
using System.Collections;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
namespace LanguageManager
{
/// <summary>
/// This script is used to display an error message from the LanguageManager system
/// on a Text or TextMeshProUGUI component.
/// </summary>
public class ErrorMessageTest : MonoBehaviour
{
public TextMeshProUGUI textMeshProUGUI; // The TextMeshPro component to display the error message
public UnityEngine.UI.Text text; // The Unity Text component to display the error message
public string textId; // The ID of the text in the LanguageManager
/// <summary>
/// Retrieves the error message from LanguageManager using the provided textId
/// and displays it on the specified Text or TextMeshProUGUI component.
/// </summary>
public void ShowErrorMessage()
{
try
{
// Get the error message from the LanguageManager using the textId
string errorMessage = LanguageManager.Instance.GetTextEntryByID(textId);
// Check if TextMeshProUGUI is assigned and set the error message
if (textMeshProUGUI != null)
{
textMeshProUGUI.text = errorMessage;
textMeshProUGUI.color = Color.red; // Example: Set the text color to red
}
// If not, check if Unity Text is assigned and set the error message
else if (text != null)
{
text.text = errorMessage;
text.color = Color.red; // Example: Set the text color to red
}
else
{
Debug.LogError("No Text or TextMeshProUGUI component is assigned to display the error message.");
return;
}
}
catch (System.Exception ex)
{
Debug.LogError("Failed to retrieve or display the error message: " + ex.Message);
}
}
}
}Key Concepts
Usage Scenario
Last updated