2016-11-13 137 views
5

我想在EditorWindow的某个区域内有一个缩放效果,就像一个可缩放的滚动视图。如何平移/缩放GUI区域内的内容?

仅采用平移效果下面的代码片断交易,但它体现在可缩放的区域对剪切矩形里面的内容不能相互独立的,通过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

有没有办法做到这一点,也许用滚动视图?

回答

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可能对您有所帮助。

+0

不幸的是,它不起作用,如果您将窗口“hello”稍微移动到左上角,它会被剪切,无论您如何移动滑块,它都会被剪切。我认为这是因为GUI.matrix也适用于裁剪矩形,这对于GUI元素来说是有意义的,但是它没有留下自定义平移或缩放效果的空间。 Martin的文章中的代码有一个解决方法,它为每个GUI元素的位置添加一个增量,而不是添加到GUI.matrix – rraallvv