Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Unsupported file inspector

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

  1. Crash-Konijn

    Crash-Konijn

    Joined:
    Mar 4, 2010
    Posts:
    224
    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,481
    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. Crash-Konijn

    Crash-Konijn

    Joined:
    Mar 4, 2010
    Posts:
    224
    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,481
    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.
     
    Siro360, wcw, correia55 and 2 others like this.
unityunity