- Home /
How to limit an ObjectField to assets with specific file extension
I am building a custom editor/inspector for a custom asset type. Currently my editor has an EditorGUILayout.ObjectField onto which I can drag and drop any object from my assets in order to associate that asset with my script pretty much in the same way you can drag an drop a .wav file onto an audiosource component. I would like the same behavior but only for my specific assets files (which are binary files with a custom filename extension ".myext"). So I would like my object field to only accept ".myfile" dropped in. How to properly achieve that?
So far my code looks like this:
[CustomEditor( typeof (MyClass)) ]
public class MyClassEditor : Editor {
System.Type myType = typeof(Object); //this will accept objects of *any* types. I would like to limit to objects whose file extensions is ".myext"
MyClass mc;
public override void OnInspectorGUI()
{
newFileObj = (Object)EditorGUILayout.ObjectField ("file", newFileObj, myType, false);
if(GUI.changed)
{
mc.Load(newFileObj); //MyClass has a method to actually load the file.
}
}
}
I am also interested to hear of any good suggestions for this. I am wanting to filter them to only show GameObjects that contain a certain Component Class.
Using that Component Class as the type does not populate the dialog with anything.
Answer by MakeCodeNow · May 25, 2014 at 07:14 PM
Object field can only filter by type, not by extension. If you want to filter by extension, you'll need to do your own file finding, probably using the .NET Directory and File APIs.
could I make a custom type for my custom assets? Could I use CreateAsset somehow to create objects of type myType? I am not familiar with Types nor CreateAssets though, I just know they exist. Not sure what I could do with them or if any of them would be appropriate in my case.
Yes. That is generally the most correct thing to do. $$anonymous$$ake a new subclass of ScriptableObject and then use the AssetDatabase to save the instances of that class you create. You can then have the ObjectField look only for your new subclass and it'll all "just work."
Answer by 10HPLeft · Aug 08, 2015 at 12:19 PM
It's just an idea, but you can maybe get the filepath using AssetDatabase.FindAssets and confirm that it's extension matches the one you want - then displaying a dialog telling the user that the file they selected wasn't a csv and cancelling out of it.
It's sort of hacky, but it might work.
Your answer
Follow this Question
Related Questions
Object references in a custom editor 2 Answers
Prefab as a subasset of a ScriptableObject 0 Answers
AssetDatabase.CreateAsset() - Value cannot be null 2 Answers
Custom inspector editor - how to put new editor fields in a specific place 1 Answer
Custom Editor ScriptableObject List shows "Type Mismatch" after CreateInstance 2 Answers