Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by baba · May 25, 2014 at 06:16 PM · assetcustom editorassetdatabasecustom inspector

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.
         }
     }
 
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alturis2 · Jan 07, 2018 at 05:45 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image baba · May 25, 2014 at 07:59 PM 0
Share

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.

avatar image MakeCodeNow · May 26, 2014 at 10:32 PM 0
Share

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."

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges