So I tried making a custom editor for the TextureImporter class but I guess there is already a built in editor that seems to disappear when I make a custom editor for it. How can I keep the built in editor but add my own stuff?
[CustomEditor(typeof(TextureImporter))]
public class OverrideImportSettings : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Test"))
{
}
}
}
The insperctor after I apply the script above:
EDIT: I have managed to get an even better workflow without modifying the texture import settings, but in case anyone else is wondering how, I won’t close the thread.
Taobao
August 21, 2024, 3:23am
4
I found your question while was searching for the same answer. Since I wrote the solution, I will share it here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEngine;
[CustomEditor(typeof(TextureImporter))]
[CanEditMultipleObjects]
public class CustomTextureImporter : Editor {
private static readonly Lazy<Type> _textureImporterInspectorType = new(() => Type.GetType("UnityEditor.TextureImporterInspector, UnityEditor"));
private static readonly Lazy<MethodInfo> _setAssetImporterTargetEditorMethod = new(() => _textureImporterInspectorType.Value.GetMethod("InternalSetAssetImporterTargetEditor", BindingFlags.Instance | BindingFlags.NonPublic));
private static readonly Lazy<FieldInfo> _onEnableCalledField = new(() => typeof(AssetImporterEditor).GetField("m_OnEnableCalled", BindingFlags.Instance | BindingFlags.NonPublic));
private Editor _defaultEditor;
private List<TextureImporter> _targets;
private void OnEnable() {
if (_defaultEditor != null) {
_onEnableCalledField.Value.SetValue(_defaultEditor, true);
DestroyImmediate(_defaultEditor);
_defaultEditor = null;
}
_defaultEditor = (AssetImporterEditor)CreateEditor(targets, _textureImporterInspectorType.Value);
_setAssetImporterTargetEditorMethod.Value.Invoke(_defaultEditor, new object[] { this });
_targets = targets.Cast<TextureImporter>().ToList();
}
public override void OnInspectorGUI() {
_defaultEditor.OnInspectorGUI();
if (GUILayout.Button("Test")) {
foreach (var target in _targets) {
Debug.Log($"Do something to {target}");
}
}
hasUnsavedChanges = _defaultEditor.hasUnsavedChanges;
saveChangesMessage = _defaultEditor.saveChangesMessage;
}
public override void SaveChanges() {
base.SaveChanges();
_defaultEditor.SaveChanges();
}
public override void DiscardChanges() {
base.DiscardChanges();
_defaultEditor.DiscardChanges();
}
}
DaveA
September 9, 2021, 5:19pm
3
Probably related to this:
I’m not sure if it is possible. But I really hope so. Basically I want to add a checkbox to the inspector, that will determine whether or not to execute something on import. I know I can write a script for OnPostprocessModel, but that would...
Reading time: 3 mins 🕑
Likes: 17 ❤