CreateAssetCommand.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using JetBrains.Annotations;
  4. using LLM.Editor.Core;
  5. using LLM.Editor.Helper;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using Object = UnityEngine.Object;
  9. namespace LLM.Editor.Commands
  10. {
  11. public enum AssetType
  12. {
  13. Folder,
  14. Text,
  15. Script,
  16. Material,
  17. AssemblyDefinition,
  18. Prefab
  19. }
  20. [Serializable]
  21. public class CreateAssetParams
  22. {
  23. public AssetType assetType;
  24. public string assetName;
  25. public string logicalName; // A unique name for the LLM to reference this object later
  26. public string path; // Relative to Assets folder
  27. public string content;
  28. public string sourceAssetGuid; // Optional, for creating prefabs from models
  29. public string sourceGameObjectIdentifier; // Optional, for creating prefabs from scene objects
  30. public string shaderName; // Optional, for creating materials
  31. }
  32. [UsedImplicitly]
  33. public class CreateAssetCommand : ICommand
  34. {
  35. private readonly CreateAssetParams _params;
  36. public CreateAssetCommand(string jsonParams)
  37. {
  38. _params = jsonParams?.FromJson<CreateAssetParams>();
  39. }
  40. public CommandOutcome Execute(Data.CommandContext context)
  41. {
  42. if (_params == null || string.IsNullOrEmpty(_params.assetName))
  43. {
  44. context.ErrorMessage = "Invalid parameters: assetName is required.";
  45. return CommandOutcome.Error;
  46. }
  47. var relativePath = _params.path?.Replace("Assets/", string.Empty) ?? "";
  48. var fullPath = Path.Combine(Application.dataPath, relativePath, _params.assetName);
  49. var assetPathForDb = $"Assets/{Path.Combine(relativePath, _params.assetName)}";
  50. var dirName = Path.GetDirectoryName(fullPath);
  51. if (!string.IsNullOrEmpty(dirName) && !Directory.Exists(dirName))
  52. {
  53. Directory.CreateDirectory(dirName);
  54. }
  55. try
  56. {
  57. switch (_params.assetType)
  58. {
  59. case AssetType.Folder:
  60. Directory.CreateDirectory(fullPath);
  61. assetPathForDb += "/";
  62. break;
  63. case AssetType.Text:
  64. File.WriteAllText(fullPath, _params.content ?? "");
  65. break;
  66. case AssetType.Script:
  67. assetPathForDb += ".cs";
  68. fullPath += ".cs";
  69. File.WriteAllText(fullPath, _params.content ?? "using UnityEngine;\n\npublic class NewScript : MonoBehaviour\n{\n}\n");
  70. break;
  71. case AssetType.Material:
  72. assetPathForDb += ".mat";
  73. var shader = string.IsNullOrEmpty(_params.shaderName) ? Shader.Find("Standard") : Shader.Find(_params.shaderName);
  74. if (shader == null)
  75. {
  76. Debug.LogWarning($"[CreateAssetCommand] Shader '{_params.shaderName}' not found. Falling back to Standard.");
  77. shader = Shader.Find("Standard");
  78. }
  79. var material = new Material(shader);
  80. AssetDatabase.CreateAsset(material, assetPathForDb);
  81. break;
  82. case AssetType.AssemblyDefinition:
  83. assetPathForDb += ".asmdef";
  84. var asmdefContent = _params.content ?? $"{{\n \"name\": \"{_params.assetName}\"\n}}";
  85. File.WriteAllText(assetPathForDb, asmdefContent);
  86. break;
  87. case AssetType.Prefab:
  88. assetPathForDb += ".prefab";
  89. CreatePrefab(context, assetPathForDb);
  90. break;
  91. default:
  92. context.ErrorMessage = $"Unsupported asset type: {_params.assetType}";
  93. return CommandOutcome.Error;
  94. }
  95. AssetDatabase.Refresh();
  96. // Folders don't have GUIDs in the same way assets do, so we handle them differently.
  97. // Their path is their identifier.
  98. if (_params.assetType == AssetType.Folder)
  99. {
  100. var logicalNameForFolder = !string.IsNullOrEmpty(_params.logicalName) ? _params.logicalName : $"{_params.assetName}_{Guid.NewGuid():N}";
  101. context.IdentifierMap[logicalNameForFolder] = assetPathForDb;
  102. SessionManager.SaveIdentifierMap(context.IdentifierMap);
  103. context.CurrentSubject = logicalNameForFolder;
  104. Debug.Log($"[CreateAssetCommand] Successfully created folder '{_params.assetName}' and mapped it to logical name '{logicalNameForFolder}'.");
  105. return CommandOutcome.Success;
  106. }
  107. // For all other assets, we require a valid GUID.
  108. var assetGuid = AssetDatabase.AssetPathToGUID(assetPathForDb);
  109. if (string.IsNullOrEmpty(assetGuid))
  110. {
  111. AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
  112. assetGuid = AssetDatabase.AssetPathToGUID(assetPathForDb);
  113. if (string.IsNullOrEmpty(assetGuid))
  114. {
  115. throw new Exception($"Could not get GUID for newly created asset at path: {assetPathForDb}");
  116. }
  117. }
  118. var logicalName = !string.IsNullOrEmpty(_params.logicalName) ? _params.logicalName : $"{_params.assetName}_{Guid.NewGuid():N}";
  119. context.IdentifierMap[logicalName] = assetGuid;
  120. SessionManager.SaveIdentifierMap(context.IdentifierMap);
  121. context.CurrentSubject = logicalName;
  122. Debug.Log($"[CreateAssetCommand] Successfully created asset '{_params.assetName}' and mapped it to logical name '{logicalName}'.");
  123. return CommandOutcome.Success;
  124. }
  125. catch (Exception e)
  126. {
  127. context.ErrorMessage = $"Failed to create asset '{_params.assetName}'. Exception: {e.Message}";
  128. Debug.LogException(e);
  129. return CommandOutcome.Error;
  130. }
  131. }
  132. private void CreatePrefab(Data.CommandContext context, string finalPrefabPath)
  133. {
  134. GameObject sourceObject = null;
  135. if (!string.IsNullOrEmpty(_params.sourceGameObjectIdentifier))
  136. {
  137. var resolvedObject = CommandUtility.ResolveIdentifier(context, _params.sourceGameObjectIdentifier);
  138. if (resolvedObject is GameObject go)
  139. {
  140. sourceObject = go;
  141. }
  142. else
  143. {
  144. throw new Exception($"Could not resolve scene object with logical name: '{_params.sourceGameObjectIdentifier}'");
  145. }
  146. }
  147. else if (!string.IsNullOrEmpty(_params.sourceAssetGuid))
  148. {
  149. var sourcePath = AssetDatabase.GUIDToAssetPath(_params.sourceAssetGuid);
  150. sourceObject = AssetDatabase.LoadAssetAtPath<GameObject>(sourcePath);
  151. if (sourceObject == null)
  152. {
  153. throw new Exception($"Could not find source asset with GUID: {_params.sourceAssetGuid}");
  154. }
  155. }
  156. else
  157. {
  158. sourceObject = new GameObject(_params.assetName);
  159. // Save the new empty object as a prefab, then immediately destroy the instance.
  160. PrefabUtility.SaveAsPrefabAsset(sourceObject, finalPrefabPath);
  161. Object.DestroyImmediate(sourceObject);
  162. return;
  163. }
  164. // For existing scene objects or assets, save as a prefab.
  165. PrefabUtility.SaveAsPrefabAssetAndConnect(sourceObject, finalPrefabPath, InteractionMode.UserAction);
  166. }
  167. }
  168. }