public object OnGUI(string label, object value, System.Type type, out bool isDirty) { var current = value; if (current != null) { var isFoldout = GetFoldout(value); try { UnityEditor.EditorGUILayout.BeginHorizontal(); isFoldout = UnityEditor.EditorGUILayout.Foldout(isFoldout, label); if (copyRoot != null && GUILayout.Button("复制初始化")) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); GameObject root = copyRoot.GetValue(current) as GameObject; if (root != null) { sb.AppendLine("var rt = root.transform;"); sb.AppendLine("Transform rtm = null;"); foreach (var d in infos) { if (d.Name == "root") { continue; } if (isInherited(d.FieldType, typeof(Component))) { Component v = (Component)d.GetValue(current); if (v != null) { if (d.FieldType == typeof(Transform)) { sb.AppendFormat("{0} = (rtm = rt.Find(\"{0}\")) == null ? null : rtm;", d.Name, GetPath(root, v.gameObject)); } else if (d.FieldType == typeof(RectTransform)) { sb.AppendFormat("{0} = (rtm = rt.Find(\"{0}\")) == null ? null : (RectTransform)rtm;", d.Name, GetPath(root, v.gameObject)); } else { sb.AppendFormat("{0} = (rtm = rt.Find(\"{1}\")) == null ? null : rtm.GetComponent<{2}>();", d.Name, GetPath(root, v.gameObject), d.FieldType.Name); } sb.AppendLine(); } } else if (d.FieldType == typeof(GameObject)) { GameObject v = (GameObject)d.GetValue(current); if (v != null) { sb.AppendFormat("{0} = (rtm = rt.Find(\"{1}\")) == null ? null : rtm.gameObject;", d.Name, GetPath(root, v.gameObject), d.FieldType.Name); sb.AppendLine(); } } } GUIUtility.systemCopyBuffer = sb.ToString(); wxb.L.Log(sb.ToString()); } } } finally { UnityEditor.EditorGUILayout.EndHorizontal(); } SetFoldout(value, isFoldout); if (!isFoldout) { isDirty = false; return(false); } } isDirty = false; if (value == null) { value = IL.Help.Create(type); isDirty = true; } for (int i = 0; i < infos.Count; ++i) { var field = infos[i]; object v = null; try { v = field.GetValue(value); } catch (System.Exception ex) { wxb.L.LogException(ex); } if (v == null && (!IL.Help.isType(field.FieldType, typeof(Object)))) { v = IL.Help.Create(field.FieldType); field.SetValue(value, v); isDirty = true; } isDirty |= TypeEditor.Get(field.FieldType, field).OnGUI(value, field); } return(value); }
bool ShowTypeSelect(ref object value, string label, string key) { bool isDirty = false; var allTypes = IL.Help.GetBaseType(baseType.FullName); { for (int i = allTypes.Count - 1; i >= 0; --i) { var at = allTypes[i]; if (at.IsAbstract || !at.IsSerializable) { allTypes.RemoveAt(i); } } allTypes.Insert(0, null); } bool isSet = false; string newTypename; using (new GUIColor(Color.cyan)) { var typeName = GetKey(key); if (string.IsNullOrEmpty(typeName) && value != null) { typeName = IL.Help.GetInstanceType(value).FullName; } if (value != null) { UnityEditor.EditorGUILayout.LabelField("当前类型:" + IL.Help.GetInstanceType(value).FullName); } UnityEditor.EditorGUILayout.BeginHorizontal(); newTypename = IL.Editor.ILMonoEditor.StringPopupT($"设置{label}类型", typeName, allTypes, (System.Type t) => { return(t == null ? "null" : t.FullName); }, ""); SetKey(key, newTypename); if (newTypename == "null") { newTypename = string.Empty; } isSet = GUILayout.Button("设置"); UnityEditor.EditorGUILayout.EndHorizontal(); } if (isSet) { var newInstance = string.IsNullOrEmpty(newTypename) ? null : IL.Help.Create(IL.Help.GetTypeByFullName(newTypename)); if (value != null && newInstance != null) { // 赋值下 SetValue(newInstance, value); } if (newInstance != null) { var te = TypeEditor.Get(IL.Help.GetInstanceType(newInstance), null); if (te is AnyType) { ((AnyType)te).SetFoldout(newInstance, true); } } value = newInstance; isDirty = true; } return(isDirty); }