Unity3d 如何平移/缩放GUI区域内的内容?

Unity3d 如何平移/缩放GUI区域内的内容?,unity3d,Unity3d,我想在EditorWindow中的某个区域内具有缩放效果,类似于可缩放的滚动视图 下面的代码片段只处理平移效果,但它举例说明了当可缩放区域和剪切矩形内的内容无法通过hSliderValue1(剪切)和hSliderValue2(平移)彼此独立处理时我遇到的问题 使用系统; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEditor; 公共类ZoomTestWindow:EditorWindow { 专用静态浮点kEditorWindowT

我想在EditorWindow中的某个区域内具有缩放效果,类似于可缩放的滚动视图

下面的代码片段只处理平移效果,但它举例说明了当可缩放区域和剪切矩形内的内容无法通过
hSliderValue1
(剪切)和
hSliderValue2
(平移)彼此独立处理时我遇到的问题

使用系统;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
公共类ZoomTestWindow:EditorWindow
{
专用静态浮点kEditorWindowTabHeight=20;
私有静态矩阵x4x4_;
公共静态浮点hSliderValue1=0;
公共静态浮点hSliderValue2=0;
Rect[]wr=新Rect[]{
新的Rect(0,0,100,100),
新Rect(50,50,100,100),
新矩形(100100100100)
};
[菜单项(“窗口/缩放测试”#%w”)]
私有静态void Init()
{
ZoomTestWindow window=EditorWindow.GetWindow(“缩放测试”,真,新系统。类型[]{
类型(UnityEdit.SceneView),
typeof(EditorWindow.Assembly.GetType(“UnityEditor.scenehierarchyindow”)};
window.Show();
FocusWindowIfItsOpen();
}
公共静态Rect BeginZoomArea()
{
GUI.EndGroup();//结束Unity开始的组,这样我们就不受EditorWindow的约束
BeginGroup(新的Rect(hSliderValue1,02000));
_prevGuiMatrix=GUI.matrix;
GUI.matrix=Matrix4x4.TRS(新的Vector2(hSliderValue2,0),Quaternion.identity,Vector3.one);;
返回新的Rect();
}
公共静态void EndZoomArea()
{
GUI.matrix=\u prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(新的Rect(0.0f,kEditorWindowTabHeight,Screen.width,Screen.height-(kEditorWindowTabHeight+3));
}
公营机构
{
BeginZoomArea();
BeginWindows();
wr[0]=GUI.Window(0,wr[0],DrawWindow,“hello”);
wr[1]=GUI.Window(1,wr[1],DrawWindow,“世界”);
wr[2]=GUI.Window(2,wr[2],DrawWindow,“!”;
EndWindows();
EndZoomArea();
hSliderValue1=GUI.HorizontalSlider(新的Rect(200,5100,30),hSliderValue1,0100);
hSliderValue2=GUI.HorizontalSlider(新的Rect(200,25100,30),hSliderValue2,0100);
}
无效绘图窗口(内部id)
{
按钮(新的矩形(0,30,100,50),“Wee!”);
GUI.DragWindow();
}
}


是否有办法做到这一点,可以使用滚动视图?

将由
hSliderValue1
控制的组嵌入到新组中

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ZoomMoveTestWindow: EditorWindow
{
    private static float kEditorWindowTabHeight = 20;
    private static Matrix4x4 _prevGuiMatrix;
    public static float hSliderValue1 = 0;
    public static float hSliderValue2 = 0;

    Rect[] wr = new Rect[]{
        new Rect(0, 0, 100, 100),
        new Rect(50, 50, 100, 100),
        new Rect(100, 100, 100, 100)
    };

    [MenuItem("Window/Zoom Test #%w")]
    private static void Init()
    {
        ZoomMoveTestWindow window = EditorWindow.GetWindow<ZoomMoveTestWindow>("Zoom Test", true, new System.Type[] {
            typeof(UnityEditor.SceneView),
            typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow")});

        window.Show();
        EditorWindow.FocusWindowIfItsOpen<ZoomMoveTestWindow>();
    }

    public static Rect BeginZoomArea(Rect rect)
    {
        GUI.BeginGroup(rect);

        GUI.BeginGroup(new Rect(hSliderValue1, 0, 200, 200));

        _prevGuiMatrix = GUI.matrix;

        GUI.matrix = Matrix4x4.TRS(new Vector2(hSliderValue2, 0), Quaternion.identity, Vector3.one);

        return new Rect();
    }

    public static void EndZoomArea()
    {
        GUI.EndGroup();

        GUI.matrix = _prevGuiMatrix;
        GUI.EndGroup();
        GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
    }

    public void OnGUI()
    {
        GUI.EndGroup(); //End the group that Unity began so we're not bound by the EditorWindow

        BeginZoomArea(new Rect(10,10, 200, 200));

        BeginWindows();
        wr[0] = GUI.Window(0, wr[0], DrawWindow, "hello");
        wr[1] = GUI.Window(1, wr[1], DrawWindow, "world");
        wr[2] = GUI.Window(2, wr[2], DrawWindow, "!");
        EndWindows();

        EndZoomArea();

        hSliderValue1 = GUI.HorizontalSlider(new Rect(250, 5, 100, 30), hSliderValue1, 0, 100);
        hSliderValue2 = GUI.HorizontalSlider(new Rect(250, 35, 100, 30), hSliderValue2, 0, 100);
    }

    void DrawWindow(int id)
    {
        GUI.Button(new Rect(0, 30, 100, 50), "Wee!");
        GUI.DragWindow();
    }
}
使用系统;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
公共类ZoomMoveTestWindow:EditorWindow
{
专用静态浮点kEditorWindowTabHeight=20;
私有静态矩阵x4x4_;
公共静态浮点hSliderValue1=0;
公共静态浮点hSliderValue2=0;
Rect[]wr=新Rect[]{
新的Rect(0,0,100,100),
新Rect(50,50,100,100),
新矩形(100100100100)
};
[菜单项(“窗口/缩放测试”#%w”)]
私有静态void Init()
{
ZoomMoveTestWindow window=EditorWindow.GetWindow(“缩放测试”,真,新系统。类型[]{
类型(UnityEdit.SceneView),
typeof(EditorWindow.Assembly.GetType(“UnityEditor.scenehierarchyindow”)};
window.Show();
FocusWindowIfItsOpen();
}
公共静态Rect BeginZoomArea(Rect Rect)
{
GUI.BeginGroup(rect);
BeginGroup(新的Rect(hSliderValue1,02000));
_prevGuiMatrix=GUI.matrix;
GUI.matrix=Matrix4x4.TRS(新的Vector2(hSliderValue2,0),Quaternion.identity,Vector3.one);
返回新的Rect();
}
公共静态void EndZoomArea()
{
GUI.EndGroup();
GUI.matrix=\u prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(新的Rect(0.0f,kEditorWindowTabHeight,Screen.width,Screen.height-(kEditorWindowTabHeight+3));
}
公营机构
{
GUI.EndGroup();//结束Unity开始的组,这样我们就不受EditorWindow的约束
BeginZoomArea(新Rect(10,10200200));
BeginWindows();
wr[0]=GUI.Window(0,wr[0],DrawWindow,“hello”);
wr[1]=GUI.Window(1,wr[1],DrawWindow,“世界”);
wr[2]=GUI.Window(2,wr[2],DrawWindow,“!”;
EndWindows();
EndZoomArea();
hSliderValue1=GUI.HorizontalSlider(新的Rect(250,5100,30),hSliderValue1,0100);
hSliderValue2=GUI.HorizontalSlider(新的Rect(250,3510030),hSliderValue2,0100);
}
无效绘图窗口(内部id)
{
按钮(新的矩形(0,30,100,50),“Wee!”);
GUI.DragWindow();
}
}

这很有效,但我不知道为什么。因为
GUI.matrix
BeginGroup(rect)
之间的交互是未知的


PS:可能会对您有所帮助。

不幸的是,如果您将窗口“hello”稍微移动到左上角,它会被剪裁,并且无论您如何移动滑块,它都会继续被剪裁。我认为这是因为GUI.matrix也应用于clipping rect,这对于GUI元素来说是有意义的,但它没有为自定义平移或缩放效果留下任何空间。Martin文章中的代码有一个变通方法,即在每个GUI元素的位置上添加一个增量,而不是在GUI.matrix中
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ZoomMoveTestWindow: EditorWindow
{
    private static float kEditorWindowTabHeight = 20;
    private static Matrix4x4 _prevGuiMatrix;
    public static float hSliderValue1 = 0;
    public static float hSliderValue2 = 0;

    Rect[] wr = new Rect[]{
        new Rect(0, 0, 100, 100),
        new Rect(50, 50, 100, 100),
        new Rect(100, 100, 100, 100)
    };

    [MenuItem("Window/Zoom Test #%w")]
    private static void Init()
    {
        ZoomMoveTestWindow window = EditorWindow.GetWindow<ZoomMoveTestWindow>("Zoom Test", true, new System.Type[] {
            typeof(UnityEditor.SceneView),
            typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow")});

        window.Show();
        EditorWindow.FocusWindowIfItsOpen<ZoomMoveTestWindow>();
    }

    public static Rect BeginZoomArea(Rect rect)
    {
        GUI.BeginGroup(rect);

        GUI.BeginGroup(new Rect(hSliderValue1, 0, 200, 200));

        _prevGuiMatrix = GUI.matrix;

        GUI.matrix = Matrix4x4.TRS(new Vector2(hSliderValue2, 0), Quaternion.identity, Vector3.one);

        return new Rect();
    }

    public static void EndZoomArea()
    {
        GUI.EndGroup();

        GUI.matrix = _prevGuiMatrix;
        GUI.EndGroup();
        GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
    }

    public void OnGUI()
    {
        GUI.EndGroup(); //End the group that Unity began so we're not bound by the EditorWindow

        BeginZoomArea(new Rect(10,10, 200, 200));

        BeginWindows();
        wr[0] = GUI.Window(0, wr[0], DrawWindow, "hello");
        wr[1] = GUI.Window(1, wr[1], DrawWindow, "world");
        wr[2] = GUI.Window(2, wr[2], DrawWindow, "!");
        EndWindows();

        EndZoomArea();

        hSliderValue1 = GUI.HorizontalSlider(new Rect(250, 5, 100, 30), hSliderValue1, 0, 100);
        hSliderValue2 = GUI.HorizontalSlider(new Rect(250, 35, 100, 30), hSliderValue2, 0, 100);
    }

    void DrawWindow(int id)
    {
        GUI.Button(new Rect(0, 30, 100, 50), "Wee!");
        GUI.DragWindow();
    }
}