Created
February 12, 2021 05:01
-
-
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; | |
[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 SizedFoldoutGroupAttribute(string groupName, float order = 0) | |
: base(groupName, order) | |
{ | |
} | |
public SizedFoldoutGroupAttribute(string groupName, float r, float g, float b, float a = 1f, float order = 0) | |
: base(groupName, order) | |
{ | |
this.R = r; | |
this.G = g; | |
this.B = b; | |
this.A = a; | |
HasDefinedColor = true; | |
} | |
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 Sirenix.OdinInspector.Editor.ValueResolvers; | |
public class SizedFoldoutGroupAttributeDrawer : OdinGroupDrawer<SizedFoldoutGroupAttribute> | |
{ | |
private ValueResolver<string> titleGetter; | |
private ValueResolver<Color> colorGetter; | |
protected override void Initialize() | |
{ | |
this.titleGetter = ValueResolver.GetForString(this.Property, this.Attribute.GroupName); | |
if (this.Attribute.HasDefinedExpanded) | |
{ | |
this.Property.State.Expanded = this.Attribute.Expanded; | |
} | |
} | |
protected override void DrawPropertyLayout(GUIContent label) | |
{ | |
var property = this.Property; | |
var attribute = this.Attribute; | |
if (this.titleGetter.HasError) | |
{ | |
SirenixEditorGUI.ErrorMessageBox(this.titleGetter.ErrorMessage); | |
} | |
if (this.Attribute.HasDefinedColor) | |
{ | |
GUIHelper.PushColor(new Color(this.Attribute.R, this.Attribute.G, this.Attribute.B, this.Attribute.A)); | |
} | |
SirenixEditorGUI.BeginBox(); | |
SirenixEditorGUI.BeginBoxHeader(); | |
if (this.Attribute.HasDefinedColor) | |
{ | |
GUIHelper.PopColor(); | |
} | |
GUIStyle s = new GUIStyle(SirenixGUIStyles.Foldout); | |
s.richText = true; | |
var content = GUIHelper.TempContent(this.titleGetter.HasError ? property.Label.text : this.titleGetter.GetValue()); | |
s.fixedWidth = 450; | |
s.wordWrap = true; | |
s.alignment = TextAnchor.UpperLeft; | |
var height = s.CalcHeight(content, 450); | |
GUILayout.BeginVertical(); | |
switch(height) | |
{ | |
default: | |
GUILayout.Space((height / 2) - 10); | |
break; | |
case 45: | |
GUILayout.Space((height / 2) - 18); | |
break; | |
case 15: | |
GUILayout.Space((height / 2)); | |
break; | |
} | |
this.Property.State.Expanded = SirenixEditorGUI.Foldout(this.Property.State.Expanded, content, s); | |
switch (height) | |
{ | |
default: | |
GUILayout.Space((height / 2)); | |
break; | |
case 45: | |
GUILayout.Space((height / 2) + 8); | |
break; | |
} | |
GUILayout.EndVertical(); | |
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(); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: