Skip to content

Instantly share code, notes, and snippets.

@Muchaszewski
Created February 28, 2022 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muchaszewski/a243eea4b8b05da16b7c212e6d96e257 to your computer and use it in GitHub Desktop.
Save Muchaszewski/a243eea4b8b05da16b7c212e6d96e257 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
#if UNITY_EDITOR
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
#endif
namespace OdinExtensions
{
/// <summary>
/// Allows for foldout element to remain and put any inline properties next to it
/// </summary>
public class InlinePartialAttribute : Attribute
{
public readonly bool ExpandToVertical;
/// <summary>
/// Allows for foldout element to remain and put any inline properties next to it
/// </summary>
/// <param name="expandToVertical">Should all items be put in horizontal or vertical orientation</param>
public InlinePartialAttribute(bool expandToVertical = false)
{
ExpandToVertical = expandToVertical;
}
}
/// <summary>
/// Should given field that is showed as inline, be also duplicated into foldout menu
/// </summary>
public class ShowInFoldoutEditorAttribute : Attribute
{
/// <summary>
/// Should given field that is showed as inline, be also duplicated into foldout menu
/// </summary>
public ShowInFoldoutEditorAttribute()
{
}
}
#if UNITY_EDITOR
[DrawerPriority(0, 95)]
public class InlinePartialAttributeDrawer : OdinAttributeDrawer<InlinePartialAttribute>
{
private bool _expanded;
protected override void DrawPropertyLayout(GUIContent label)
{
EditorGUILayout.BeginHorizontal();
_expanded = SirenixEditorGUI.Foldout(_expanded, label);
if (Attribute.ExpandToVertical)
{
EditorGUILayout.BeginVertical();
}
foreach (var child in Property.Children)
{
if (child.GetAttribute<InlinePropertyAttribute>() != null)
{
child.Draw();
}
}
if (Attribute.ExpandToVertical)
{
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndHorizontal();
if (_expanded)
{
EditorGUI.indentLevel++;
foreach (var child in Property.Children)
{
if (child.GetAttribute<InlinePropertyAttribute>() == null || child.GetAttribute<ShowInFoldoutEditorAttribute>() != null)
{
child.Draw();
}
}
EditorGUI.indentLevel--;
}
}
}
#endif
}
@Muchaszewski
Copy link
Author

Odin extension that allows for the foldout element to remain and put any inline properties next to it

image

    [Serializable]
    [InlinePartial]
    public class JsonSprite
    {
        [ReadOnly]
        public string ImagePath;

        public string Test;

        [HideLabel]
        [InlineProperty]
        [ShowInFoldoutEditorAttribute]
        public string Test2;
                
#if UNITY_EDITOR
        [DoNotSerialize]
        private Sprite _image;
        
        [HideLabel]
        [ShowInInspector]
        [InlineProperty]
        public Sprite Image
        {
            get
            {
                if (_image != null)
                {
                    var asset = AssetDatabase.GetAssetPath(_image);
                    ImagePath = asset;
                }
                else
                {
                    ImagePath = null;
                }
                return _image;
            }
            set
            {
                _image = value;
            }
        }
#endif
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment