[UNITY]2DRPG開発日誌 #53 クラス内クラスの使い方(システムメッセージ編)

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

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

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 TestTextManager : SerializedMonoBehaviour
{

   //クラス内クラス
   class CustomSystemTMP
   {
      public TestTextManager testTextManager;
      public GameObject obj_TMP;
      public TextMeshProUGUI tmp;

      /// <summary>
      /// システムTMP生成
      /// </summary>
      public CustomSystemTMP Instantiate_CustomSystemTMP(GameObject _parentObj, Vector3 _position, string _text, UIColor _color = UIColor.White)
      {
         //カスタムシステムメッセージを生成
         CustomSystemTMP csTmp = new CustomSystemTMP();

         //メッセージを生成
         csTmp.obj_TMP = Instantiate(testTextManager.prefab_SystemMessage, _parentObj.transform, false);

         //親オブジェクトを設定
         csTmp.obj_TMP.transform.SetParent(_parentObj.transform);

         //位置を設定
         csTmp.obj_TMP.transform.localPosition = _position;

         //メッセージ全体のサイズを設定
         RectTransform rt = csTmp.obj_TMP.GetComponent<RectTransform>();
         rt.sizeDelta = Vector2.one;

         //TMPの設定
         csTmp.tmp = csTmp.obj_TMP.GetComponent<TextMeshProUGUI>();
         csTmp.tmp.fontSize = 50; //文字サイズ
         csTmp.tmp.alignment = TextAlignmentOptions.Center; //中央揃え(横)
         csTmp.tmp.alignment = TextAlignmentOptions.Midline; //中央揃え(縦)
         csTmp.tmp.lineSpacing = 100; //行間

         //色を設定
         if (_color == UIColor.White)
            csTmp.tmp.color = testTextManager.dic_UIColor[UIColor.White];
         else if (_color == UIColor.DeepRed)
            csTmp.tmp.color = testTextManager.dic_UIColor[UIColor.DeepRed];
         else if (_color == UIColor.Red)
            csTmp.tmp.color = testTextManager.dic_UIColor[UIColor.Red];

         //自動で改行しないようにする
         csTmp.tmp.enableWordWrapping = false;

         //枠をハミ出るようにする
         csTmp.tmp.overflowMode = TextOverflowModes.Overflow;

         //テキスト内容
         csTmp.tmp.text = _text;
         return csTmp;
      }
   }

   //クラス内クラスをインスタンス化
   CustomSystemTMP customSystemTMP = new CustomSystemTMP();

   //色(カラーコード)
   Dictionary<UIColor, Color> dic_UIColor = new Dictionary<UIColor, Color>{
      { UIColor.White, Color.white },
      { UIColor.DeepRed, new Color(180f / 255f, 32f / 255f, 42f / 255f) },
      { UIColor.Red, Color.red }
   };

   //色の名前
   enum UIColor
   {
      White,
      DeepRed,
      Red
   }

   void Start()
   {
      //クラス内クラスに自分自身を認識させる
      customSystemTMP.testTextManager = this;
   }

   [Button]
   void InstantiateSystemMessage()
   {
      //メッセージを生成
      CustomSystemTMP csTmp = customSystemTMP.Instantiate_CustomSystemTMP(Canvas, Vector2.zero, "これが最終テストメッセージです。\n良い感じですね!");
   }

   //親オブジェクト
   [SerializeField] GameObject Canvas;

   //システムメッセージのプレハブ
   [SerializeField] GameObject prefab_SystemMessage;
}

 

 

UNITY
スポンサーリンク
Share
Follow Us
KITTYPOOL