[UNITY]2DRPG開発日誌 #54 ローカライズでフォントも切り替える方法

動画で解説しましたので、詳しくは動画をご参照ください。

動画で使ったソースコードはこんな感じ。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro; //TextMeshPro
using Sirenix.OdinInspector; //Odin
using DG.Tweening; //DOTween

public class TestManager : MonoBehaviour
{
    public TestLocalizeManager testLocalizeManager;

    public enum LanguageState
    {
        English,
        Japanese,
        Korean
    }
    public LanguageState currentLanguageState;

    [Header("フォント")]
    public TMP_FontAsset fontAsset_System_English_Japanese;
    public TMP_FontAsset fontAsset_Quote_English_Japanese;
    public TMP_FontAsset fontAsset_System_Korean;
    public TMP_FontAsset fontAsset_Quote_Korean;

    [Header("テキスト")]
    public TextMeshProUGUI testText_SysmteMessage;
    public TextMeshProUGUI testText_Quote;


    [Header("ローカライザブルフォントを適用するTMPのリスト")]
    public List li_UpdateFontAssetTMP_System = new List();
    public List li_UpdateFontAssetTMP_Quote = new List();

    public void ローカライザブルフォントTMPをセット_システム(TextMeshProUGUI _tmp)
    {
        //既に登録済みでないかチェック
        foreach (TextMeshProUGUI tmpInList in li_UpdateFontAssetTMP_System)
            if (_tmp == tmpInList) //既に登録済みだったら、
                return;            //メソッドを抜ける

        //未登録なら追加
        li_UpdateFontAssetTMP_System.Add(_tmp);
    }

    public void ローカライザブルフォントTMPをセット_Quote(TextMeshProUGUI _tmp)
    {
        //既に登録済みでないかチェック
        foreach (TextMeshProUGUI tmpInList in li_UpdateFontAssetTMP_Quote)
            if (_tmp == tmpInList) //既に登録済みだったら、
                return;            //メソッドを抜ける

        //未登録なら追加
        li_UpdateFontAssetTMP_Quote.Add(_tmp);
    }

    void リストのTMP全てにフォントをセット(List _tmpList, TMP_FontAsset _fontAsset)
    {
        foreach (TextMeshProUGUI tmp in _tmpList)
            tmp.font = _fontAsset;
    }

    [Button]
    void 二つのTMPをリストにセット()
    {
        ローカライザブルフォントTMPをセット_システム(testText_SysmteMessage);
        ローカライザブルフォントTMPをセット_Quote(testText_Quote);
    }

    [Button]
    void 二つのTMPにテキストをセット()
    {
        testText_Quote.text = testLocalizeManager.hero_Quote;

        testText_SysmteMessage.text = testLocalizeManager.attack_SystemMessage;
    }

    [Button]
    public void 言語を変更(LanguageState _languageState)
    {
        if (_languageState == LanguageState.English)
        {
            currentLanguageState = LanguageState.English;

            testLocalizeManager.SetEnglish();

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_System, fontAsset_System_English_Japanese);

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_Quote, fontAsset_Quote_English_Japanese);
        }
        else if (_languageState == LanguageState.Korean)
        {
            currentLanguageState = LanguageState.Korean;

            testLocalizeManager.SetKorean();

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_System, fontAsset_System_Korean);

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_Quote, fontAsset_Quote_Korean);
        }
        else if (_languageState == LanguageState.Japanese)
        {
            currentLanguageState = LanguageState.Japanese;

            testLocalizeManager.SetJapanese();

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_System, fontAsset_System_English_Japanese);

            リストのTMP全てにフォントをセット(li_UpdateFontAssetTMP_Quote, fontAsset_Quote_English_Japanese);
        }
    }
}

 

UNITYローカライズ
スポンサーリンク
Share
Follow Us
KITTYPOOL