Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom TextureImporterInspector

Discussion in 'Scripting' started by DeadNinja, Aug 6, 2014.

  1. DeadNinja

    DeadNinja

    Joined:
    Jun 3, 2013
    Posts:
    39
    Good day everyone. Just want to share some code if anyone in need for it. Recently i was working on extending import of some supported image formats so i came in need for a custom TextureImporterInspector. The problem was to show custom Inspector for some formats and usual for others. So here is the solution based on some reflection black magic:

    Code (csharp):
    1.  
    2. [CustomEditor(typeof(TextureImporter))]
    3. public class TextureImporterCustomEditor : Editor
    4. {
    5.     private SerializedObject serializedTarget;
    6.  
    7.     private Editor nativeEditor;
    8.  
    9.     public void OnEnable()
    10.     {
    11.         serializedTarget = new SerializedObject(target);
    12.         SceneView.onSceneGUIDelegate = TargetUpdate;    
    13.    
    14.         Type t = null;  
    15.         foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    16.         {
    17.             foreach (Type type in assembly.GetTypes())
    18.             {
    19.                 if (type.Name.ToLower().Contains("textureimporterinspector"))
    20.                 {
    21.                     t = type;
    22.                     break;
    23.                 }
    24.             }
    25.         }
    26.  
    27.         nativeEditor = Editor.CreateEditor(serializedObject.targetObject, t);
    28.     }
    29.  
    30.     void TargetUpdate(SceneView sceneview)
    31.     {
    32.         Event e = Event.current;
    33.     }
    34.  
    35.     public override void OnInspectorGUI()
    36.     {
    37.         if (nativeEditor != null)
    38.         {    
    39.             bool isCustom = // check  ((TextureImporter)serializedObject.targetObject).assetPath for file formats you need
    40.             if (isCustom)
    41.             {
    42.               // Do your stuff here
    43.               // you can use ((TextureImporter)serializedObject.targetObject).userData to store your stuff between sessions
    44.             }
    45.             else
    46.               nativeEditor.OnInspectorGUI();        
    47.         }
    48.  
    49.         // Unfortuanaly we cant hide ImportedObject section because InspectorWindow check it by
    50.         //  if (editor is AssetImporterEditor) and all flags that this check sets are method local variables
    51.         //  so aside from direct patching UnityEditor.dll no luck for reflection here :(
    52.      
    53.         //  in my case i just moved ImportedObject section out of view
    54.         GUILayout.Space(2048);      
    55.  
    56.         SceneView.RepaintAll();
    57.     }
    58.     protected override void OnHeaderGUI()
    59.     {  
    60.          // We cant use nativeEditor.OnHeaderGUI because some internal variables not sets correctly by:
    61.          // Editor.CreateEditor so do some stuff to emulate native inspector header or customize it for your likes
    62.     }
    63. }
    64.  
     
    Last edited: Aug 6, 2014
  2. tiancaiwrk

    tiancaiwrk

    Joined:
    Nov 23, 2017
    Posts:
    35
    it works , I have been searching for a long time for this, thx:)
     
  3. skulllism

    skulllism

    Joined:
    Feb 26, 2015
    Posts:
    6
    wow

    it works!!

    I applaud you:D
     
  4. reedreedchen

    reedreedchen

    Joined:
    Feb 12, 2017
    Posts:
    9
    It worked kinda... the Apply and Revert buttons disappear using the script. Any help?

    Edit: I ended up writing apply and reset on my own.
     
    Last edited: Feb 16, 2022
  5. g4ma

    g4ma

    Joined:
    Dec 18, 2018
    Posts:
    32