unity 输出类里所有值
时间: 2023-07-11 11:30:10 浏览: 37
如果您想要输出 Unity 类中所有的值,可以使用反射来实现。以下是一个示例代码,可以输出一个 GameObject 中所有的组件及其对应的字段和属性:
```csharp
using UnityEngine;
using System.Reflection;
public class PrintAllValues : MonoBehaviour
{
void Start()
{
Component[] components = gameObject.GetComponents<Component>();
foreach (Component component in components)
{
Debug.Log("Component: " + component.GetType().Name);
FieldInfo[] fields = component.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] properties = component.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
Debug.Log("Field: " + field.Name + " = " + field.GetValue(component));
}
foreach (PropertyInfo prop in properties)
{
Debug.Log("Property: " + prop.Name + " = " + prop.GetValue(component));
}
}
}
}
```
这段代码会遍历 GameObject 中的所有组件,然后输出每个组件中所有的字段和属性的值。请注意,这种方式可能会产生大量的输出,因此需要谨慎使用。