- Home /
Custom Inspector Element on Texture Importer?
I'm trying to get a custom editor to show up in the inspector for textures. If I use
@CustomEditor(TextureImporter)
it overwrites the Texture Importer settings. How would I go about attaching this to the inspector for images?
I agree, it would be useful if there was a way to daisy-chain some custom controls after/before the default editor. This is quite an old question, but there still doesn't appear to be a way to do this.
Answer by Statement · Apr 04, 2011 at 01:08 AM
@CustomEditor(TextureImporter) class Texture2DEditor extends Editor {
var drawDefaultInspector : boolean;
var drawExtendedInspector : boolean;
function OnInspectorGUI() {
GUI.enabled = true;
drawDefaultInspector = GUILayout.Toggle(drawDefaultInspector, "Default Inspector", EditorStyles.toolbarDropDown);
if (drawDefaultInspector) DrawDefaultInspector();
drawExtendedInspector = GUILayout.Toggle(drawExtendedInspector, "Extended Inspector", EditorStyles.toolbarDropDown);
if (drawExtendedInspector) DrawExtendedInspector();
}
function DrawExtendedInspector() {
GUILayout.Label("Hello OnInspectorGUI!");
}
}
Note that you have to redo the upper portion, I was unable to draw the built in inspector. I also tried Texture2D editors but they didn't work well either (check answer revision history). I checked out the built in texture inspector in UnityEditor.dll, but it uses internal functions that are inaccessible. You might be able to use reflection to invoke them but if I remember correctly, they have application security that deny those operations so you're out of luck if that is the case. Anyhow, it's the best I can give.
I have spent some time trying to rebuild the original TextureImporterInspector using reflector so we could reuse its original GUI. This was not an easy task, and one have to rebuild several classes. I'm currently at 1100 lines of code and there's still tons missing. What's more is everywhere they make use of internal members to key classes, so, I highly doubt you'll ever be able to get the standard gui + extended gui this way. I'll investigate another route though, just got an idea.
I had an idea to add a custom asset object to the texture in an AssetPostprocessor and create an inspector for that object type. However, it doesn't seem to work. $$anonymous$$aybe I'm doing something wrong, or it can't be done. Warning: Don't try to save any asset in AssetPostprocessor or you'll get caught in a loop. I also tried to create a menu item that would add this whenever the user chose to, to break free of the loop. But then the default texture importer seems to overwrite any changes, and I still won't get the object in inspector. I'm thinking this is a dead route.
Answer by Statement · Apr 04, 2011 at 12:36 PM
I came to the conclusion that you can't easily do this. A much easier approach would be to make another editor window that sits next to your inspector. I know it's not ideal but I think it's the best we can do until Unity Technologies open up more customization options for inspectors. The script is very minimal at its current state but you should be able to extend it to fit your likings.
class TextureInspector extends EditorWindow { var scrollView : Vector2;
@MenuItem("Window/Texture Inspector")
static function OpenWindow() {
var window : TextureInspector = EditorWindow.GetWindow (TextureInspector);
window.title = "T2D Inspector";
}
function OnGUI() {
var textures = Selection.GetFiltered(Texture2D, SelectionMode.Assets);
scrollView = GUILayout.BeginScrollView(scrollView);
GUILayout.Label ("Selected Textures", EditorStyles.boldLabel);
for (var texture : Texture2D in textures) {
EditorGUILayout.InspectorTitlebar(true, texture);
DrawInspector(texture);
}
GUILayout.EndScrollView();
Repaint();
}
function DrawInspector(texture : Texture2D) {
GUILayout.Label("Add your custom inspection here");
}
}
Your answer
Follow this Question
Related Questions
Can you set Read/Write on an image in an Editor Script? 1 Answer
EditorWindow texture effected by Playmode Color Tint 1 Answer
Editor class "Texture Importer" question (applying settings to multiple texture assets). 2 Answers
AssetBundle to prefab => meshes/textures missing 1 Answer
Editor Script Selection thinks Sprites are Texture2Ds. 2 Answers