public static bool TryDrawInspectableObject(Rect position, SerializedProperty property, bool drawObjectReference = false, System.Func <Rect, GUIContent, InspectableObject, bool> elementHeaderHandler = null, System.Action <Rect, InspectableObject> elementFooterHandler = null) { var data = property.GetInspectableObjectData(); if (data != null) { var inspectableObject = InspectableObject.CreateInstance(property, data); if (inspectableObject != null) { var iterProp = inspectableObject.GetIterator(); var depth = iterProp.Depth; var index = 0; position.height = 0f; do { if (depth != iterProp.Depth || (index > 0 && !inspectableObject.IsExpanded)) { break; } var displayName = new GUIContent(iterProp.DisplayName); if (index == 0) { position.height = GetPropertyHeight(iterProp, GUIContent.none, iterProp.IsExpanded); PropertyField(position, iterProp, GUIContent.none, iterProp.IsExpanded); if (elementHeaderHandler != null) { elementHeaderHandler(position, displayName, inspectableObject); } else { TryDrawDefaultElementHeader(position, displayName, inspectableObject); } } position.yMin += position.height; EditorGUI.indentLevel++; position.height = GetObjectReferenceHeight(iterProp, drawObjectReference); if (TryDrawObjectReference(position, iterProp, displayName, drawObjectReference)) { } else if (index > 0) { position.height = GetPropertyHeight(iterProp, displayName, iterProp.IsExpanded); PropertyField(position, iterProp, displayName, iterProp.IsExpanded); } EditorGUI.indentLevel--; index++; } while (iterProp.NextVisible(false)); elementFooterHandler?.Invoke(position, inspectableObject); inspectableObject.ApplyModifiedProperties(); return(true); } } return(false); }
public static void PasteComponentAsNew(object userData) { InspectableObject inspectableObject = userData as InspectableObject; SerializedProperty serializedProperty = null; if (inspectableObject == null) { serializedProperty = userData as SerializedProperty; } else { serializedProperty = inspectableObject.SerializedObject.GetIterator(); serializedProperty.NextVisible(true); } do { if (serializedProperty.IsInspectableObjectDataArrayOrList()) { serializedProperty.arraySize++; serializedProperty.GetArrayElementAtIndex(serializedProperty.arraySize - 1).SetInspectableObjectData(datas[0]); break; } } while (serializedProperty.NextVisible(false)); serializedProperty.serializedObject.ApplyModifiedProperties(); }
public static void SetPrefabOverride(object userData) { InspectableProperty inspectableProperty = userData as InspectableProperty; if (inspectableProperty != null && inspectableProperty.Type != NullableStr) { #if UNITY_2018_2_OR_NEWER Object prefab = PrefabUtility.GetCorrespondingObjectFromSource(inspectableProperty.InspectableObject.SerializedObject.targetObject); #else Object prefab = PrefabUtility.GetPrefabParent(inspectableProperty.InspectableObject.SerializedObject.targetObject); #endif SerializedObject serializedObject = new SerializedObject(prefab); SerializedProperty serializedProperty = serializedObject.FindProperty(inspectableProperty.InspectableObject.Path); InspectableObject inspectableObject = InspectableObject.CreateInstance(serializedProperty); inspectableProperty.Value = inspectableObject.FindProperty(inspectableProperty.PropertyPath).Value; inspectableProperty.InspectableObject.ApplyModifiedProperties(); } else { InspectableObject inspectableObject = inspectableProperty == null ? userData as InspectableObject : inspectableProperty.InspectableObject; if (inspectableObject != null) { InspectableProperty prop = inspectableObject.GetIterator(); while (prop.Next(true)) { SetPrefabOverride(prop); } } else { SerializedProperty serializedProperty = userData as SerializedProperty; if (serializedProperty != null && serializedProperty.IsInspectableObjectDataArrayOrList()) { for (int i = 0; i < serializedProperty.arraySize; i++) { SerializedProperty prop = serializedProperty.GetArrayElementAtIndex(i); if (prop.IsInspectableObjectData()) { SetPrefabOverride(InspectableObject.CreateInstance(prop)); } } } } } }
public static InspectableObject CreateInstance(SerializedProperty serializedProperty, InspectableObjectData data) { if (data != null) { var o = data.CreateInstance(); if (o != null) { var inspectableObject = LoadFromCache(GetHashCode(serializedProperty, o)); if (inspectableObject == null) { inspectableObject = new InspectableObject(serializedProperty, o); } inspectableObject.SerializedObject = serializedProperty.serializedObject; return(inspectableObject); } } return(null); }
protected override bool DoElementHeader(Rect position, GUIContent label, InspectableObject inspectableObject) { EditorGUI.BeginChangeCheck(); var property = inspectableObject.FindProperty(IsOn); var boolReactive = (BoolReactiveProperty)property.Value; var togglePosition = new Rect(position); togglePosition.xMin -= 8; togglePosition.width = 15; var isOn = EditorGUI.Toggle(togglePosition, boolReactive.Value); if (EditorGUI.EndChangeCheck()) { boolReactive.Value = isOn; property.Value = boolReactive; inspectableObject.ApplyModifiedProperties(); } EditorGUI.indentLevel++; return(base.DoElementHeader(position, label, inspectableObject)); }
public static float GetInspectableObjectHeight(SerializedProperty property, bool drawObjectReference = false) { var height = 0f; var height2 = 0f; var data = property.GetInspectableObjectData(); if (data != null) { var inspectableObject = InspectableObject.CreateInstance(property, data); if (inspectableObject != null) { var iterProp = inspectableObject.GetIterator(); int depth = iterProp.Depth; do { if (depth != iterProp.Depth) { break; } height2 = GetObjectReferenceHeight(iterProp, drawObjectReference); if (height2 == 0f) { height += GetPropertyHeight(iterProp, new GUIContent(iterProp.DisplayName), iterProp.IsExpanded); } else { height += height2; } if (!inspectableObject.IsExpanded) { break; } } while (iterProp.NextVisible(false)); } else { height += EditorGUIUtility.singleLineHeight; } } return(height); }
protected override void DoElementFooter(Rect position, InspectableObject inspectableObject) { EditorGUI.indentLevel--; }
protected virtual void DoElementFooter(Rect position, InspectableObject inspectableObject) { }
protected virtual bool DoElementHeader(Rect position, GUIContent label, InspectableObject inspectableObject) { return(EasyGUI.TryDrawDefaultElementHeader(position, label, inspectableObject)); }
public InspectablePropertyBase(InspectableObject inspectableObject, FieldInfo propertyField, System.Type type, string propertyPath, string name, int depth, System.Func <object> getValueCallback, System.Action <object> setValueCallback) { Editable = true; InspectableObject = inspectableObject; PropertyType = type; PropertyPath = propertyPath; Name = name; DisplayName = name; DisplayName = Regex.Replace(DisplayName, UppercasePattern, match => { var str = match.ToString(); return(char.ToUpperInvariant(str[0]) + str.Substring(1)); }); DisplayName = Regex.Replace(DisplayName, SeparatePattern, match => { var str = match.ToString(); return(str + SpaceStr); }); Depth = depth; Type = type.ToString(); this.GetValueCallback += getValueCallback; this.SetValueCallback += setValueCallback; if (propertyField != null) { var attrs = propertyField.GetCustomAttributes(typeof(FixedBufferAttribute), false); if (attrs != null && attrs.Length > 0) { var attr = (FixedBufferAttribute)attrs[0]; var property = new InspectablePropertyBase(InspectableObject, null, InspectablePropertyType.FixedBufferSize, string.Format(CombinePathLayout, PropertyPath, SizeStr), SizeStr, Depth + 1, () => { return(attr.Length); }, null); IsFixedBuffer = true; property.Editable = false; property.IsFixedBuffer = true; Properties = new List <InspectablePropertyBase>() { property }; VisibleProperties = new List <InspectablePropertyBase>() { property }; var elementType = attr.ElementType; for (int i = 0; i < attr.Length; i++) { var index = i; var elementName = string.Format(CombineNameLayout, ElementStr, i); var element = new InspectablePropertyBase(InspectableObject, null, elementType, string.Format(CombinePathLayout, PropertyPath, elementName), elementName, Depth + 1, () => { return(FixedBufferUtility.GetValue(Value, attr, index)); }, (val) => { Value = FixedBufferUtility.SetValue(Value, PropertyType, attr, index, val); }); Properties.Add(element); VisibleProperties.Add(element); } } } if (!IsFixedBuffer && PropertyType.HasChildren()) { var fields = PropertyType.GetAllInstanceFieldsFromCached(); if (fields != null) { Properties = new List <InspectablePropertyBase>(); foreach (var field in fields) { var property = new InspectablePropertyBase(InspectableObject, field, field.FieldType, string.Format(CombinePathLayout, PropertyPath, field.Name), field.Name, Depth + 1, () => { return(field.GetValue(Value)); }, (val) => { var obj = Value; field.SetValue(obj, val); Value = obj; }); Properties.Add(property); if (field.IsVisible()) { if (VisibleProperties == null) { VisibleProperties = new List <InspectablePropertyBase>(); } VisibleProperties.Add(property); } } } } if (IsArray) { ArraySizeChanged += OnArraySizeChanged; var property = new InspectablePropertyBase(InspectableObject, null, InspectablePropertyType.ArraySize, string.Format(CombinePathLayout, PropertyPath, SizeStr), SizeStr, Depth + 1, () => { return((int)getCountMethod.Invoke(Value, null)); }, (val) => { ArraySizeChanged.Invoke((int)val); }); property.ArraySizeChanged += OnArraySizeChanged; foreach (var method in PropertyType.GetMethods()) { if (PropertyType.IsArray) { if (method.Name.ToLower() == Get_LengthStr) { getCountMethod = method; property.getCountMethod = method; } else if (method.Name.ToLower() == GetStr) { getMethod = method; property.getMethod = method; } else if (method.Name.ToLower() == SetStr) { setMethod = method; property.setMethod = method; } } else { if (method.Name.ToLower() == Get_CountStr) { getCountMethod = method; property.getCountMethod = method; } else if (method.Name.ToLower() == Get_ItemStr) { getMethod = method; property.getMethod = method; } else if (method.Name.ToLower() == Set_ItemStr) { setMethod = method; property.setMethod = method; } else if (method.Name.ToLower() == AddStr) { addMethod = method; property.addMethod = method; } else if (method.Name.ToLower() == RemoveRangeStr) { removerangeMethod = method; property.removerangeMethod = method; } } } Properties = new List <InspectablePropertyBase>() { property }; VisibleProperties = new List <InspectablePropertyBase>() { property }; ApplyModifiedArray(); } }