• 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
2
Question by Filip Van Bouwel · Nov 05, 2013 at 04:27 PM · inspectorfilecustomimporter

Custom inspector for files of certain type.

I would like to write a custom inspector for specific file types. Let's say you have a custom data file format (*.mydata) and for each of those .mydata files, you want to make some custom settings in unity. Just like when you click for example on an fbx file in the project view and you see all these import settings. Is this possible?

I have been googling and the closest solution I could find was to make a custom asset file (.asset) for every .mydata file, but that's not really what I want.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by freakrho · Jun 07, 2017 at 10:43 PM

I'm working on that exact thing and I got most of it working. The custom inspector you can get it with something like this (I got it from here: https://forum.unity3d.com/threads/unsupported-file-inspector.441581/)

 using System.Collections;
 using UnityEditor;
 using UnityEngine;
  
 /// <summary>
 /// Inspector for .SVG assets
 /// </summary>
 [CustomEditor(typeof(DefaultAsset))]
 public class SVGEditor : Editor
 {
     AssetImporter _importer;
     bool _svgFile;

     private void OnEnable()
     {
         var path = AssetDatabase.GetAssetPath(target);
         // Check if .ase
         if (path.EndsWith(".svg"))
         {
             // Get importer and userData
             _importer = AssetImporter.GetAtPath(path);
 
             _svgFile = true;
         }
     }

     public override void OnInspectorGUI()
     {
         if (_svgFile)
         {
             SVGInspectorGUI();
         }
         else
         {
             base.OnInspectorGUI();
         }
     }
  
     private void SVGInspectorGUI()
     {  
         // TODO: Add inspector code here
     }
 }

The thing is I don't think you can use more than one of this for different file types, you would have to have one script for all types, but I guess that will be a problem when you need to have it on more than one file type.

The next thing is saving custom information on the .meta file, for this I used the asset importer that let's you edit the AssetImporter.userData with whatever information you need. I used JSON as a format to save information, it works like a charm.

 _importer.userData = _data.ToString();

Now the problem I found was with saving the data to disk. There is an easy option which is AssetImporter.SaveAndReimport, the thing is, if you have an AssetPostProcessor working on this file, it will be called every time you call this, in my personal case, it would get stuck in an infinite loop because the post processor would edit the data as well. If you don't have an AssetPostProcessor, use this and you're golden:

 _importer.SaveAndReimport();

Other than that, your alternative is:

 EditorUtility.SetDirty(target);
 AssetDatabase.SaveAssets();

Thing is, I couldn't get it to work, I check the .meta file after running this and there is no change. It will eventually save when Unity feels like it. If you ever get this to work, please tell me, I'm not finding it.

Now, you might be interested in having a postprocessor, it's quite useful if you want to run some function automatically when the file is modified the same way unity reimports models, for example.

You do this with the function OnPostprocessAllAssets

 static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
     string[] movedAssets, string[] movedFromAssetPaths)
 {
     for (int i = 0; i < importedAssets.Length; i++)
     {
         string tPath = importedAssets[i];
         if (tPath.EndsWith(".svg"))
         {
             AssetImporter tImporter = AssetImporter.GetAtPath(tPath);
             // You can modify tImporter.userData here

             EditorUtility.SetDirty(AssetDatabase.LoadAssetAtPath<Object>(tPath));
             AssetDatabase.SaveAssets();
         }
     }
 }

Here I have the same issue when saving, but you definitely can't use SaveAndReimport because you'll get stuck in a loop.

I just realized this question was written in 2013, but I hope it helps someone who needs it because it took me a while to find all this.

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
avatar image
0

Answer by freakrho · Jun 08, 2017 at 03:29 PM

I'm working on that exact thing and I got most of it working. The custom inspector you can get it with something like this (I got it from here: https://forum.unity3d.com/threads/unsupported-file-inspector.441581/)

 using System.Collections;
 using UnityEditor;
 using UnityEngine;
  
 /// <summary>
 /// Inspector for .SVG assets
 /// </summary>
 [CustomEditor(typeof(DefaultAsset))]
 public class SVGEditor : Editor
 {
     AssetImporter _importer;
     bool _svgFile;

     private void OnEnable()
     {
         var path = AssetDatabase.GetAssetPath(target);
         // Check if .ase
         if (path.EndsWith(".svg"))
         {
             // Get importer and userData
             _importer = AssetImporter.GetAtPath(path);
 
             _svgFile = true;
         }
     }

     public override void OnInspectorGUI()
     {
         if (_svgFile)
         {
             SVGInspectorGUI();
         }
         else
         {
             base.OnInspectorGUI();
         }
     }
  
     private void SVGInspectorGUI()
     {  
         // TODO: Add inspector code here
     }
 }

The thing is I don't think you can use more than one of this for different file types, you would have to have one script for all types, but I guess that will be a problem when you need to have it on more than one file type.

The next thing is saving custom information on the .meta file, for this I used the asset importer that let's you edit the AssetImporter.userData with whatever information you need. I used JSON as a format to save information, it works like a charm.

 _importer.userData = _data.ToString();

Now the problem I found was with saving the data to disk. There is an easy option which is AssetImporter.SaveAndReimport, the thing is, if you have an AssetPostProcessor working on this file, it will be called every time you call this, in my personal case, it would get stuck in an infinite loop because the post processor would edit the data as well. If you don't have an AssetPostProcessor, use this and you're golden:

 _importer.SaveAndReimport();

Other than that, your alternative is:

 EditorUtility.SetDirty(target);
 AssetDatabase.SaveAssets();

Thing is, I couldn't get it to work, I check the .meta file after running this and there is no change. It will eventually save when Unity feels like it. If you ever get this to work, please tell me, I'm not finding it.

Now, you might be interested in having a postprocessor, it's quite useful if you want to run some function automatically when the file is modified the same way unity reimports models, for example.

You do this with the function OnPostprocessAllAssets

 static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
     string[] movedAssets, string[] movedFromAssetPaths)
 {
     for (int i = 0; i < importedAssets.Length; i++)
     {
         string tPath = importedAssets[i];
         if (tPath.EndsWith(".svg"))
         {
             AssetImporter tImporter = AssetImporter.GetAtPath(tPath);
             // You can modify tImporter.userData here

             EditorUtility.SetDirty(AssetDatabase.LoadAssetAtPath<Object>(tPath));
             AssetDatabase.SaveAssets();
         }
     }
 }

Here I have the same issue when saving, but you definitely can't use SaveAndReimport because you'll get stuck in a loop.

I just realized this question was written in 2013, but I hope it helps someone who needs it because it took me a while to find all this.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Custom Inspector when Selecting from Project Tree? 0 Answers

Inspector button for custom class 1 Answer

How do you make a custom inspector for a class or instance? 3 Answers

add gameobject via the inspector? 1 Answer

WebPlayer Custom Params Inspector 0 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