Search code examples
c#unity-game-engineassets

ScriptedImporterEditor don't work Unity tools


I'm Unity tools developper and i want to create an event when the user import a .FBX file

My code :

using System.IO;
using UnityEditor;
using UnityEditor.Experimental.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, ".fbx")]
public class EmptinessImporter : ScriptedImporter
{
    public override void OnImportAsset(AssetImportContext ctx)
    {
        Debug.Log(ctx.assetPath);
    }
}

Error :

Scripted importers EmptinessImporter and EmptinessImporter are targeting the fbx extension, rejecting both. UnityEditor.Experimental.AssetImporters.ScriptedImporter:RegisterScriptedImporters()

But that doesn't work, I'm working on Unity 2019.4.9f1


Solution

  • Sounds like you have two concurrent implementations with the same name and both targeting fbx.

    However, if you anynway do not really want to specifically handle the import but rather just react with an event once a specific import has happened you might rather want to implement an AssetPostprocessor like maybe e.g.

    #if UNITY_EDITOR
    using System;
    using UnityEditor;
    using UnityEngine;
    
    public class FbxPostProcessor : AssetPostprocessor
    {
        public static event Action<string, GameObject> OnFBXImported; 
        
        private void OnPostprocessModel(GameObject gameObject)
        {
            if (assetPath.EndsWith(".fbx", StringComparison.InvariantCultureIgnoreCase))
            {
                Debug.Log($"An FBX file \"{assetPath}\" has been imported!");
                    
                OnFBXImported?.Invoke(assetPath, gameObject);
            }
        }
    }
    #endif
    

    so another editor script could simply listen to

    FbxPostProcessor.OnFBXImported += SomeCallback;
    

    enter image description here