Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EmbeddedInspectorAttribute
/*The MIT License (MIT)
Copyright (c) 2016 Edward Rowe (@edwardlrowe)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace RedBlueGames.Tools
{
using System;
using UnityEngine;
/// <summary>
/// Attribute that marks a field to draw the contents of its object reference as an embedded custom inspector.
/// </summary>
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public class EmbeddedInspectorAttribute : PropertyAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RedBlueGames.Tools.EmbeddedInspectorAttribute"/> class.
/// </summary>
/// <param name="isEditable">If set to <c>true</c> the embedded custom inspector is editable.</param>
public EmbeddedInspectorAttribute(bool isEditable = false)
{
this.IsEditable = isEditable;
}
/// <summary>
/// Gets or sets a value indicating whether this instance is editable.
/// </summary>
/// <value><c>true</c> if this instance is editable; otherwise, <c>false</c>.</value>
public bool IsEditable { get; set; }
}
}
/*The MIT License (MIT)
Copyright (c) 2016 Edward Rowe (@edwardlrowe)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace RedBlueGames.Tools
{
using UnityEditor;
using UnityEngine;
/// <summary>
/// Embedded inspector attribute drawer.
/// </summary>
[CustomPropertyDrawer(typeof(EmbeddedInspectorAttribute))]
public class EmbeddedInspectorAttributeDrawer : PropertyDrawer
{
private bool isExpanded;
/// <summary>
/// Gets the height of the property.
/// </summary>
/// <returns>The property height.</returns>
/// <param name="property">Property to draw.</param>
/// <param name="label">Label for the property.</param>
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
{
return EditorGUIUtility.singleLineHeight;
}
UnityEngine.Object objectReference = property.objectReferenceValue;
if (objectReference != null && this.isExpanded)
{
return EditorGUIUtility.singleLineHeight;
}
else
{
return EditorGUI.GetPropertyHeight(property, label);
}
}
/// <summary>
/// Draws the Property
/// </summary>
/// <param name="position">Position of the property.</param>
/// <param name="property">Property as serialized data.</param>
/// <param name="label">Label for the property.</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
{
EditorGUI.LabelField(position, label.text, "Use EmbeddedInspectorAttribute only on ObjectReferenceTypes");
return;
}
label = EditorGUI.BeginProperty(position, label, property);
if (property.objectReferenceValue != null)
{
float foldoutWidth = 5.0f;
Rect foldoutPosition = new Rect(position.x, position.y, foldoutWidth, EditorGUIUtility.singleLineHeight);
this.isExpanded = EditorGUI.Foldout(foldoutPosition, this.isExpanded, string.Empty);
Rect objectFieldPosition = new Rect(position.x + foldoutWidth, position.y, position.width - foldoutWidth, EditorGUIUtility.singleLineHeight);
EditorGUI.PropertyField(objectFieldPosition, property);
// If Property is cleared out while rendering, we are done
if (property.objectReferenceValue != null && this.isExpanded)
{
var attribute = (EmbeddedInspectorAttribute)this.attribute;
GUI.enabled = attribute.IsEditable;
EditorGUI.indentLevel++;
var editor = Editor.CreateEditor(property.objectReferenceValue);
editor.OnInspectorGUI();
EditorGUI.indentLevel--;
GUI.enabled = true;
}
}
else
{
EditorGUI.PropertyField(position, property, label, true);
}
EditorGUI.EndProperty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.