using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; public class PatternSystemEditor : EditorWindow { [MenuItem("Window/AddPatternToSystem")] static void AddPatternToSystem() { var gameManager = GameObject.Find("GameManager"); if (gameManager != null) { var patternManager = gameManager.GetComponent<PatternManager>(); if (Selection.gameObjects.Length == 1) { var item = Selection.gameObjects[0].transform.Find("Item"); if (item != null) { Pattern pattern = new Pattern(); foreach (var child in item) { Transform childTransform = child as Transform; if (childTransform != null) { var prefeb = UnityEditor.PrefabUtility.GetPrefabParent(childTransform.gameObject); if (prefeb != null) { PatternItem patternItem = new PatternItem { gameobject = prefeb as GameObject, position = childTransform.localPosition }; pattern.PatternItems.Add(patternItem); } } } patternManager.Patterns.Add(pattern); } } } } }
时间: 2024-02-10 19:19:02 浏览: 138
这是一个Unity编辑器脚本,用于添加游戏中的Pattern(模式)到PatternManager(模式管理器)中。它包含一个名为AddPatternToSystem()的静态方法,该方法可以通过Unity编辑器中的菜单栏中的"Window/AddPatternToSystem"进行调用。在方法中,首先查找名为"GameManager"的游戏物体,然后获取PatternManager组件。接下来,判断当前选中的游戏物体是否为一个包含名为"Item"的子对象的物体。如果是,就创建一个新的Pattern对象,并将其子物体中的每个Prefab及其位置信息添加到PatternItems列表中。最后将Pattern添加到PatternManager的Patterns列表中。这个脚本可以方便地添加游戏中的Pattern,减少手动操作的工作量。
相关问题
unity editor 调用deepseek
### 如何在 Unity 编辑器中集成和使用 DeepSeek
#### 创建 EditorWindow 来管理 DeepSeek 的配置
为了方便开发者管理和测试 DeepSeek 功能,在 Unity 编辑器内创建自定义窗口是一个不错的选择。通过 `EditorWindow` 类可以轻松实现这一点[^3]。
```csharp
using UnityEngine;
using UnityEditor;
public class DeepSeekConfigWindow : EditorWindow {
private string apiKey = "sk-d17be4a259504db3825c8d20d463dddd";
[MenuItem("Window/DeepSeek Config")]
public static void ShowWindow() {
GetWindow<DeepSeekConfigWindow>("DeepSeek Config");
}
void OnGUI() {
GUILayout.Label("DeepSeek API Key", EditorStyles.boldLabel);
apiKey = EditorGUILayout.TextField(apiKey);
if (GUILayout.Button("Test Connection")) {
Debug.Log($"Testing connection with key {apiKey}");
// Add code here to test the connection using provided API key.
}
}
}
```
此脚本允许用户输入自己的 DeepSeek API 密钥并提供了一个按钮来验证连接是否成功[^2]。
#### 使用 RESTful API 调用 DeepSeek 大模型
对于实际调用 DeepSeek 提供的服务来说,通常会采用 HTTP 请求的方式发送数据给服务器端处理。下面展示了一种简单的方法来进行同步请求:
```csharp
using System.Collections.Generic;
using UnityEngine.Networking;
IEnumerator CallDeepSeek(string message, System.Action<string> callback) {
var request = new UnityWebRequest(
url: $"https://api.deepseek.com/v1/chat",
method: "POST"
);
WWWForm form = new WWWForm();
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Authorization"] = $"Bearer sk-d17be4a259504db3825c8d20d463dddd";
foreach (var header in headers) {
request.SetRequestHeader(header.Key, header.Value);
}
byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonUtility.ToJson(new {
prompt = message,
max_tokens = 150
}));
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
yield return request.SendWebRequest();
while (!request.isDone) yield return null;
if (string.IsNullOrEmpty(request.error)) {
callback?.Invoke(request.downloadHandler.text);
} else {
Debug.LogError($"Error calling DeepSeek API: {request.error}");
}
}
```
这段代码展示了如何构建 POST 请求并将消息作为 JSON 发送到指定 URL 地址;同时设置了必要的认证头信息以便能够访问受保护资源。
#### 将上述组件结合起来实现在编辑器中的交互体验
最后一步就是把前面提到的功能组合起来形成完整的用户体验流程——即当玩家点击某个 UI 控件时触发向 AI 查询的操作,并显示返回的结果。这可以通过监听特定事件或绑定回调函数到相应控件上来完成。
```csharp
void StartConversation() {
StartCoroutine(CallDeepSeek("你好啊!", HandleResponse));
}
void HandleResponse(string responseText) {
// Process and display the received text from DeepSeek.
Debug.Log(responseText);
}
```
以上就是在 Unity 编辑器环境中集成了 DeepSeek 并实现了基本对话功能的具体方法[^1]。
阅读全文
相关推荐















