AssetImporterEditor
class in
UnityEditor.AssetImporters
/
Inherits from:Editor
Suggest a change
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
Description
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.
The following example demonstrates a specific case where the user cannot change settings and the Apply/Revert buttons are hidden with needsApplyRevert.
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)
{
// ...
}
}
Properties
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 |
Public Methods
HasModified | Determine if the import settings have been modified. |
OnDisable | This function is called when the editor object goes out of scope. |
OnEnable | This function is called when the object is loaded. |
OnInspectorGUI | Override this method to create your own Inpsector GUI for a ScriptedImporter. |
Protected Methods
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. |
Inherited Members
Properties
serializedObject | A SerializedObject representing the object or objects being inspected. |
target | The object being inspected. |
targets | An array of all the object being inspected. |
hideFlags | Should the object be hidden, saved with the Scene or modifiable by the user? |
name | The name of the object. |
Public Methods
CreateInspectorGUI | Implement this method to make a custom UIElements inspector. |
DrawDefaultInspector | Draws the built-in inspector. |
DrawHeader | Call this function to draw the header of the editor. |
DrawPreview | The first entry point for Preview Drawing. |
GetInfoString | Implement this method to show asset information on top of the asset preview. |
GetPreviewTitle | Override this method if you want to change the label of the Preview area. |
HasPreviewGUI | Override this method in subclasses if you implement OnPreviewGUI. |
OnInteractivePreviewGUI | Implement to create your own interactive custom preview. Interactive custom previews are used in the preview area of the inspector and the object selector. |
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 | Override this method if you want to show custom controls in the preview header. |
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 | Override this method in subclasses to return false if you don't want default margins. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the object. |
Static Methods
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 | Make a custom editor for targetObject or 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 | Returns the first active loaded object of Type type. |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
CreateInstance | Creates an instance of a scriptable object. |
Operators
bool | Does the object exist? |
operator != | Compares if two objects refer to a different object. |
operator == | Compares two object references to see if they refer to the same object. |
Messages
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 | This function is called when the scriptable object will be destroyed. |
OnValidate | This function is called when the script is loaded or a value is changed in the Inspector (Called in the editor only). |
Reset | Reset to default values. |
Events
finishedDefaultHeaderGUI | An event raised while drawing the header of the Inspector window, after the default header items have been drawn. |