Skip to content

Instantly share code, notes, and snippets.

@hippocoder
Created December 20, 2018 07:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hippocoder/4d48b2f33254ba8028225f80899d533d to your computer and use it in GitHub Desktop.
Save hippocoder/4d48b2f33254ba8028225f80899d533d to your computer and use it in GitHub Desktop.
Draw the default inspector for your SettingsProvider to save time!
using UnityEditor;
static class GameSettingsProvider
{
[SettingsProvider]
public static SettingsProvider CreateGameSettings()
{
var provider = new SettingsProvider("Game/Settings", SettingsScope.Project)
{
guiHandler = (searchContext) =>
{
Editor ed = Editor.CreateEditor(ScriptableObjectUtilities.GetOrCreate<GameSettings>(GameSettings.path));
ed.OnInspectorGUI();
},
};
return provider;
}
}
@hippocoder
Copy link
Author

Normally you will want to supply the ScriptableObject yourself, but here is the GetOrCreate method for the curious:

public static T GetOrCreate<T>(string path) where T : ScriptableObject
{
	var obj = AssetDatabase.LoadAssetAtPath(path, typeof(T));
	if (obj == null)
	{
		obj = ScriptableObject.CreateInstance(typeof(T));
		AssetDatabase.CreateAsset(obj, path);
		AssetDatabase.SaveAssets();
	}
	return obj as T;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment