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

时间:2016-11-13 14:46:19

标签: unity3d

我希望在EditorWindow的某个区域内有一个缩放效果,就像可缩放的滚动视图一样。

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

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

public class ZoomTestWindow : 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()
    {
        ZoomTestWindow window = EditorWindow.GetWindow<ZoomTestWindow>("Zoom Test", true, new System.Type[] {
            typeof(UnityEditor.SceneView),
            typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow")});

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

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

        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.matrix = _prevGuiMatrix;
        GUI.EndGroup();
        GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
    }

    public void OnGUI()
    {
        BeginZoomArea();

        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(200, 5, 100, 30), hSliderValue1, 0, 100);
        hSliderValue2 = GUI.HorizontalSlider(new Rect(200, 25, 100, 30), hSliderValue2, 0, 100);
    }

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

enter image description here

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

1 个答案:

答案 0 :(得分:3)

将由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();
    }
}

result

它有效,但我不知道为什么。因为GUI.matrixBeginGroup(rect)的互动未知。

PS:This post可能对您有帮助。