private void OnEnable() { Bind = target as UIBind; SerializedValue = serializedObject.FindProperty("Value"); Type type = Bind.GetWidgetClass(); if (type == null) { return; } NodeOptions.Clear(); var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < fields.Length; ++i) { var filed = fields[i]; object[] fieldAttr = filed.GetCustomAttributes(typeof(UiNameAttribute), true); if (fieldAttr.Length == 0) { continue; } UiNameAttribute attr = fieldAttr[0] as UiNameAttribute; string bindkey = string.IsNullOrEmpty(attr.Description) ? filed.Name : attr.Description; NodeOptions.Add(bindkey); } }
void InitData() { Type t = GetType(); do { FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { var fieldAttr = fields[i].GetCustomAttributes(typeof(UiNameAttribute), true); var fieldAttrInit = fields[i].GetCustomAttributes(typeof(UiInitAttribute), true); if (fieldAttr.Length > 0) { UiNameAttribute attr = fieldAttr[0] as UiNameAttribute; string bindkey = string.IsNullOrEmpty(attr.Description) ? fields[i].Name : attr.Description; object widget = FindBindingWidget(bindkey, fields[i].FieldType); if (widget != null) { fields[i].SetValue(this, widget); if (!Binds.ContainsKey(bindkey)) { Binds.Add(bindkey, widget); } } else { if (fieldAttrInit.Length == 0) { Debug.LogError(string.Format("Class = {0}", t.Name)); Debug.LogError(string.Format("[{0}]没有绑定!Type = {1}! Name = {2}", attr.Description, fields[i].FieldType.Name, fields[i].Name)); Debug.LogError(string.Format(GetPath(transform, ""))); } UiInitAttribute attrInit = fieldAttrInit[0] as UiInitAttribute; if (fields[i].FieldType.BaseType == typeof(Array)) { fields[i].SetValue(this, FindUiWidgets(attrInit.Description, fields[i].FieldType)); } else { fields[i].SetValue(this, FindUiWidget(attrInit.Description, fields[i].FieldType)); } } } else if (fieldAttrInit.Length > 0) { UiInitAttribute attrInit = fieldAttrInit[0] as UiInitAttribute; if (fields[i].FieldType.BaseType == typeof(Array)) { fields[i].SetValue(this, FindUiWidgets(attrInit.Description, fields[i].FieldType)); } else { fields[i].SetValue(this, FindUiWidget(attrInit.Description, fields[i].FieldType)); } } } t = t.BaseType; }while (t != typeof(UiBase) && t != typeof(Widget)); }