LoginSignup
0
0

More than 1 year has passed since last update.

Unity 多次元のListがインスペクター上で表示されない

Last updated at Posted at 2022-12-04

問題

UnityEditorのインスペクターでは多次元のListが表示されなくListの状態を確認できません。
image.png

開発環境

Unity:2021.3.9
IDE:Rider
OS:Window10

対処方法

TextMeshProコンポーネントのtextにListの内容を出力して対処します。
この方法の良いところはインスペクターの挙動を考慮しなくて済むことです。
欠点は読み取り専用で値の変更はできません。

sample.cs
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;


public class Strings : MonoBehaviour
{
    // インスペクター上に表示されない
    [SerializeField] private List<Monster> monsterList = new List<Monster>();
    // インスペクター上に表示される。他にはbool型とかも
    [SerializeField] public int count = 0;
    // 文字出力用
    [SerializeField] private TextMeshProUGUI debugMonitor;
    
    // Update is called once per frame
    void Update()![ArrayImprovement.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/fcfd586f-0320-551e-8187-c11516287f94.gif)

    {
        displayByTextMeshPro();
    }
    
    
    private void displayByTextMeshPro()
    {
        debugMonitor.text = null;
        // ヘッダー
        debugMonitor.text = "Monster List \n hp ,name         \n";
        foreach (var monster in monsterList)
        {
            // -のときは左揃え、 -を外すと右揃え
            debugMonitor.text += string.Format("{0,-3:#} {1,-9:#} \n",  monster.Hp,monster.Name );
        }
    }
    /// <summary>
    ///  GameObjectをマウスクリックすると monsterListにmonsterが追加されます。5回まで対応
    /// </summary>
    private void OnMouseDown()
    {
        if (count == 0)
        monsterList.Add(new Monster(10,"goblin"));
        if (count == 1)
            monsterList.Add(new Monster(20,"Orc"));
        if (count == 2)
            monsterList.Add(new Monster(30,"Giant"));
        if (count == 3)
            monsterList.Add(new Monster(50,"FireGiant"));
        if (count == 4)
            monsterList.Add(new Monster(70,"AncientGiant"));
        count++;
    }
}

class Monster
 {
     private int hp;
     private String name;

     public int Hp
     {
         get => hp;
         set => hp = value;
     }

     public string Name
     {
         get => name;
         set => name = value;
     }

     public Monster(int hp, string name)
     {
         Hp = hp;
         this.name = name;
     }

 }

実際の動き

下のgifアニメは実行したときの様子です。

ArrayImprovement.gif

参考

別の対処方法 
https://kan-kikuchi.hatenablog.com/entry/ValueListList

C# の文字揃え方法
https://ict119.com/alignment/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0