Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unsupported file inspector

Discussion in 'Immediate Mode GUI (IMGUI)' started by CrashKonijn, Nov 17, 2016.

  1. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Hi all,

    I'm currently working on a SVG to PNG converter in unity to optimize the workflow in our project. Currently I can already convert these files, but I'd like to have import settings for the .svg file before I run my code.

    So, what I want is to have a custom (file importer?) inspector when I select an .svg file in the project view (see screenshot). I've been googling for a while now but I've been unable to find a way to do this.
    ss (2016-11-17 at 04.57.29).png

    Hopefully you guys can point me in the right direction!

    - Peter
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You should define your own inspector for your .svg files.
    Since Unity doesn't recognize the .svg file as an image, it is probably imported as a generic asset (text? default? not sure). If you want to create an inspector for it, you would have to create an inspector for this type.

    Also, Unity doesn't allow creating your own importer, it has a set of a few importers for known file types, and that's it.

    You can "simulate" this behaviour using 2 different components:
    1. Inspector -- will be shown when selecting the .svg asset. Will allow configuring it (per the options you want to expose).
    2. AssetPostProcessor -- invoked when importing new assets (see this method: https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html)
    You can check out my post on how to create custom inspectors here: http://www.tallior.com/unity-custom-inspectors/
     
  3. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Hi liortal,

    Thanks for your reply! I am aware that you can make custom inspectors, but I was only able to find how to do this for classes (which I've already done many times). The problem is that I don't know how to make a custom inspector for a specific file type.

    Anyway, as far as I can tell Unity doesn't import the .svg files in any way, not even as a text. I was indeed planning on using the assetpostprocessor to automate the conversion of any new .svg files in the project :)
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Here's a basic inspector code that you could start with:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. /// <summary>
    6. /// Inspector for .SVG assets
    7. /// </summary>
    8. [CustomEditor(typeof(DefaultAsset))]
    9. public class SVGEditor : Editor
    10. {
    11.     public override void OnInspectorGUI()
    12.     {
    13.         // .svg files are imported as a DefaultAsset.
    14.         // Need to determine that this default asset is an .svg file
    15.         var path = AssetDatabase.GetAssetPath(target);
    16.  
    17.         if (path.EndsWith(".svg"))
    18.         {
    19.             SVGInspectorGUI();
    20.         }
    21.         else
    22.         {
    23.             base.OnInspectorGUI();
    24.         }
    25.     }
    26.  
    27.     private void SVGInspectorGUI()
    28.     {  
    29.         // TODO: Add inspector code here
    30.     }
    31. }
    Add whatever you want to show in the inspector and it will be displayed when selecting .svg assets.
     
    CodeSmile, RebelRae, Siro360 and 4 others like this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    The above custom editor works also for Text Assets if you replace DefaultAsset with TextAsset.

    However in the case of TextAsset I lose the text preview window that normally appears, even when calling base.OnInspectorGUI(). Does anyone know how to restore the default TextAsset inspector GUI?


    Btw, for better compatibility I chose to use Path.GetExtension and ensure the extension is lowercase:
    Code (CSharp):
    1. var extension = Path.GetExtension(AssetDatabase.GetAssetPath(target));
    2. if (string.IsNullOrEmpty(extension) == false && extension.ToLower() == ".csv")
    3.     CsvInspectorGUI();
    4.