PLUGIN SDK / Plugin and host / PluginHost
PluginHost
The editor seen from a C# plugin: registrations (commands, panels, settings pages, services, importers, exporters, asset types, viewport tools, automation steps, component types), the document scene, selection, undoable transactions, editor tasks, notifications, logging, per-user settings and paths. Calls run on the editor's main thread unless documented otherwise (Log, Post). Registrations stay until the plugin is unloaded or reloaded, or until Unregister. Everything not wrapped here is reachable through Raw (the C tables of kroys/kroys_plugin.h).
Kindclass
NamespaceKroys.Plugins
Members42
Source
PluginHost.csEXAMPLE
In a plugin
A complete plugin that uses PluginHost.
Guides.cs
public sealed class SceneStats : EditorPlugin { public override void OnLoad() { Host.AddCommand(new CommandOptions("Count", "Count Entities") { Menu = "Stats" }, () => { int count = Host.Document?.Entities.Count ?? 0; Host.Notify(Severity.Info, "Scene", $"{count} entities, {Host.Selection.Count} selected"); }); // Slow work on a worker thread, with progress in the Tasks panel; the completion runs on the main thread. Host.AddCommand(new CommandOptions("Scan", "Scan Content") { Menu = "Stats" }, () => Host.RunTask("Scan content", progress => { string[] files = Directory.GetFiles(Host.ContentFolder, "*", SearchOption.AllDirectories); progress.Report(1.0f, $"{files.Length} files"); }, (succeeded, message) => Host.Log($"scan finished: {message}"))); } public override void OnUnload() => Host.Log("Stats unloading (registrations are removed for you)"); }
REFERENCE
Members
Properties
#public KroysHost* Raw { get; }The raw C host (tables of kroys_plugin.h, mirrored in
Kroys.Plugins.Native).#public nint Context { get; }The raw KroysContext passed first to every C function.
#public string Id { get; }The plugin id (manifest).
#public Version SdkVersion { get; }The engine's SDK version.
#public Viewport Viewport { get; }Viewport drawing for viewport tools.
#public string PluginFolder { get; }The plugin folder (manifest, sources).
#public string ProjectFolder { get; }The project root ("" without a project).
#public string ContentFolder { get; }The content root.
#public string DataFolder { get; }A per-user writable folder of this plugin.
#public bool IsPlaying { get; }True while Play runs (the document cannot be edited then).
#public World? Document { get; }The edited scene; null without a scene or during Play (permission scene.read).
#public IReadOnlyList<Entity> Selection { get; }The selected entities.
Methods
#public void Log(string message)A Console line (thread-safe).
#public void LogWarning(string message)A Console warning (thread-safe).
#public void LogError(string message)A Console error (thread-safe).
#public void Log(Severity severity, string message)A Console line of the given severity (thread-safe).
#public void Notify(Severity severity, string title, string message)A non-modal toast and a Console line.
#public string GetSetting(string key, string fallback = "")A per-user plugin setting (kept across reloads and restarts).
#public void SetSetting(string key, string value)Stores a per-user plugin setting.
#public void Unregister(Registration registration)Removes one registration.
#public Registration AddCommand(CommandOptions options, Action execute, Func<bool>? isEnabled = null, Func<bool>? isChecked = null)A command (capability "commands"): palette, Tools > Plugins menu, toolbar, shortcut.
#public Registration AddPanel(PanelOptions options, Action<Ui> draw)A dockable panel (capability "panels") drawn every frame with
Ui.#public Registration AddSettingsPage(SettingsPageOptions options, Action<Ui> draw)A Settings window page (capability "settings").
#public Registration AddService(string name, Action<float> tick)A per-frame editor service (capability "services").
#public Registration AddAutomationStep(string name, Func<string, AutomationJob, bool> run)An automation step (capability "automation"): capture scripts run it as plugin-step <name> [argument].
#public Registration AddImporter(ImporterOptions options, Action<FileJob> run)An importer (capability "importers");
run runs on a worker thread.#public Registration AddExporter(ExporterOptions options, Action<FileJob> run)An exporter (capability "exporters");
run runs on a worker thread.#public Registration AddAssetType(AssetTypeOptions options, AssetTypeHandlers handlers)An asset type (capability "asset-types").
#public Registration AddViewportTool(ViewportToolOptions options, ViewportToolHandlers handlers)A viewport tool (capability "viewport-tools").
#public ComponentType AddComponentType(ComponentTypeOptions options)A plugin component type (capability "components"): a real engine component named "<plugin id>.<name>" with reflected fields (inspector, scene files, undo). Registering the identical definition again (after a reload) returns the same type.
#public ComponentType FindComponentType(string typeName)Any registered component type ("TransformComponent", "Plugin.Id.Name"); invalid when unknown.
#public void Execute(string commandId)Runs any editor command ("File.SaveProject", "<plugin id>.<id>").
#public void SetPanelOpen(string panelId, bool open)Opens or closes a panel (local id of this plugin, or a full id).
#public void MarkAssetDirty(string path)Asks the host to call the asset type's Save hook (Save All / Plugins.SaveAssets).
#public void Select(params Entity[] entities)Replaces the selection.
#public IDisposable Transaction(string label)Groups document edits into one undo step until disposed:
using (Host.Transaction("Align")) { ... }.#public ulong RunTask(string name, Action<TaskProgress> run, Action<bool, string>? complete = null, string? lane = null)An editor task (capability "tasks", shown in the Tasks panel):
run on a worker thread, then complete on the main thread. Returns the task id.#public void CancelTask(ulong taskId)Cancels a task.
#public void Post(Action action)Runs
action on the main thread during the next editor update (thread-safe).#public byte[] ReadContentFile(string path)Reads a content file (content-relative path, '/' separators).
#public void ImportFile(string sourcePath, string destinationFolder)Imports a file into a content folder (permission assets.write).
#public void CreateAssetFile(string folder, string fileName, byte[] bytes)Creates a content file through the asset database (new GUID, undoable; permission assets.write).
Signatures
public KroysHost* Raw { get; } public nint Context { get; } public string Id { get; } public Version SdkVersion { get; } public Viewport Viewport { get; } public void Log(string message) public void LogWarning(string message) public void LogError(string message) public void Log(Severity severity, string message) public void Notify(Severity severity, string title, string message) public string GetSetting(string key, string fallback = "") public void SetSetting(string key, string value) public string PluginFolder { get; } public string ProjectFolder { get; } public string ContentFolder { get; } public string DataFolder { get; } public void Unregister(Registration registration) public Registration AddCommand(CommandOptions options, Action execute, Func<bool>? isEnabled = null, Func<bool>? isChecked = null) public Registration AddPanel(PanelOptions options, Action<Ui> draw) public Registration AddSettingsPage(SettingsPageOptions options, Action<Ui> draw) public Registration AddService(string name, Action<float> tick) public Registration AddAutomationStep(string name, Func<string, AutomationJob, bool> run) public Registration AddImporter(ImporterOptions options, Action<FileJob> run) public Registration AddExporter(ExporterOptions options, Action<FileJob> run) public Registration AddAssetType(AssetTypeOptions options, AssetTypeHandlers handlers) public Registration AddViewportTool(ViewportToolOptions options, ViewportToolHandlers handlers) public ComponentType AddComponentType(ComponentTypeOptions options) public ComponentType FindComponentType(string typeName) public void Execute(string commandId) public void SetPanelOpen(string panelId, bool open) public void MarkAssetDirty(string path) public bool IsPlaying { get; } public World? Document { get; } public IReadOnlyList<Entity> Selection { get; } public void Select(params Entity[] entities) public IDisposable Transaction(string label) public ulong RunTask(string name, Action<TaskProgress> run, Action<bool, string>? complete = null, string? lane = null) public void CancelTask(ulong taskId) public void Post(Action action) public byte[] ReadContentFile(string path) public void ImportFile(string sourcePath, string destinationFolder) public void CreateAssetFile(string folder, string fileName, byte[] bytes)
KroysEngine 0.1.0 documentation, built from the engine repository by Tools/BuildDocsSite.py.