2025-04-06 Unity Editor 2 —— GUILayout

常用组件

​ 创建 GUILayoutExample.cs 脚本,继承 EditorWindow。

public class GUILayoutExample : EditorWindow
{
    ...

    [MenuItem("EditorExtension/02.IMGUI/01.GUILayoutExample")]
    static void OpenGUILayoutExample()
    {
        GetWindow<GUILayoutExample>().Show();
    }

    private void OnGUI()
    {
        ... // 在这里编写面板控件
    }
}

1 Label 文本标签

  • GUILayout.LabelField(string文本内容);
private void OnGUI()
{
    ...
        
    GUILayout.Label("Label: Hello IMGUI");
    
    ...
}
image-20250406025135416

2 TextField / TextArea / PasswordField 输入框

  • string变量 = GUILayout.TextField(string变量);

    • 单行输入,不可以 Enter 换行。
  • string变量 = GUILayout.TextArea(string变量);

    • 多行输入,可以 Enter 换行。
  • string变量 = GUILayout.PasswordField(string变量, char掩码);

    • 密码输入框,输入内容会显示 char 掩码。
private string _textfieldValue;
private string _textAreaValue;
private string _passwordFieldValue = "";

private void OnGUI()
{
    ...
        
    GUILayout.Label("TextField");
    _textfieldValue = GUILayout.TextField(_textfieldValue);

    GUILayout.Label("TextArea");
    _textAreaValue = GUILayout.TextArea(_textAreaValue);

    GUILayout.Label("PasswordField");
    _passwordFieldValue = GUILayout.PasswordField(_passwordFieldValue, '*');
    
    ...
}
image-20250406030137173

3 Butto / RepeatButton 按钮

  • if (GUILayout.Button(string按钮名称)) { ... }

    按下时触发。

  • if (GUILayout.RepeatButton(string按钮名称)) { ... }

    按下和松开都会触发。

private void OnGUI()
{
    ...
        
    if (GUILayout.Button("Button"))
    {
        Debug.Log("Button Clicked");
    }
    
    // 按下松开都会触发一次
    if (GUILayout.RepeatButton("RepeatButton"))
    {
        Debug.Log("RepeatButton Clicked");
    }
    
    ...
}
image-20250406030529256

4 Horizontal / Vertical 方向布局

  • 使用 GUILayout.BeginHorizontal();GUILayout.EndHorizontal(); 包围代码块,使代码块的内容水平排列。

  • 使用 GUILayout.BeginVertical ();GUILayout.EndVertical (); 包围代码块,使代码块的内容垂直排列。

​ 默认排列方式为垂直排列。

private void OnGUI()
{
    ...
        
    GUILayout.BeginHorizontal();
    { // 使用大括号缩进表明排列关系
        GUILayout.Label("TextField");
        _textfieldValue = GUILayout.TextField(_textfieldValue);
    }
    GUILayout.EndHorizontal();
    
    ...
}
image-20250406031046470

5 Box 自动布局框

  • GUILayout.Box(string文本内容);

    使用包围盒包裹文本内容(深色区域)。

private void OnGUI()
{
    ...
        
    GUILayout.BeginHorizontal();
    {
        GUILayout.Label("Box");
        GUILayout.Box("AutoLayout Box");
    }
    GUILayout.EndHorizontal();
    
    ...
}
image-20250406031146685

6 ScrollView 滚动视图

  • Vector2布局 = GUILayout.BeginScrollView(Vector2布局); // 开启滚动视图
    ...
    EditorGUILayout.EndScrollView(); // 结束滚动视图

    当区域不够显示全部内容时,启用滑动条呈现滚动视图。

private Vector2 _scrollPosition;

private void OnGUI()
{
    ...
        
    _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
    {
        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("TextField");
            _textfieldValue = GUILayout.TextField(_textfieldValue);
        }
        GUILayout.EndHorizontal();
        
        ...
    }
    GUILayout.EndScrollView();
    
    ...
}
image-20250406031555341

7 Horizontal / VerticalSlider 滑动条

  • float变量 = GUILayout.HorizontalSlider(float变量, 最小值, 最大值);
  • float变量 = GUILayout.VerticalSlider(float变量, 最小值, 最大值);
private float _sliderValue;

private void OnGUI()
{
    ...
        
    GUILayout.BeginHorizontal();
    {
        GUILayout.Label("HorizontalSlider");
        _sliderValue = GUILayout.HorizontalSlider(_sliderValue, 0, 1);
    }
    GUILayout.EndHorizontal();

    GUILayout.BeginHorizontal();
    {
        GUILayout.Label("VerticalSlider");
        _sliderValue = GUILayout.VerticalSlider(_sliderValue, 0, 1);
    }
    GUILayout.EndHorizontal();
    
    ...
}
image-20250406031832206

8 Area GUI 区域

  • GUILayout.BeginArea(Rect布局位置);

    开启一块区域。

private void OnGUI()
{
    ...
        
    GUILayout.BeginArea(new Rect(0, 0, 100, 100));
    {
        // 显示重合了
        GUI.Label(new Rect(0, 0, 20, 20), "1");
    }
    GUILayout.EndArea();
    
    ...
}
image-20250406032101686

9 Window 窗口

  • public static Rect Window(int标识ID, Rect布局位置, GUI.WindowFunction绘制函数, string窗口标题);

    在 EditorWindow 中,该窗口无法显示。

    可在 RunTime 模式下显示。

private void OnGUI()
{
    ...
        
    // 目前不可见
    GUILayout.Window(1, new Rect(0, 0, 100, 100), id => { }, "Window");
    
    ...
}

10 Toolbar 工具栏

  • int下标 = GUILayout.Toolbar(int下标, string[]显示名称);
private int _toolBarIndex;

private void OnGUI()
{
    ...
        
    _toolBarIndex = GUILayout.Toolbar(_toolBarIndex, new[] { "1", "2", "3", "4", "5" });
    
    ...
}
image-20250406032917893

11 Toggle 开关按钮

  • bool开关 = GUILayout.Toggle(bool开关, string名称);
private bool _toggleValue;

private void OnGUI()
{
    ...
        
    _toggleValue = GUILayout.Toggle(_toggleValue, "Toggle");
    
    ...
}
image-20250406033041472

12 Space / FlexibleSpace 空白

  • GUILayout.Space(int间距);

    空出给定间距。

  • GUILayout.FlexibleSpace();

    向两边扩张,挤压中间区域

private void OnGUI()
{
    ...
        
     GUILayout.BeginHorizontal();
    {
        GUILayout.Label("TextField");
        _textfieldValue = GUILayout.TextField(_textfieldValue);
    }
    GUILayout.EndHorizontal();

    GUILayout.Space(100); // 间距 100

    ...

    GUILayout.BeginHorizontal();
    {
        GUILayout.Label("Button");

        GUILayout.FlexibleSpace(); // 挤压中间区域,将 Button 推到右边

        if (GUILayout.Button("Button"))
        {
            Debug.Log("Button Clicked");
        }
    }
    GUILayout.EndHorizontal();
    
    ...
}

​ 下图 “1” 表示垂直布局间隔 100,“2” 表示 Button 被水平挤压到最小。

image-20250406033245882

13 Width / Height / MinWidth / MinHeight / MaxWidth / MaxHeight 宽高控制

  • GUILayout.MinWidth(int值);
  • GUILayout.MaxWidth(int值);
  • GUILayout.MinHeight(int值);
  • GUILayout.MaxHeight(int值);
private void OnGUI()
{
    ...
        
    GUILayout.BeginHorizontal();
    {
        GUILayout.Label("Button");

        GUILayout.FlexibleSpace(); // 挤压中间区域,将 Button 推到右边

        if (GUILayout.Button(
                "Button",
                GUILayout.MinWidth(100), GUILayout.MaxWidth(150),
                GUILayout.MinHeight(100), GUILayout.MaxHeight(150)))
        {
            Debug.Log("Button Clicked");
        }
    }
    GUILayout.EndHorizontal();
    
    ...
}
image-20250406033734497

14 SelectionGrid 选择网格

  • int下标 = GUILayout.SelectionGrid(int下标, string[]名称, int水平数量);
private int _selectedGridIndex;

private void OnGUI()
{
    ...
        
    _selectedGridIndex = GUILayout.SelectionGrid(_selectedGridIndex, new[] { "1", "2", "3", "4", "5" }, 3);
    
    ...
}
image-20250406033918637
### 如何在 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]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔗理苦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
OSZAR »