- Home /
Custom inspector for files of certain type.
I would like to write a custom inspector for specific file types. Let's say you have a custom data file format (*.mydata) and for each of those .mydata files, you want to make some custom settings in unity. Just like when you click for example on an fbx file in the project view and you see all these import settings. Is this possible?
I have been googling and the closest solution I could find was to make a custom asset file (.asset) for every .mydata file, but that's not really what I want.
Answer by freakrho · Jun 07, 2017 at 10:43 PM
I'm working on that exact thing and I got most of it working. The custom inspector you can get it with something like this (I got it from here: https://forum.unity3d.com/threads/unsupported-file-inspector.441581/)
using System.Collections;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Inspector for .SVG assets
/// </summary>
[CustomEditor(typeof(DefaultAsset))]
public class SVGEditor : Editor
{
AssetImporter _importer;
bool _svgFile;
private void OnEnable()
{
var path = AssetDatabase.GetAssetPath(target);
// Check if .ase
if (path.EndsWith(".svg"))
{
// Get importer and userData
_importer = AssetImporter.GetAtPath(path);
_svgFile = true;
}
}
public override void OnInspectorGUI()
{
if (_svgFile)
{
SVGInspectorGUI();
}
else
{
base.OnInspectorGUI();
}
}
private void SVGInspectorGUI()
{
// TODO: Add inspector code here
}
}
The thing is I don't think you can use more than one of this for different file types, you would have to have one script for all types, but I guess that will be a problem when you need to have it on more than one file type.
The next thing is saving custom information on the .meta file, for this I used the asset importer that let's you edit the AssetImporter.userData with whatever information you need. I used JSON as a format to save information, it works like a charm.
_importer.userData = _data.ToString();
Now the problem I found was with saving the data to disk. There is an easy option which is AssetImporter.SaveAndReimport, the thing is, if you have an AssetPostProcessor working on this file, it will be called every time you call this, in my personal case, it would get stuck in an infinite loop because the post processor would edit the data as well. If you don't have an AssetPostProcessor, use this and you're golden:
_importer.SaveAndReimport();
Other than that, your alternative is:
EditorUtility.SetDirty(target);
AssetDatabase.SaveAssets();
Thing is, I couldn't get it to work, I check the .meta file after running this and there is no change. It will eventually save when Unity feels like it. If you ever get this to work, please tell me, I'm not finding it.
Now, you might be interested in having a postprocessor, it's quite useful if you want to run some function automatically when the file is modified the same way unity reimports models, for example.
You do this with the function OnPostprocessAllAssets
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
string[] movedAssets, string[] movedFromAssetPaths)
{
for (int i = 0; i < importedAssets.Length; i++)
{
string tPath = importedAssets[i];
if (tPath.EndsWith(".svg"))
{
AssetImporter tImporter = AssetImporter.GetAtPath(tPath);
// You can modify tImporter.userData here
EditorUtility.SetDirty(AssetDatabase.LoadAssetAtPath<Object>(tPath));
AssetDatabase.SaveAssets();
}
}
}
Here I have the same issue when saving, but you definitely can't use SaveAndReimport because you'll get stuck in a loop.
I just realized this question was written in 2013, but I hope it helps someone who needs it because it took me a while to find all this.
Answer by freakrho · Jun 08, 2017 at 03:29 PM
I'm working on that exact thing and I got most of it working. The custom inspector you can get it with something like this (I got it from here: https://forum.unity3d.com/threads/unsupported-file-inspector.441581/)
using System.Collections;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Inspector for .SVG assets
/// </summary>
[CustomEditor(typeof(DefaultAsset))]
public class SVGEditor : Editor
{
AssetImporter _importer;
bool _svgFile;
private void OnEnable()
{
var path = AssetDatabase.GetAssetPath(target);
// Check if .ase
if (path.EndsWith(".svg"))
{
// Get importer and userData
_importer = AssetImporter.GetAtPath(path);
_svgFile = true;
}
}
public override void OnInspectorGUI()
{
if (_svgFile)
{
SVGInspectorGUI();
}
else
{
base.OnInspectorGUI();
}
}
private void SVGInspectorGUI()
{
// TODO: Add inspector code here
}
}
The thing is I don't think you can use more than one of this for different file types, you would have to have one script for all types, but I guess that will be a problem when you need to have it on more than one file type.
The next thing is saving custom information on the .meta file, for this I used the asset importer that let's you edit the AssetImporter.userData with whatever information you need. I used JSON as a format to save information, it works like a charm.
_importer.userData = _data.ToString();
Now the problem I found was with saving the data to disk. There is an easy option which is AssetImporter.SaveAndReimport, the thing is, if you have an AssetPostProcessor working on this file, it will be called every time you call this, in my personal case, it would get stuck in an infinite loop because the post processor would edit the data as well. If you don't have an AssetPostProcessor, use this and you're golden:
_importer.SaveAndReimport();
Other than that, your alternative is:
EditorUtility.SetDirty(target);
AssetDatabase.SaveAssets();
Thing is, I couldn't get it to work, I check the .meta file after running this and there is no change. It will eventually save when Unity feels like it. If you ever get this to work, please tell me, I'm not finding it.
Now, you might be interested in having a postprocessor, it's quite useful if you want to run some function automatically when the file is modified the same way unity reimports models, for example.
You do this with the function OnPostprocessAllAssets
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
string[] movedAssets, string[] movedFromAssetPaths)
{
for (int i = 0; i < importedAssets.Length; i++)
{
string tPath = importedAssets[i];
if (tPath.EndsWith(".svg"))
{
AssetImporter tImporter = AssetImporter.GetAtPath(tPath);
// You can modify tImporter.userData here
EditorUtility.SetDirty(AssetDatabase.LoadAssetAtPath<Object>(tPath));
AssetDatabase.SaveAssets();
}
}
}
Here I have the same issue when saving, but you definitely can't use SaveAndReimport because you'll get stuck in a loop.
I just realized this question was written in 2013, but I hope it helps someone who needs it because it took me a while to find all this.