Deadcows / MyBox

MyBox is a set of attributes, tools and extensions for Unity
http://deadcow.ru
MIT License
1.94k stars 244 forks source link

Passing a List or Array to DefinedValues #235

Closed Vedrina closed 1 year ago

Vedrina commented 1 year ago

Hi, I'm not sure if this is possible already, but I've used TriInspector before and used my code like this:

[Serializable]
public class AnimalDictionaryItem {

    #if UNITY_EDITOR
    [Dropdown(nameof(GetNames))]
    #endif
    public string name;
    public Sprite sprite;
    public AnimalType animalType;

    #if UNITY_EDITOR
    private List<string> GetNames()
    {
        var assetName = AssetDatabase.FindAssets("WordsDictionary", new[] { "Assets/_Game/Data/Core" });

        var assetPath    = AssetDatabase.GUIDToAssetPath(assetName[0]);
        var wordsDictionary= AssetDatabase.LoadAssetAtPath<WordsDictionary>(assetPath);
        return wordsDictionary.animalsWords;
    }
    #endif
}

In here I wanted to get a dropdown of strings which were returned as a list from other scriptable object. In WordsDictionary.cs i have public animalsWords which have been read from json file, and it was serialized beforehand.

[Serializable]
[CreateAssetMenu(fileName = "WordsDictionary ", menuName = "WordsDictionary ")]
public class WordsDictionary : ScriptableObject
{
    // There's code before this section

    [HideInInspector] public List<string> allWords;
    [HideInInspector] public List<string> animalsWords;
    [HideInInspector] public List<string> plantsWords;

    // There's code after this section
}

Just to conclude, I want dropdown of List names which would be generated from json file in the editor.

Is something like this possible in Mybox?

Thank you upfront!

Deadcows commented 1 year ago

Really cool idea! It's not supported at the moment, but I already made initial working solution. Now I'm trying to make it possible to pass any objects suitable for the inspector with the method, like a popup of the filtered Transforms from the child objects 🤔

It may take a few days, since I'm on a vacation, but I really eager to make this improvement

Deadcows commented 1 year ago

@Vedrina I made this change. I will improve it a little bit later on, but it is pretty useful as it is.

Try the Update button in the MyBox Window. It won't show that there is a new version, but will grab the latest changes anyway.

For now, it is possible to use only with the methods returning arrays, so in your method simply put wordsDictionary.animalsWords.ToArray() at the end and change return type to string[]. [DefinedValues(nameof(GetNames))] public string name; should do just fine.

The code I used to test this new feature:

[DefinedValues(nameof(StringTest))] 
public string TestStrMethodPopup1;

[DefinedValues("Hello", "World")] 
public string TestStrPopup2;

[DefinedValues(.5f, 1f, 1.5f, 2f)] 
public float TestFloatPopup;

[DefinedValues(nameof(TransformTest))] 
public Transform TestTrMethodPopup;

private string[] StringTest() => new[] { "A", "B", "C" };

private Transform[] TransformTest()
{
    var childs = transform.GetChilds();
    childs.Insert(0, null);
    return childs.ToArray();
}

image

Vedrina commented 1 year ago

My god you are fast. Thank you!