public void OnGUI(string newTypename) { string typeName = (string)typeNameField.GetValue(target); if (newTypename != typeName) { RegisterCompleteObjectUndo(); typeName = newTypename; typeNameField.SetValue(target, typeName); instanceField.SetValue(target, null); EditorUtility.SetDirty(target); } if (string.IsNullOrEmpty(typeName)) { return; } var type = Help.GetType(newTypename); if (type == null) { EditorGUILayout.LabelField(string.Format("类型:{0},查找失败!", newTypename)); return; } object instance = instanceField.GetValue(target); if (instance == null) { return; } if (TypeEditor.OnGUI(instance)) { EditorUtility.SetDirty(target); RegisterCompleteObjectUndo(); } }
public object OnGUI(string label, object value, System.Type type, out bool isDirty) { var current = value; if (current != null) { int hashcode = current.GetHashCode(); var isFoldout = false; if (!isFoldouts.TryGetValue(hashcode, out isFoldout)) { isFoldouts.Add(hashcode, isFoldout); } 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(); Debug.Log(sb.ToString()); } } } finally { UnityEditor.EditorGUILayout.EndHorizontal(); } isFoldouts[hashcode] = isFoldout; if (!isFoldout) { isDirty = false; return(false); } } isDirty = false; if (value == null) { value = 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) { Debug.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); }