Default editor for all asset importer settings.
Use the default editor to edit the import settings for assets. You can define a custom import settings editor for a specific asset type. To do this, create a new class that inherits from AssetImporterEditor and uses a CustomEditorAttribute that refers to a ScriptedImporter.
The following example shows how to make a custom ScriptedImporterEditor for a ScriptedImporter with a custom layout.
using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine;
[CustomEditor(typeof(TransformImporter))] [CanEditMultipleObjects] public class TransformImporterEditor : ScriptedImporterEditor { // Stored SerializedProperty to draw in OnInspectorGUI. SerializedProperty m_GenerateChild;
public override void OnEnable() { base.OnEnable(); // Once in OnEnable, retrieve the serializedObject property and store it. m_GenerateChild = serializedObject.FindProperty("generateChild"); }
public override void OnInspectorGUI() { // Update the serializedObject in case it has been changed outside the Inspector. serializedObject.Update();
// Draw the boolean property. EditorGUILayout.PropertyField(m_GenerateChild);
// Apply the changes so Undo/Redo is working serializedObject.ApplyModifiedProperties();
// Call ApplyRevertGUI to show Apply and Revert buttons. ApplyRevertGUI(); } }
[ScriptedImporter(0, ".transform")] public class TransformImporter : ScriptedImporter { public bool generateChild;
public override void OnImportAsset(AssetImportContext ctx) { GameObject root = ObjectFactory.CreateGameObject(Path.GetFileNameWithoutExtension(ctx.assetPath)); if (generateChild) { GameObject child = ObjectFactory.CreateGameObject("child"); child.transform.SetParent(root.transform); } ctx.AddObjectToAsset("main", root); ctx.SetMainObject(root); } }
The following example demonstrates a specific case where the user cannot change settings and the Apply/Revert buttons are hidden with needsApplyRevert.
using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine;
[CustomEditor(typeof(EmptinessImporter))] [CanEditMultipleObjects] public class EmptinessImporterEditor : ScriptedImporterEditor { //Let the parent class know that the Apply/Revert mechanism is skipped. protected override bool needsApplyRevert => false;
public override void OnInspectorGUI() { // Draw some information EditorGUILayout.HelpBox("Because this Importer doesn't have any settings, the Apply/Revert buttons are hidden.", MessageType.None); } }
[ScriptedImporter(0, ".emptiness")] public class EmptinessImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { GameObject root = ObjectFactory.CreateGameObject(Path.GetFileNameWithoutExtension(ctx.assetPath)); ctx.AddObjectToAsset("main", root); ctx.SetMainObject(root); } }
The following example shows how to use extraDataType to read or save settings that are not part of the ScriptedImporter serialization, in the custom AssetImporterEditor.
using System; using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine; using Object = UnityEngine.Object;
[CustomEditor(typeof(BooleanImporter))] [CanEditMultipleObjects] public class BooleanImporterEditor : ScriptedImporterEditor { // Property to show in the custom OnInspectorGUI. SerializedProperty m_BooleanProperty;
// override extraDataType to return the type that will be used in the Editor. protected override Type extraDataType => typeof(BooleanClass);
// override InitializeExtraDataInstance to set up the data. protected override void InitializeExtraDataInstance(Object extraTarget, int targetIndex) { var boolean = (BooleanClass)extraTarget; // Read the boolean value from the text file and fill the extraTarget object with the data. string fileContent = File.ReadAllText(((AssetImporter)targets[targetIndex]).assetPath); if (!bool.TryParse(fileContent, out boolean.boolean)) { boolean.boolean = false; } }
protected override void Apply() { base.Apply(); // After the Importer is applied, rewrite the file with the custom value. for (int i = 0; i < targets.Length; i++) { string path = ((AssetImporter)targets[i]).assetPath; File.WriteAllText(path, ((BooleanClass)extraDataTargets[i]).boolean.ToString()); } }
public override void OnEnable() { base.OnEnable(); // In OnEnable, retrieve the importerUserSerializedObject property and store it. m_BooleanProperty = extraDataSerializedObject.FindProperty("boolean"); }
public override void OnInspectorGUI() { // Note: you don't need to call serializedObject.Update or serializedObject.ApplyModifiedProperties // because you are not changing the target (serializedObject) itself.
// Update the importerUserSerializedObject in case it has been changed outside the Inspector. extraDataSerializedObject.Update();
// Draw the boolean property. EditorGUILayout.PropertyField(m_BooleanProperty);
// Apply the changes so Undo/Redo is working. extraDataSerializedObject.ApplyModifiedProperties();
// Call ApplyRevertGUI to show Apply and Revert buttons. ApplyRevertGUI(); } }
public class BooleanClass : ScriptableObject { public bool boolean; }
[ScriptedImporter(0, ".boolean")] public class BooleanImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { string fileContent = File.ReadAllText(ctx.assetPath); var booleanObj = ObjectFactory.CreateInstance<BooleanClass>(); if (!bool.TryParse(fileContent, out booleanObj.boolean)) { booleanObj.boolean = false; } ctx.AddObjectToAsset("main", booleanObj); ctx.SetMainObject(booleanObj); Debug.Log("Imported Boolean value: " + booleanObj.boolean); } }
You can also use ScriptedImporter settings and extraData in the same AssetImporterEditor:
using UnityEditor; using UnityEditor.AssetImporters;
[CustomEditor(typeof(SomeScriptedImporter))] [CanEditMultipleObjects] public class SomeImporterEditor : ScriptedImporterEditor { // ...
public override void OnInspectorGUI() { serializedObject.Update(); extraDataSerializedObject.Update();
// Use propertyDrawers and custom GUI for any property from both serializedObject and extraDataSerializedObject.
extraDataSerializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
ApplyRevertGUI(); } }
[ScriptedImporter(0, ".someFile")] public class SomeScriptedImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { // ... } }
extraDataSerializedObject | A SerializedObject that represents the extraDataTarget or the extraDataTargets of the AssetImporterEditor. |
extraDataTarget | The extra data object associated with the Editor.target. |
extraDataTargets | An array of objects associated with each Editor.targets. |
extraDataType | Override this property to return a type that inherits from ScriptableObject. This makes the AssetImporterEditor aware of serialized data outside of the Importer. |
needsApplyRevert | Whether the ApplyRevertGUI method is required to draw in the Inspector. |
showImportedObject | Should imported object be shown as a separate editor? |
useAssetDrawPreview | Determines if the asset preview is handled by the AssetEditor or the Importer DrawPreview |
HasModified | Determine if the import settings have been modified. |
OnDisable | This function is called when the editor object goes out of scope. |
OnEnable | オブジェクトがロードされたとき、この関数は呼び出されます。 |
OnInspectorGUI | Override this method to create your own Inpsector GUI for a ScriptedImporter. |
Apply | Saves any changes from the Editor's control into the asset's import settings object. |
ApplyAndImport | Saves the changes from the editor UI to the settings object and imports the asset. |
ApplyButton | Implements the 'Apply' button of the inspector. |
ApplyRevertGUI | Add's the 'Apply' and 'Revert' buttons to the editor. |
Awake | This function is called when the Editor script is started. |
CanApply | Determines if the modifications to import settings can be applied. |
InitializeExtraDataInstance | This method is called during the initialization process of the Editor, after Awake and before OnEnable. |
OnApplyRevertGUI | Process the 'Apply' and 'Revert' buttons. |
ResetValues | Reset the import settings to their last saved values. |
RevertButton | Implements the 'Revert' button of the inspector. |
serializedObject | object や objects の SerializedObject |
target | ターゲットとなるオブジェクト |
targets | 複数選択された場合のターゲットとなるオブジェクト群 |
hideFlags | Should the object be hidden, saved with the Scene or modifiable by the user? |
name | オブジェクト名 |
CreateInspectorGUI | Implement this method to make a custom UIElements inspector. |
DrawDefaultInspector | Draws the built-in inspector. |
DrawHeader | Editor のヘッダーを描画するためにはこの関数を呼び出します。 |
DrawPreview | プレビュー描画するための最初のエントリーポイントです。 |
GetInfoString | プレビューにアセットの情報を表示するにはこのメソッドを使用します。 |
GetPreviewTitle | プレビューのタイトルを変更したい場合はこのメソッドをオーバーライドします。 |
HasPreviewGUI | OnPreviewGUI を実装する場合は、サブクラスでこのメソッドをオーバーライドします。 |
OnInteractivePreviewGUI | 自身のカスタムのインタラクティブなプレビューを作成するために実装します。カスタムのインタラクティブなプレビューはインスペクター上のプレビューエリアとオブジェクト選択ツールで使用されます |
OnPreviewGUI | Implement to create your own custom preview for the preview area of the inspector, the headers of the primary editor, and the object selector. |
OnPreviewSettings | プレビューのヘッダーを自由にカスタマイズしたい場合にオーバーライドして使用します。 |
RenderStaticPreview | Override this method if you want to render a static preview. |
Repaint | Redraw any inspectors that shows this editor. |
RequiresConstantRepaint | Checks if this editor requires constant repaints in its current state. |
UseDefaultMargins | デフォルトのマージンを取らせたくない場合、Editor を継承したクラスでメソッドをオーバーライドし、false を返すようにします |
GetInstanceID | オブジェクトのインスタンス ID を返します |
ToString | Returns the name of the object. |
ShouldHideOpenButton | Returns the visibility setting of the "open" button in the Inspector. |
CreateCachedEditor | On return previousEditor is an editor for targetObject or targetObjects. The function either returns if the editor is already tracking the objects, or destroys the previous editor and creates a new one. |
CreateCachedEditorWithContext | Creates a cached editor using a context object. |
CreateEditor | targetObject や複数の targetObjects のためのカスタムエディターを作成します。 |
CreateEditorWithContext | Make a custom editor for targetObject or targetObjects with a context object. |
DrawFoldoutInspector | Draws the inspector GUI with a foldout header for target. |
Destroy | Removes a GameObject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. You are strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Do not destroy the target Object when loading a new Scene. |
FindObjectOfType | タイプ type から最初に見つけたアクティブのオブジェクトを返します |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | original のオブジェクトをクローンします |
CreateInstance | ScriptableObject のインスタンスを作成します。 |
bool | オブジェクトが存在するかどうか |
operator != | 二つのオブジェクトが異なるオブジェクトを参照しているか比較します |
operator == | 2つのオブジェクト参照が同じオブジェクトを参照しているか比較します。 |
HasFrameBounds | Validates whether custom bounds can be calculated for this editor. |
OnGetFrameBounds | Gets custom bounds for the target of this editor. |
OnSceneGUI | Enables the Editor to handle an event in the Scene view. |
OnDestroy | ScriptableObject が破棄されるとき、この関数は呼び出されます。 |
OnValidate | Editor-only function that Unity calls when the script is loaded or a value changes in the Inspector. |
Reset | デフォルト値にリセットします |
finishedDefaultHeaderGUI | An event raised while drawing the header of the Inspector window, after the default header items have been drawn. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.