1. 程式人生 > >Unity 通過反射獲取 SystemInfo 的欄位

Unity 通過反射獲取 SystemInfo 的欄位

建立 Scroll View,設定 Top、Bottom、Left、Right 為 0。
將 Text 新增到 Content 下面,調整 Content 的高度,使其大於 Text 的最大高度

using System;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {

    public Text text;

    // Use this for initialization
void Start () { Type type = typeof(SystemInfo); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty); StringBuilder sb = new StringBuilder(); sb.AppendLine("total: " + propertyInfos.Length + " items"
); for (int i = 0; i < propertyInfos.Length; i++) { sb.AppendFormat("{0:D2}. {1,-50} = ", i + 1, propertyInfos[i].Name); //sb.AppendFormat("{0:D2}. {1} = ", i, propertyInfos[i].Name.PadRight(50)); sb.Append(type.InvokeMember(propertyInfos[i].Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty, null
, null, null)); sb.AppendLine(); } text.text = sb.ToString(); } // Update is called once per frame void Update () { } private void OnGUI() { } }

這裡寫圖片描述