Add prefab command UX

This commit is contained in:
Meta Vens 2026-06-15 01:30:32 +01:00
commit 85a1e617e7
5 changed files with 116 additions and 2 deletions

View file

@ -110,6 +110,10 @@ write typed command intents; Unity executes recognized intents and publishes
receipts. `setComponentProperty` writes the target object by default; when
`componentType` is present, Unity resolves that component on the target
GameObject and writes the serialized property there.
`instantiatePrefab` uses `assetPath` as the source prefab, `name` as the
optional instance name, and `parentObjectId` as the optional parent.
`createPrefabVariant` uses `assetPath` as the source prefab and `value` as the
destination prefab path.
`createScriptableObject` uses `componentType` as the ScriptableObject type and
`assetPath` as the destination asset path.

View file

@ -115,5 +115,23 @@ namespace GameCult.Brokkr.Editor
get => EditorPrefs.GetString("GameCult.Brokkr.ScriptableObjectAssetPath", "Assets/BrokkrAsset.asset");
set => EditorPrefs.SetString("GameCult.Brokkr.ScriptableObjectAssetPath", value);
}
internal static string PrefabAssetPath
{
get => EditorPrefs.GetString("GameCult.Brokkr.PrefabAssetPath", "");
set => EditorPrefs.SetString("GameCult.Brokkr.PrefabAssetPath", value);
}
internal static string PrefabInstanceName
{
get => EditorPrefs.GetString("GameCult.Brokkr.PrefabInstanceName", "");
set => EditorPrefs.SetString("GameCult.Brokkr.PrefabInstanceName", value);
}
internal static string PrefabVariantPath
{
get => EditorPrefs.GetString("GameCult.Brokkr.PrefabVariantPath", "Assets/BrokkrPrefabVariant.prefab");
set => EditorPrefs.SetString("GameCult.Brokkr.PrefabVariantPath", value);
}
}
}

View file

@ -174,6 +174,8 @@ namespace GameCult.Brokkr.Editor
var baseName = string.IsNullOrWhiteSpace(command.name) ? $"{prefab.name}Variant" : command.name;
variantPath = $"Assets/{baseName}.prefab";
}
EnsureAssetFolder(variantPath);
variantPath = AssetDatabase.GenerateUniqueAssetPath(variantPath);
var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
PrefabUtility.SaveAsPrefabAsset(instance, variantPath);

View file

@ -38,6 +38,9 @@ namespace GameCult.Brokkr.Editor
private string scriptableObjectType = "";
private string scriptableObjectName = "";
private string scriptableObjectAssetPath = "Assets/BrokkrAsset.asset";
private string prefabAssetPath = "";
private string prefabInstanceName = "";
private string prefabVariantPath = "Assets/BrokkrPrefabVariant.prefab";
[MenuItem("GameCult/Brokkr")]
public static void Open()
@ -65,6 +68,9 @@ namespace GameCult.Brokkr.Editor
scriptableObjectType = BrokkrSettings.ScriptableObjectType;
scriptableObjectName = BrokkrSettings.ScriptableObjectName;
scriptableObjectAssetPath = BrokkrSettings.ScriptableObjectAssetPath;
prefabAssetPath = BrokkrSettings.PrefabAssetPath;
prefabInstanceName = BrokkrSettings.PrefabInstanceName;
prefabVariantPath = BrokkrSettings.PrefabVariantPath;
Selection.selectionChanged += Repaint;
EditorSceneManagerBridge.SceneDirtied += OnEditorSignal;
EditorApplication.playModeStateChanged += OnPlayModeChanged;
@ -110,6 +116,9 @@ namespace GameCult.Brokkr.Editor
BrokkrSettings.ScriptableObjectType = scriptableObjectType;
BrokkrSettings.ScriptableObjectName = scriptableObjectName;
BrokkrSettings.ScriptableObjectAssetPath = scriptableObjectAssetPath;
BrokkrSettings.PrefabAssetPath = prefabAssetPath;
BrokkrSettings.PrefabInstanceName = prefabInstanceName;
BrokkrSettings.PrefabVariantPath = prefabVariantPath;
SetStatus("Settings saved.", MessageType.Info);
}
@ -171,6 +180,24 @@ namespace GameCult.Brokkr.Editor
{
PublishCreateScriptableObjectCommand();
}
EditorGUILayout.Space();
prefabAssetPath = EditorGUILayout.TextField("Prefab Asset Path", prefabAssetPath);
prefabInstanceName = EditorGUILayout.TextField("Instance Name", prefabInstanceName);
prefabVariantPath = EditorGUILayout.TextField("Variant Path", prefabVariantPath);
using (new EditorGUILayout.HorizontalScope())
{
if (GUILayout.Button("Instantiate Prefab"))
{
PublishInstantiatePrefabCommand();
}
if (GUILayout.Button("Create Prefab Variant"))
{
PublishCreatePrefabVariantCommand();
}
}
}
private void DrawSyncSection()
@ -323,8 +350,7 @@ namespace GameCult.Brokkr.Editor
? "Assets/BrokkrAsset.asset"
: scriptableObjectAssetPath.Trim()
};
mirror.PublishCommandAsync(command).GetAwaiter().GetResult();
PollAndExecuteCommand(true);
PublishCommandAndPoll(command);
}
catch (Exception error)
{
@ -332,6 +358,67 @@ namespace GameCult.Brokkr.Editor
}
}
private void PublishInstantiatePrefabCommand()
{
try
{
RequireMirror();
if (string.IsNullOrWhiteSpace(prefabAssetPath))
{
throw new InvalidOperationException("Enter a prefab asset path before instantiating.");
}
var selected = Selection.activeGameObject;
var command = new BrokkrUnityCommand
{
commandId = StableId("unity-command", "instantiate-prefab", Guid.NewGuid().ToString("N")),
action = "instantiatePrefab",
assetPath = prefabAssetPath.Trim(),
name = prefabInstanceName.Trim(),
parentObjectId = selected != null ? BrokkrUnitySnapshotBuilder.GetObjectId(selected) : ""
};
PublishCommandAndPoll(command);
}
catch (Exception error)
{
SetStatus(error.Message, MessageType.Error);
}
}
private void PublishCreatePrefabVariantCommand()
{
try
{
RequireMirror();
if (string.IsNullOrWhiteSpace(prefabAssetPath))
{
throw new InvalidOperationException("Enter a prefab asset path before creating a variant.");
}
var command = new BrokkrUnityCommand
{
commandId = StableId("unity-command", "prefab-variant", Guid.NewGuid().ToString("N")),
action = "createPrefabVariant",
assetPath = prefabAssetPath.Trim(),
name = prefabInstanceName.Trim(),
value = string.IsNullOrWhiteSpace(prefabVariantPath)
? "Assets/BrokkrPrefabVariant.prefab"
: prefabVariantPath.Trim()
};
PublishCommandAndPoll(command);
}
catch (Exception error)
{
SetStatus(error.Message, MessageType.Error);
}
}
private void PublishCommandAndPoll(BrokkrUnityCommand command)
{
mirror.PublishCommandAsync(command).GetAwaiter().GetResult();
PollAndExecuteCommand(true);
}
private void PublishSelectedObjectSync()
{
try

View file

@ -32,6 +32,9 @@ In Unity:
6. Click `Publish Mirror`.
7. To create a ScriptableObject through the mirror, fill `ScriptableObject Type`,
`Asset Name`, and `Asset Path`, then click `Create ScriptableObject Asset`.
8. To instantiate or variant a prefab through the mirror, fill `Prefab Asset
Path`, optional `Instance Name`, and optional `Variant Path`, then click the
matching prefab command.
The Unity adapter writes the latest editor snapshot to: