Hey all! I am writing a custom importer for Unity that parses a proprietary point cloud format.
Here is my folder structure:
Editor/
MyCustomImporter.cs
Runtime/
PointCloudRenderer.cs
FrameManager.cs
At import time, MyCustomImporter will fire a hook when my format import is detected and unzip it to a folder on the user machine. MyCustomImporter inherits, unsurprisingly, form ScriptedImporter.
At runtime, the FrameManager will read the files from this temp unzipped folder as buffers and pass them to PointCloudRenderer for rendering. Both classes inherit from MonoBehaviour and PointCloudRenderer does rendering via Graphics.DrawProceduralNow().
What I am struggling with is code organisation and data sharing between the Editor and Runtime scripts.
Here is my import script annotated with some comments:
[ScriptedImporter(1, "MY_FILE_FORMAT")]
public class MyCustomImporter : ScriptedImporter {
public override void OnImportAsset(AssetImportContext ctx) {
string zipFilePath = ctx.assetPath;
dirPath = Path.Combine(Application.temporaryCachePath, Path.GetFileNameWithoutExtension(zipFilePath));
if (Directory.Exists(dirPath)) {
Directory.Delete(dirPath, true);
}
Directory.CreateDirectory(dirPath);
UnzipFile(dirPath);
// 1. Create a new GameObject
GameObject gameObject = new GameObject("Point Cloud");
// 2. Add PointCloudRenderer as a component to the GameObject
PointCloudRenderer renderer = gameObject.AddComponent<PointCloudRenderer>();
// 3. Add FrameManager as a component to the GameObject
FrameManager frameManager = gameObject.AddComponent<FrameManager>();
// 4. I want to pass the temp dirPath location from the editor script to the runtime script
frameManager.dirPath = dirPath;
frameManager.Initialize();
// 5. Connect my PointCloud renderer with the frameManager so they can talk to each other
renderer.frameManager = frameManager;
// 6. Add the GameObject as a node
ctx.AddObjectToAsset("prefab", gameObject);
ctx.SetMainObject(gameObject);
}
}
My questions are:
- Is this a correct approach for sharing data (mainly
dirPath) from editor to runtime scripts? - Should
FrameManagerinherit fromMonoBehaviourorScriptableObjectis better alternative. I read thatScriptableObjectis perfect for data storing, howeverFrameManagerdoes not have any data really, it just reads bytes from disk and forwards them toPointCloudRenderer. - Please notice how I “link” the two
MonoBehaviourclasses on line 23. Is this correct? Is there a better “Unity approach” to this?
Thanks in advance!