Last active
July 7, 2024 14:30
-
-
Save digiwombat/27616c85563a386001997ca8e91bc9dc to your computer and use it in GitHub Desktop.
SizedFoldoutGroup Attribute for Odin Inspector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sirenix.OdinInspector | |
{ | |
#pragma warning disable | |
using System; | |
using UnityEngine; | |
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)] | |
[System.Diagnostics.Conditional("UNITY_EDITOR")] | |
public class SizedFoldoutGroupAttribute : PropertyGroupAttribute | |
{ | |
private bool expanded; | |
public bool Expanded | |
{ | |
get { return this.expanded; } | |
set | |
{ | |
this.expanded = value; | |
this.HasDefinedExpanded = true; | |
} | |
} | |
public bool HasDefinedExpanded { get; private set; } | |
public bool HasDefinedColor = false; | |
public float R, G, B, A; | |
public float Padding; | |
public SizedFoldoutGroupAttribute(string groupName, float order = 0) | |
: base(groupName, order) | |
{ | |
} | |
public SizedFoldoutGroupAttribute(string groupName, string hexColor, float order = 0, float padding = 0) | |
: base(groupName, order) | |
{ | |
Color color = GameData.Colors.FromHex(hexColor); | |
this.R = color.r; | |
this.G = color.g; | |
this.B = color.b; | |
this.A = color.a; | |
HasDefinedColor = true; | |
this.Padding = padding; | |
} | |
public SizedFoldoutGroupAttribute(string groupName, float r, float g, float b, float a = 1f, float order = 0, float padding = 0) | |
: base(groupName, order) | |
{ | |
this.R = r; | |
this.G = g; | |
this.B = b; | |
this.A = a; | |
HasDefinedColor = true; | |
this.Padding = padding; | |
} | |
public SizedFoldoutGroupAttribute(string groupName, bool expanded, float order = 0) | |
: base(groupName, order) | |
{ | |
this.expanded = expanded; | |
this.HasDefinedExpanded = true; | |
} | |
protected override void CombineValuesWith(PropertyGroupAttribute other) | |
{ | |
var attr = other as SizedFoldoutGroupAttribute; | |
if (attr.HasDefinedExpanded) | |
{ | |
this.HasDefinedExpanded = true; | |
this.Expanded = attr.Expanded; | |
} | |
if (this.HasDefinedExpanded) | |
{ | |
attr.HasDefinedExpanded = true; | |
attr.Expanded = this.Expanded; | |
} | |
this.R = Math.Max(attr.R, this.R); | |
this.G = Math.Max(attr.G, this.G); | |
this.B = Math.Max(attr.B, this.B); | |
this.A = Math.Max(attr.A, this.A); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
namespace Sirenix.OdinInspector.Editor.Drawers | |
{ | |
#pragma warning disable | |
using Utilities.Editor; | |
using UnityEngine; | |
using UnityEditor; | |
using Sirenix.Utilities; | |
using Sirenix.OdinInspector.Editor; | |
using Sirenix.OdinInspector.Editor.ValueResolvers; | |
public class SizedFoldoutGroupAttributeDrawer : OdinGroupDrawer<SizedFoldoutGroupAttribute> | |
{ | |
private ValueResolver<string> titleGetter; | |
private ValueResolver<Color> colorGetter; | |
private Color _color = Color.gray; | |
protected override void Initialize() | |
{ | |
this.titleGetter = ValueResolver.GetForString(this.Property, this.Attribute.GroupName); | |
if (this.Attribute.HasDefinedExpanded) | |
{ | |
this.Property.State.Expanded = this.Attribute.Expanded; | |
} | |
if (this.Attribute.HasDefinedColor) | |
{ | |
_color = new Color(this.Attribute.R, this.Attribute.G, this.Attribute.B, this.Attribute.A); | |
} | |
else | |
{ | |
_color = Color.gray; | |
} | |
} | |
protected override void DrawPropertyLayout(GUIContent label) | |
{ | |
var property = this.Property; | |
var attribute = this.Attribute; | |
if (this.titleGetter.HasError) | |
{ | |
SirenixEditorGUI.ErrorMessageBox(this.titleGetter.ErrorMessage); | |
} | |
GUIHelper.PushColor(_color); | |
SirenixEditorGUI.BeginBox(); | |
SirenixEditorGUI.BeginBoxHeader(); | |
GUIHelper.PopColor(); | |
var content = GUIHelper.TempContent(this.titleGetter.HasError ? property.Label.text : this.titleGetter.GetValue()); | |
GUIStyle s = new GUIStyle(SirenixGUIStyles.Foldout); | |
s.richText = true; | |
s.fixedWidth = s.CalcWidth(content); | |
s.wordWrap = true; | |
s.stretchHeight = true; | |
s.alignment = TextAnchor.UpperLeft; | |
float height = s.CalcHeight(content, s.fixedWidth); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(attribute.Padding); | |
} | |
GUILayout.BeginVertical(); | |
GUILayout.Space(4); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(attribute.Padding); | |
} | |
EditorGUILayout.BeginVertical(GUILayout.Height(height + 4)); | |
this.Property.State.Expanded = SirenixEditorGUI.Foldout(this.Property.State.Expanded, content, s); | |
EditorGUILayout.EndVertical(); | |
//GUILayout.Space(height/2); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(attribute.Padding); | |
} | |
GUILayout.EndVertical(); | |
if (this.Attribute.Padding > 0) | |
{ | |
GUILayout.Space(attribute.Padding); | |
} | |
SirenixEditorGUI.EndBoxHeader(); | |
if (SirenixEditorGUI.BeginFadeGroup(this, this.Property.State.Expanded)) | |
{ | |
for (int i = 0; i < property.Children.Count; i++) | |
{ | |
var child = property.Children[i]; | |
child.Draw(child.Label); | |
} | |
} | |
SirenixEditorGUI.EndFadeGroup(); | |
SirenixEditorGUI.EndBox(); | |
EditorGUI.DrawRect( | |
Property.LastDrawnValueRect.SetWidth(3), | |
_color); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: