Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Editor.Extras/Drawers/InlineButtonDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Reflection;
using TriInspector;
using TriInspector.Drawers;
using UnityEngine;

[assembly: RegisterTriAttributeDrawer(typeof(InlineButtonDrawer), TriDrawerOrder.Drawer)]

namespace TriInspector.Drawers
{
public class InlineButtonDrawer : TriAttributeDrawer<InlineButtonAttribute>
{
private const float MinButtonWidth = 28f;
private const float ButtonSpacing = 4f;
private const float ButtonPadding = 12f;
private const float MaxButtonWidthRatio = 0.25f;

public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (string.IsNullOrEmpty(Attribute.Name))
{
return "[InlineButton] method name cannot be empty";
}

return TriExtensionInitializationResult.Ok;
}

public override float GetHeight(float width, TriProperty property, TriElement next)
{
return next.GetHeight(width);
}

public override void OnGUI(Rect position, TriProperty property, TriElement next)
{
var buttonText = !string.IsNullOrEmpty(Attribute.ButtonLabel)
? Attribute.ButtonLabel
: Attribute.Name;

var buttonContent = new GUIContent(buttonText);
var maxButtonWidth = position.width * MaxButtonWidthRatio;
var buttonWidth = Attribute.ButtonWidth > 0f
? Attribute.ButtonWidth
: Mathf.Clamp(GUI.skin.button.CalcSize(buttonContent).x + ButtonPadding, MinButtonWidth, maxButtonWidth);

var propertyRect = new Rect(position)
{
width = position.width - buttonWidth - ButtonSpacing,
};

var buttonRect = new Rect(position)
{
x = position.x + position.width - buttonWidth,
width = buttonWidth,
};

next.OnGUI(propertyRect);

if (GUI.Button(buttonRect, buttonContent))
{
InvokeMethod(property);
}
}

private void InvokeMethod(TriProperty property)
{
var methodName = Attribute.Name;

property.ModifyAndRecordForUndo(targetIndex =>
{
try
{
var parentValue = property.Parent.GetValue(targetIndex);
var targetType = parentValue?.GetType() ?? property.Parent.FieldType;

var methodInfo = targetType.GetMethod(
methodName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

if (methodInfo == null)
{
Debug.LogError($"[InlineButton] Method '{methodName}' not found on type '{targetType.Name}'");
return;
}

var parameters = methodInfo.GetParameters();
if (parameters.Length > 0)
{
Debug.LogError($"[InlineButton] Method '{methodName}' must have no parameters");
return;
}

methodInfo.Invoke(parentValue, null);
}
catch (Exception e)
{
Debug.LogException(e);
}
});
}
}
}
3 changes: 3 additions & 0 deletions Editor.Extras/Drawers/InlineButtonDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Editor.Samples/Buttons/Buttons_InlineButtonSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnityEngine;

namespace TriInspector.Editor.Samples.Buttons
{
public class Buttons_InlineButtonSample : ScriptableObject
{
[InlineButton("click add age",nameof(Add))]
public int age;

void Add()
{
age++;
Debug.Log(age);
}
}
}
3 changes: 3 additions & 0 deletions Editor.Samples/Buttons/Buttons_InlineButtonSample.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Runtime/Attributes/Buttons/InlineButtonAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;

namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property)]
[Conditional("UNITY_EDITOR")]
public class InlineButtonAttribute : Attribute
{
public InlineButtonAttribute(string name)
{
Name = name;
}
public InlineButtonAttribute(string label,string name):this(name)
{
ButtonLabel = label;
}

public string Name { get; set; }

/// <summary>
/// Optional custom label for the button. If not set, the method name is used.
/// </summary>
public string ButtonLabel { get; set; }

/// <summary>
/// Width of the inline button in pixels. Default is 60.
/// </summary>
public float ButtonWidth { get; set; }
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/Buttons/InlineButtonAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.