Skip to content

Instantly share code, notes, and snippets.

@AquaGeneral
Created May 16, 2018 11:45
Show Gist options
  • Select an option

  • Save AquaGeneral/89da07376d9b5bc8bad3ad50a306706e to your computer and use it in GitHub Desktop.

Select an option

Save AquaGeneral/89da07376d9b5bc8bad3ad50a306706e to your computer and use it in GitHub Desktop.
See all of the values that Unity's SystemInfo class contains. Reflection is used to remove redundant work, and simultaneously makes this work in any version of Unity (I hope). A lot of work can be done to make it look nicer.
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class SystemInfoInspector : EditorWindow {
private static PropertyInfo[] properties;
private static MethodInfo[] methods;
private Vector2 scrollPosition;
static SystemInfoInspector() {
properties = typeof(SystemInfo).GetProperties(BindingFlags.Static | BindingFlags.Public);
List<MethodInfo> methodInfo = new List<MethodInfo>();
foreach(MethodInfo mi in typeof(SystemInfo).GetMethods(BindingFlags.Static | BindingFlags.Public)) {
ParameterInfo[] pi = mi.GetParameters();
if(pi.Length != 1 || pi[0].ParameterType.IsEnum == false) continue;
methodInfo.Add(mi);
}
methods = methodInfo.ToArray();
}
[MenuItem("Window/SystemInfo Inspector")]
private static void Init() {
GetWindow<SystemInfoInspector>();
}
private void OnGUI() {
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
foreach(var prop in properties) {
Type propertyType = prop.PropertyType;
if(propertyType.IsEnum) {
EditorGUILayout.EnumPopup(prop.Name, (Enum)prop.GetValue(null, null));
} else if(propertyType == typeof(bool)) {
EditorGUILayout.Toggle(prop.Name, (bool)prop.GetValue(null, null));
} else if(propertyType == typeof(float)) {
EditorGUILayout.FloatField(prop.Name, (float)prop.GetValue(null, null));
} else if(propertyType == typeof(int)) {
EditorGUILayout.IntField(prop.Name, (int)prop.GetValue(null, null));
} else if(propertyType == typeof(string)) {
EditorGUILayout.LabelField(prop.Name, (string)prop.GetValue(null, null));
}
}
foreach(var method in methods) {
EditorGUILayout.LabelField(method.Name, EditorStyles.boldLabel);
EditorGUI.indentLevel = 1;
Type type = method.GetParameters()[0].ParameterType;
string[] names = Enum.GetNames(type);
Array vals = Enum.GetValues(type);
int i = 0;
foreach(object v in vals) {
if((int)v < 0) continue; // HACK: This fixes invoking the method with obsolete values
EditorGUILayout.LabelField(names[i], method.Invoke(null, new object[] { v }).ToString());
i++;
}
EditorGUI.indentLevel = 0;
}
EditorGUILayout.EndScrollView();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment