Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Variable Label (VarLabel) #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class VarLabelAttribute : LabelAttribute
{
public VarLabelAttribute(string label) : base(label)
{
}
}
}

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

37 changes: 34 additions & 3 deletions Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ public static T[] GetAttributes<T>(SerializedProperty property) where T : class
public static GUIContent GetLabel(SerializedProperty property)
{
LabelAttribute labelAttribute = GetAttribute<LabelAttribute>(property);
string labelText = (labelAttribute == null)
? property.displayName
: labelAttribute.Label;
string labelText = property.displayName;

if(labelAttribute != null){
labelText = labelAttribute.Label;

if(labelAttribute is VarLabelAttribute) {
object target = GetTargetObjectWithProperty(property);

labelText = GetVariableLabel(target, ((VarLabelAttribute)labelAttribute).Label);
}
}

GUIContent label = new GUIContent(labelText);
return label;
Expand Down Expand Up @@ -224,6 +232,29 @@ internal static List<bool> GetConditionValues(object target, string[] conditions
return conditionValues;
}

internal static string GetVariableLabel(object target, string path) {
FieldInfo labelField = ReflectionUtility.GetField(target, path);
if(labelField != null &&
labelField.FieldType == typeof(string)) {
return (string)labelField.GetValue(target);
}

PropertyInfo labelProperty = ReflectionUtility.GetProperty(target, path);
if(labelProperty != null &&
labelProperty.PropertyType == typeof(string)) {
return (string)labelProperty.GetValue(target);
}

MethodInfo labelMethod = ReflectionUtility.GetMethod(target, path);
if(labelMethod != null &&
labelMethod.ReturnType == typeof(string) &&
labelMethod.GetParameters().Length == 0) {
return (string)labelMethod.Invoke(target, null);
}

return "[label property not found]";
}

internal static bool GetConditionsFlag(List<bool> conditionValues, EConditionOperator conditionOperator, bool invert)
{
bool flag;
Expand Down