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 /
avatar image
1
Question by swquinn · Mar 25, 2011 at 03:34 AM · editorscriptableobjectobjectfield

Is there any way to associate a "custom asset" with a particular file type?

I've seen similar questions already asked, but none seemed to answer the core of my question, that is: is it possible to create a custom asset (i.e. an object extending ScriptableObject) and associate it with a specific file type?

As an example, if I have an object:

FooAsset.cs

using UnityEngine;

public class FooAsset : ScriptableObject { public Object _asset; }


FooAssetEditor.cs

using UnityEditor; using UnityEngine;

[CustomEditor(typeof(FooAsset))] public class FooAssetEditor : Editor { protected FooAsset _fooAsset;

public override void OnInspectorGUI() { _fooAsset = (FooAsset) target; _fooAsset.Asset = (Object) EditorGUILayout.ObjectField( "Foo File", _fooAsset.Asset, typeof(Object)); } }

Is there any way that upon using the EditorGUILayout.ObjectField(...) method, or some other combination of methods/GUI objects that can I associate the _asset field on FooAsset with a completely arbitrary file type (e.g. .foo) that resides underneath my Assets folder?

I have a very simple custom editor right now doing what I want with the ObjectField(...) method, except that I can't see assets of type .foo within its selection window. (NB: my actual extension, not .foo, identifies a binary file--specifically an archive containing xml data. I haven't quite figured out how to even work with the file once I get it loaded in the manner I would like to load it in, but I'll cross that bridge when I come to it).

Any help is appreciated, if you can point me to projects, tutorials, or snippets of code that I may have missed in my search, I'd be forever grateful!


If there is no "easy" way to accomplish the above using what is already in Unity, perhaps someone can point me in the right direction for what I will need to customize?

I suspect I will have to do some combination of the following to achieve a custom field editor with behavior similar to that of the ObjectField:

  1. Create a custom editor which presents the user with a field (Drag-and-Drop enabled) to accept assets.
  2. A file browser/asset selection window that targets specifically only the file type (or types) I'm looking for, but is not necessarily confined to a specific folder (e.g. Resources).

Comment
Add comment · Show 4
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 swquinn · Jun 06, 2011 at 03:13 AM 0
Share

So, what I've ended up doing so far is creating a preview window to load the asset from the file system which will read the custom asset through an EditorUtility.OpenFilePanel(...) call and then read in the bytes from the file. Its not quite as convenient as drag-and-drop, but it works. Now I just need to figure out how to return the loaded value.

Once I have a complete solution I can post it.

avatar image SONB · Apr 26, 2012 at 01:24 PM 0
Share

Hi, I'm trying to read in the bytes from my file as well. Did you figure out how to get the loaded value?

avatar image swquinn · Apr 26, 2012 at 01:44 PM 0
Share

@SONB I don't think I ever came up with a good way of registering a custom asset type with Unity 3D; I do have a "file loader" of sorts though. I forgot that I mentioned I was going to post a solution. Let me see if I can dig it up, I haven't touched that code recently. When I find it I'll post it as a possible answer.

avatar image basil · Dec 13, 2012 at 07:31 AM 0
Share

hi it's my first time to handle ScriptAbleObjects, I did Custom asset file and it works fine in a List<> but when i use hashtable() the hashtable is not found, i seems data is not stored when it comes to hash table..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Apr 26, 2012 at 02:56 PM

I still don't fully understand the question. Arbitrary assets (ScriptableObjects) needs the extension ".asset". You can't use another extension, see AssetDatabase.CreateAsset.

Windows users (like myself) are used to the fact that the extension tells you the type of the file. Unity does it in a similar way but you have to see the situation from Unitys point of view. The assetdatabase is designed to handle the build-in types in the first place. The scriptable object is a way to store arbitrary data. Unity doesn't care about the derived type, so it's just an "asset".

If you want to handle the assets based on the file name, give it a meaningful name. For example a scriptable object that contains an instance of FooAsset could be named like this:

 "MyFoo.foo.asset", "MyFoo(foo).asset", "Foo_MyFoo.asset", ...

Now you can check the name (not the real file extension) for your additional extension or any other way to store the type information in the filename.

Unity supports only a few asset types. For example if you need any type of binary / text / xml asset you have to use ".txt" so it's imported as TextAsset which is stored as it is in the project.

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 Bunny83 · Apr 26, 2012 at 03:24 PM 0
Share

Just realised that this question is quite old and got bumbed by comments ^^. oh well...

avatar image swquinn · Apr 26, 2012 at 03:35 PM 0
Share

@Bunny83 a bit old, yeah, but I appreciate the response any way.

The impetus behind this was the creation of a packaged set of dialogues into a compressed and encoded format (using a .dialogue extension) that I was hoping I could associated with Unity 3D seamlessly. As you point out Unity 3D supports only a few asset types with tight integration to the editor. Ultimately, because identifying it as a text asset was not sufficient for me I ended up creating a custom editor to load and process what I wanted and needed to process.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

EditorGUILayout.ObjectField does not allow me to select a script. 1 Answer

cannot change variables on scriptableobject asset in editor 1 Answer

How should I serialize data that is also editable in the Inspector? 2 Answers

Drag from custom editor ObjectField 0 Answers

Is there a message to be listen to when editor enter play mode? 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