- Home /
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:
- Create a custom editor which presents the user with a field (Drag-and-Drop enabled) to accept assets.
- 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).
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.
Hi, I'm trying to read in the bytes from my file as well. Did you figure out how to get the loaded value?
@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.
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..
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.
Just realised that this question is quite old and got bumbed by comments ^^. oh well...
@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
Follow this Question
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