Hello !
I’m trying to import a custom file into my project, and I’d like to be able to load it from using the AssetDatabase.LoadAssetAtPath() method.
I created a “ScriptedImporter” that loads my file and adds it to the AssetImportContext, but once I want to load it from the path, it returns a null value.
Below, the code I am using:
using System.IO;
using UnityEditor.AssetImporters;
using UnityEngine;
[ScriptedImporter(1, "wrld")]
public class WrldImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
var asset = ScriptableObject.CreateInstance<WorldFile>();
asset.name = Path.GetFileNameWithoutExtension(ctx.assetPath);
// Here I load the content of the file into the current object.
asset.Initialize(File.OpenRead(ctx.assetPath));
ctx.AddObjectToAsset("main obj", asset);
ctx.SetMainObject(asset);
}
}
Here is my loading code inside of a MonoBehavior:
string mapPath = Path.Combine("Maps", _name);
string wrldFilePath = Path.Combine(mapPath, $"{_name}");
var worldFile = AssetDatabase.LoadAssetAtPath<WorldFile>(wrldFilePath);
Debug.Log(worldFile is null ? "null" : worldFile.name); // always null
Am I doing something wrong? Or maybe I didn’t quite understand the purpose of the ScriptedImporter…