Search Unity

Inspector Asset Selection Dropdown Menu?

Discussion in 'Scripting' started by The-Oddler, Apr 9, 2015.

  1. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I'm trying to create a dropdown menu to select from all possible assets of a type. I have a field for an ArmourType, which is an asset, and there you can select the type. If you click the little circle next to it you get a list of all possible assets to choose from:



    I'm now trying to make this a dropdown list. I succeeded, almost:



    As you can see, it is a dropdown list. The code for this is:

    Code (CSharp):
    1.         SerializedProperty serializedArmourType = property.FindPropertyRelative ("_armourType");
    2.         ArmourType armourType = serializedArmourType.objectReferenceValue as ArmourType;
    3.         List<ArmourType> armourTypes = Resources.FindObjectsOfTypeAll<ArmourType>()
    4.             .OrderBy(a=>a.Name)
    5.             .ToList();
    6.         int typeIndex = armourTypes.IndexOf(armourType);
    7.        
    8.         typeIndex = EditorGUI.Popup(typeRect, typeIndex, armourTypes.Select(a => a.Name).ToArray());
    9.         serializedArmourType.objectReferenceValue = armourTypes[typeIndex];
    But because I use `FindObjectsOfTypeAll` I only get the assets that are loaded into memory, not all possible ones. You can see only 4 show up in my dropdown list. If I go and open the folder where the assets are stored, and select them all so they are loaded into memory, and then go back to the dropdown list, they do all show up.

    So is there a way to fix this? Perhaps a way to get them to be loaded when I look for them? Or an easier or better way of making such a dropdown list?

    Note: I do not want to use enums. I've come to dislike enums. They still have their purpose, but what I'm trying to do is not one of those. So changing it to an enum is no solution for the dropdown menu. I say this because this is the only solution I found googling :(

    Thanks!
     
  2. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I just fixed it, thanks to the guys at stackoverflow. Instead of using "Resources.FindObjectsOfTypeAll" I now use "AssetDatabase.FindAssets".

    Code (CSharp):
    1. List<ArmourType> armourTypes = AssetDatabase.FindAssets("t:ArmourType")
    2.        .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
    3.        .Select(path => AssetDatabase.LoadAssetAtPath(path, typeof(ArmourType)))
    4.        .Select(obj  => (ArmourType)obj)
    5.        .OrderBy(a=>a.Name)
    6.        .ToList();
     
  3. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    have try but not refresh in inspector need somethink's else ?
     
  4. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133