- Home /
Importing custom assets?
Short version: is there finally some way to cleanly import custom assets / custom files and file formats?
Long version: I'm searching Internet for long time to find out how to import own file formats (for example data with 2D sprite animations) and use it in own components. But only answers I found were following:
no
use text file
no
no and you cannot import any other file than files with allowed extension
no
use binary file
no
but all of them were old (I saw this question in 2009, 2010, 2011 and 2012) so I would like to ask whether there was a change in this or this need is still ignored.
Meantime, I would like to share my current solution, but it's not much pretty.
Write class inheriting from ScriptableObject which will keep all the data.
- Use AssetPostprocessor's OnPostprocessAllAssets event to find out whether a file is imported.
Go through all the imported files and check for ones we want to import - for example by file extension.
Create new asset, parse all the data and fill data in the new object.
Advantages:
Storing data using Unity's (and C#'s) serialization system.
So that means we don't need to write saving.
Disadvantages:
Two files with same data (this shouldn't be problem with current disk sizes, but redundancy is always ugly thing).
Changes are not saved back into original file.
Duplicity also means that when the original file is changed, our custom changes are overwritten or new data are ignored (you can choose between these two options).
Notes:
- On the other hand, you might add a button in the inspector to save changes back to the original file.
That means implementing own save function.
You can find path to the new file with AssetDatabase.GetAssetPath and derive original path from it.
Example code for importing:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.IO;
public class OurImporter : AssetPostprocessor
{
static string extension = ".ourExt"; // Our custom extension
static string newExtension = ".asset"; // Extension of newly created asset - it MUST be ".asset", nothing else is allowed...
public static bool HasExtension(string asset)
{
return asset.EndsWith(extension, System.StringComparison.OrdinalIgnoreCase);
}
public static string ConvertToInternalPath(string asset)
{
string left = asset.Substring(0, asset.Length - extension.Length);
return left + newExtension;
}
// This is called always when importing something
static void OnPostprocessAllAssets
(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths
)
{
foreach(string asset in importedAssets)
{
// This is our detection of file - by extension
if(HasExtension(asset))
{
TextReader stream = new StreamReader(asset);
string firstLine = stream.ReadLine();
stream.Close();
// Also we check first file line
if(firstLine.Equals("MagicLine", StringComparison.OrdinalIgnoreCase))
{
ImportMyAsset(asset);
}
else
{
Debug.LogError("Cannot import \"" + asset + "\": bad format!");
}
}
}
}
// Imports my asset from the file
static void ImportMyAsset(string asset)
{
// Path to out new asset
string newPath = ConvertToInternalPath(asset);
// MyAsset is imported asset type, it should derive from ScriptableObject, probably
MyAsset numSeq = AssetDatabase.LoadAssetAtPath(newPath, typeof(MyAsset)) as MyAsset;
bool loaded = (numSeq != null);
if(!loaded)
{
numSeq = ScriptableObject.CreateInstance<NumSeqAsset>();
}
else
{
// return; // Uncommenting here means that when the original file is changed, changes are ignored
}
// Here we load our asset from original file
numSeq.Load(asset);
if(!loaded)
{
AssetDatabase.CreateAsset(numSeq, newPath);
}
AssetDatabase.SaveAssets();
}
}
So... is there finally clean solution, or do we need to solve it with hacky ways like this?
2017, and it seems the only change is you can now use this hacky way with JSON...?
Your answer
Follow this Question
Related Questions
Best tools for asset making? 0 Answers
Addressables - Use Asset Database (faster) - Not loading assets within a folder 1 Answer
How do I import assets from the Asset Store while offline? 2 Answers
Where are asset labels stored? 1 Answer
The AssetBundle can't be loaded because another AssetBundle with the same files is already loaded. 1 Answer