Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions com.unity.toonshader/Editor/Scripts/GUI/UnityToon3Das2DGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ internal enum HighlightMode {
}

private static readonly GUIContent[] m_outlineModeEnums = ToonEnumUtility.ToInspectorNamesAsGUIContent(typeof(ToonOutlineMode));
private static readonly int[] m_outlineModeIndices = ToonEnumUtility.ToIndices(typeof(ToonOutlineMode));
private static readonly int[] m_outlineModeIndices = ToonEnumUtility.ToIntValues(typeof(ToonOutlineMode));

private static readonly GUIContent[] m_highlightModeEnums = ToonEnumUtility.ToInspectorNamesAsGUIContent(typeof(HighlightMode));
private static readonly int[] m_highlightModeIndices = ToonEnumUtility.ToIndices(typeof(HighlightMode));
private static readonly int[] m_highlightModeIndices = ToonEnumUtility.ToIntValues(typeof(HighlightMode));
Comment thread
sindharta marked this conversation as resolved.
Outdated
Comment thread
sindharta marked this conversation as resolved.
Outdated

static readonly GUIContent COLORS_FOLDOUT = EditorGUIUtility.TrTextContent("Colors",
"Colors for basic cel-shading settings in Unity Toon Shader.");
Expand Down
38 changes: 14 additions & 24 deletions com.unity.toonshader/Runtime/Scripts/Utilities/ToonEnumUtility.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Unity.Rendering.Toon {

internal static class ToonEnumUtility {
internal static GUIContent[] ToInspectorNamesAsGUIContent(Type t) {
MemberInfo[] members = t.GetMembers(BindingFlags.Static | BindingFlags.Public);

int numMembers = members.Length;
GUIContent[] ret = new GUIContent[numMembers];
for (int i = 0; i < numMembers; i++) {
InspectorNameAttribute inspectorNameAttribute = (InspectorNameAttribute)Attribute.GetCustomAttribute(
members[i], typeof(InspectorNameAttribute));
if (inspectorNameAttribute == null) {
ret[i] = new GUIContent(members[i].Name);
} else {
ret[i] = new GUIContent(inspectorNameAttribute.displayName);
}
string[] names = Enum.GetNames(t);
GUIContent[] ret = new GUIContent[names.Length];
for (int i = 0; i < names.Length; i++) {
FieldInfo field = t.GetField(names[i], BindingFlags.Public | BindingFlags.Static);
InspectorNameAttribute attr = field != null
? (InspectorNameAttribute)Attribute.GetCustomAttribute(field, typeof(InspectorNameAttribute))
: null;
ret[i] = new GUIContent(attr != null ? attr.displayName : names[i]);
}
Comment thread
sindharta marked this conversation as resolved.

return ret;
}

internal static int[] ToIndices(Type t) {

MemberInfo[] members = t.GetMembers(BindingFlags.Static | BindingFlags.Public);
int numMembers = members.Length;
int[] indices = new int[numMembers];
for (int i = 0; i < numMembers; ++i) {
indices[i] = i;
}

internal static int[] ToIntValues(Type t) {
Array values = Enum.GetValues(t);
int numValues = values.Length;
int[] indices = new int[numValues];
for (int i = 0; i < numValues; i++)
indices[i] = Convert.ToInt32(values.GetValue(i));
return indices;
Comment thread
sindharta marked this conversation as resolved.
Outdated
Comment thread
sindharta marked this conversation as resolved.
Outdated
Comment thread
sindharta marked this conversation as resolved.
Outdated

}

}
Expand Down
28 changes: 26 additions & 2 deletions com.unity.toonshader/Tests/Runtime/Scripts/ToonEnumUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ internal enum DummyEnum {
Second
}

// Declared out of numeric order to verify results are sorted by value, not declaration order.
internal enum NonSequentialEnum {
High = 20,
Low = 5,
Mid = 10,
}

[Test]
public void ToInspectorNamesAsGUIContentTest() {
GUIContent[] contents = ToonEnumUtility.ToInspectorNamesAsGUIContent(typeof(DummyEnum));
Expand All @@ -22,13 +29,30 @@ public void ToInspectorNamesAsGUIContentTest() {
}

[Test]
public void ToIndicesTest() {
int[] indices = ToonEnumUtility.ToIndices(typeof(DummyEnum));
public void ToIntValuesTest() {
int[] indices = ToonEnumUtility.ToIntValues(typeof(DummyEnum));
Assert.AreEqual(2, indices.Length);
Assert.AreEqual(0, indices[0]);
Assert.AreEqual(1, indices[1]);
}
Comment thread
Copilot marked this conversation as resolved.
Comment thread
Copilot marked this conversation as resolved.

[Test]
public void NonSequentialEnumNamesAndValuesAreAlignedAndSortedTest() {
GUIContent[] contents = ToonEnumUtility.ToInspectorNamesAsGUIContent(typeof(NonSequentialEnum));
int[] values = ToonEnumUtility.ToIntValues(typeof(NonSequentialEnum));

Assert.AreEqual(3, contents.Length);
Assert.AreEqual(3, values.Length);

Assert.AreEqual("Low", contents[0].text);
Assert.AreEqual("Mid", contents[1].text);
Assert.AreEqual("High", contents[2].text);

Assert.AreEqual(5, values[0]);
Assert.AreEqual(10, values[1]);
Assert.AreEqual(20, values[2]);
}

const string FIRST_VALUE = "First Value";
}

Expand Down