Unity通过反射获取SystemInfo的字段

发表于2019-01-17
评论0 1.6k浏览
创建 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()
    {
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引