Odin Custom Attribute Drawer für ListeC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Odin Custom Attribute Drawer für Liste

Post by Anonymous »

Ich möchte mit C# Odin Unity eine benutzerdefinierte Schublade für meine Liste meines abstrakten Typs erstellen.

Code: Select all

[Serializable]
[InlineProperty]
public abstract class BaseEntry : ScriptableObject
{
public string _ID;
public string _Name;
}

[Serializable]
[InlineProperty]
public class Ability : BaseEntry
{
[HorizontalLabelHeader("Test")]
public string _Description;
}
Und so sieht mein Testskript aus:

Code: Select all

public class TestScript : MonoBehaviour
{
[SerializeField]
[ClickableList("List")]
private List _ability;

[Button]
public void Load()
{
_ability = new List();
_ability.Clear();

_ability = Resources.LoadAll("").ToList();
}
}
Jetzt das Attribut und seine Schublade:

Code: Select all

[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
[Conditional("UNITY_EDITOR")]
[DontApplyToListElements]
public class ClickableListAttribute : Attribute
{
public string Label;

public ClickableListAttribute(string label)
{
Label = label;
}
}

public class ClickableListDrawer : OdinAttributeDrawer where T: BaseEntry
{
protected override void DrawPropertyLayout(GUIContent label)
{
this.LogInfo("Drawing");
List entries = ValueEntry.SmartValue;

if (entries == null)
{
entries = new List();
ValueEntry.SmartValue = entries;
}

//
for (int i = 0; i < entries.Count; i++)
{
T entry = entries[i];

SirenixEditorGUI.BeginBox(entry ? entry._Name : "New Entry");
{
if (entry == null)
{
if (GUILayout.Button("Create New " + typeof(T).Name))
{
entry = ScriptableObject.CreateInstance();
AssetDatabase.CreateAsset(entry, $"Assets/New{typeof(T).Name}.asset");
AssetDatabase.SaveAssets();
entries[i] = entry;
}
}
else
{
entry._Name = EditorGUILayout.TextField("Name", entry._Name);
entry._ID = EditorGUILayout.TextField("ID", entry._ID);
}

if (GUILayout.Button("Remove"))
{
entries.RemoveAt(i);
break;
}
}
SirenixEditorGUI.EndBox();
}
}
}
Zur Sache:

Das Attribut „ClickableListAttribute“ kann nicht auf die Eigenschaft „_ability“ des Basistyps „List“ gesetzt werden.

Dies sind die Informationen, die ich im Inspektor erhalte, wenn ich diese Liste überprüfe. Dieses Attribut funktioniert gut, wenn ich List in den TValue-Parameter von ClickableListDrawer einfüge, funktioniert aber nicht mehr, wenn ich möchte, dass es für alle Klassen funktioniert, die von BaseEntry erben. Irgendeine Lösung?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post