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
5
Question by Aithoneku · Dec 19, 2013 at 04:27 PM · assetassetsassetdatabasecustomizationcustomize

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?

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 John-LZG · Jun 30, 2017 at 07:40 PM 0
Share

2017, and it seems the only change is you can now use this hacky way with JSON...?

0 Replies

· Add your reply
  • Sort: 

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

20 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

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


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