public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // var serializedObject = property.serializedObject; var temp = (BindableObject)property.objectReferenceValue; if (temp == null) { return; } var tp = temp.GetType(); var prop = tp.GetProperty("target", BindingFlags.Public | BindingFlags.Instance); if (prop != null) { if (prop.GetValue(temp) == null) { prop.SetValue(temp, temp.GetComponent(prop.PropertyType)); } } var rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(34)); //selected bool isExpanded = property.isExpanded; var rect1 = rect; float w = rect.width; EditorGUI.HelpBox(rect, "", MessageType.None); rect1.height -= BindableObjectStyle.kExtraSpacing * 2; rect1.y += BindableObjectStyle.kExtraSpacing; rect1.x += BindableObjectStyle.kExtraSpacing; rect1.width = 26f; isExpanded = EditorGUI.Toggle(rect1, isExpanded); property.isExpanded = isExpanded; rect1.x = rect1.xMax + BindableObjectStyle.kExtraSpacing; rect1.width = w * .35f; CustomBinder customer = null; if (temp is CustomBinder) { customer = (CustomBinder)temp; EditorGUI.ObjectField(rect1, customer.target, typeof(Component), false); //显示绑定对象 } else { EditorGUI.ObjectField(rect1, temp, typeof(BindableObject), false); //显示绑定对象 } rect1.x = rect1.xMax + BindableObjectStyle.kExtraSpacing; rect1.width = w * .35f; if (customer != null) { propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, customer.target, propertyName); //绑定属性 } else { propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, temp, propertyName); //绑定属性 } rect1.x = rect1.xMax + BindableObjectStyle.kExtraSpacing; rect1.width = w * .2f - BindableObjectStyle.kExtraSpacing * 4; if (GUI.Button(rect1, "add")) { if (string.Equals(propertyName, BindableObjectStyle.FIRST_PROPERTY_NAME)) { Debug.LogWarningFormat("please choose a property to binding"); return; } Binding expression = new Binding(); expression.propertyName = propertyName; temp.AddBinding(expression); property.isExpanded = true; } EditorGUILayout.Separator(); EditorGUILayout.EndHorizontal(); if (isExpanded) { //显示列表 if (temSerialziedObject == null || temSerialziedObject.targetObject != temp) { temSerialziedObject = new SerializedObject(temp); } { var bindings = temSerialziedObject.FindProperty("bindings"); // serializedObject.targetObject if (bindings != null && bindings.isArray) { selectedList.Clear(); temSerialziedObject.Update(); var len = bindings.arraySize; SerializedProperty bindingProperty; for (int i = 0; i < len; i++) { bindingProperty = bindings.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(bindingProperty, true); if (bindingProperty.isExpanded) { selectedList.Add(i); } } rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(20)); rect.y += BindableObjectStyle.kExtraSpacing; float width; //删除数据 if (selectedList.Count > 0) { width = 102; rect.x = rect.xMax - width; rect.width = width; if (GUI.Button(rect, "del property " + selectedList.Count)) { selectedList.Sort((int a, int b) => { if (a < b) { return(1); } else if (a == b) { return(0); } else { return(-1); } }); foreach (var i in selectedList) { bindings.DeleteArrayElementAtIndex(i); } } EditorGUILayout.Separator(); } else { string DELETE_TIPS = BindableObjectStyle.PROPPERTY_CHOOSE_TIPS; width = DELETE_TIPS.Length * BindableObjectStyle.kExtraSpacing; rect.x = rect.xMax - width; rect.width = width; GUI.Box(rect, DELETE_TIPS); } temSerialziedObject.ApplyModifiedProperties(); EditorGUILayout.Separator(); EditorGUILayout.EndHorizontal(); } } } else { // GUILayout.Box("click checkbox to see detail or delete!"); } }
public override void OnInspectorGUI() { EditorGUILayout.Separator(); var temp = target as BindableObject; var tp = temp.GetType(); var prop = tp.GetProperty("target", BindingFlags.Public | BindingFlags.Instance); if (prop != null) { if (prop.GetValue(target) == null) { prop.SetValue(target, temp.GetComponent(prop.PropertyType)); } } base.OnInspectorGUI(); var rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(34)); var rect1 = rect; float w = rect.width; EditorGUI.HelpBox(rect, "", MessageType.None); rect1.height -= BindableObjectStyle.kExtraSpacing * 2; rect1.x += BindableObjectStyle.kExtraSpacing; rect1.y += BindableObjectStyle.kExtraSpacing; rect1.width = w * .4f; // CustomBinder customer = null; if (temp is CustomBinder) { customer = (CustomBinder)temp; EditorGUI.ObjectField(rect1, customer.target, typeof(Component), false); //显示绑定对象 } else { EditorGUI.ObjectField(rect1, temp, typeof(BindableObject), false); //显示绑定对象 } rect1.x = rect1.xMax + BindableObjectStyle.kExtraSpacing; rect1.width = w * .4f; if (customer != null) { propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, customer.target, propertyName); //绑定属性 } else { propertyName = BindalbeObjectUtilty.PopupComponentsProperty(rect1, temp, propertyName); //绑定属性 } rect1.x = rect1.xMax + BindableObjectStyle.kExtraSpacing; rect1.width = w * .2f - BindableObjectStyle.kExtraSpacing * 4; if (GUI.Button(rect1, "add")) { if (string.Equals(propertyName, BindableObjectStyle.FIRST_PROPERTY_NAME)) { Debug.LogWarningFormat("please choose a property to binding"); return; } Binding expression = new Binding(); expression.propertyName = propertyName; temp.AddBinding(expression); } EditorGUILayout.Separator(); EditorGUILayout.EndHorizontal(); //显示列表 if (bindings.isArray) { selectedList.Clear(); serializedObject.Update(); var len = temp.GetBindings().Count; SerializedProperty bindingProperty; for (int i = 0; i < len; i++) { bindingProperty = bindings.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(bindingProperty, true); if (bindingProperty.isExpanded) { selectedList.Add(i); } } //删除数据 if (selectedList.Count > 0) { rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(20)); rect.x = rect.xMax - 100; rect.width = 100; if (GUI.Button(rect, "del property " + selectedList.Count)) { selectedList.Sort((int a, int b) => { if (a < b) { return(1); } else if (a == b) { return(0); } else { return(-1); } }); foreach (var i in selectedList) { bindings.DeleteArrayElementAtIndex(i); } } EditorGUILayout.Separator(); EditorGUILayout.EndHorizontal(); } else { GUILayout.Box(BindableObjectStyle.PROPPERTY_CHOOSE_TIPS); } serializedObject.ApplyModifiedProperties(); } }