Unity Editor Window Zooming
参考:
http://martinecker.com/martincodes/unity-editor-window-zooming/
http://answers.unity3d.com/questions/38224/how-can-i-zoom-out-properly-within-a-editorwindow.html
方法1:
using UnityEngine;
using System.Collections;public class Test : MonoBehaviour
{private Vector2 scale = new Vector2(1, 1);private Vector2 pivotPoint;void OnGUI(){Matrix4x4 oldMatrix = GUI.matrix;pivotPoint = new Vector2(Screen.width / 2, Screen.height / 2);GUIUtility.ScaleAroundPivot(scale, pivotPoint);if (GUI.Button(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 25, 50, 50), "Big!"))scale += new Vector2(0.5F, 0.5F);GUI.matrix = oldMatrix;if (GUI.Button(new Rect(Screen.width / 2 + 25, Screen.height / 2 - 25, 50, 50), "Big!"))scale += new Vector2(0.5F, 0.5F);}
}
方法2:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;public class ZoomTestWindow : EditorWindow
{[MenuItem("Window/Zoom Test")]private static void Init(){ZoomTestWindow window = EditorWindow.GetWindow<ZoomTestWindow>(false, "Zoom Test");window.minSize = new Vector2(600.0f, 300.0f);window.wantsMouseMove = true;window.Show();//EditorWindow.FocusWindowIfItsOpen();}private const float kZoomMin = 0.1f;private const float kZoomMax = 10.0f;private readonly Rect _zoomArea = new Rect(0.0f, 75.0f, 600.0f, 300.0f - 100.0f);private float _zoom = 1.0f;private Vector2 _zoomCoordsOrigin = Vector2.zero;private Vector2 ConvertScreenCoordsToZoomCoords(Vector2 screenCoords){return (screenCoords - _zoomArea.TopLeft()) / _zoom + _zoomCoordsOrigin;}private void DrawZoomArea(){// Within the zoom area all coordinates are relative to the top left corner of the zoom area// with the width and height being scaled versions of the original/unzoomed area's width and height.EditorZoomArea.Begin(_zoom, _zoomArea);GUI.Box(new Rect(0.0f - _zoomCoordsOrigin.x, 0.0f - _zoomCoordsOrigin.y, 100.0f, 25.0f), "Zoomed Box");// You can also use GUILayout inside the zoomed area.GUILayout.BeginArea(new Rect(300.0f - _zoomCoordsOrigin.x, 70.0f - _zoomCoordsOrigin.y, 130.0f, 50.0f));GUILayout.Button("Zoomed Button 1");GUILayout.Button("Zoomed Button 2");GUILayout.EndArea();EditorZoomArea.End();}private void DrawNonZoomArea(){GUI.Box(new Rect(0.0f, 0.0f, 600.0f, 50.0f), "Adjust zoom of middle box with slider or mouse wheel.\nMove zoom area dragging with middle mouse button or Alt+left mouse button.");_zoom = EditorGUI.Slider(new Rect(0.0f, 50.0f, 600.0f, 25.0f), _zoom, kZoomMin, kZoomMax);GUI.Box(new Rect(0.0f, 300.0f - 25.0f, 600.0f, 25.0f), "Unzoomed Box");}private void HandleEvents(){// Allow adjusting the zoom with the mouse wheel as well. In this case, use the mouse coordinates// as the zoom center instead of the top left corner of the zoom area. This is achieved by// maintaining an origin that is used as offset when drawing any GUI elements in the zoom area.if (Event.current.type == EventType.ScrollWheel){Vector2 screenCoordsMousePos = Event.current.mousePosition;Vector2 delta = Event.current.delta;Vector2 zoomCoordsMousePos = ConvertScreenCoordsToZoomCoords(screenCoordsMousePos);float zoomDelta = -delta.y / 150.0f;float oldZoom = _zoom;_zoom += zoomDelta;_zoom = Mathf.Clamp(_zoom, kZoomMin, kZoomMax);_zoomCoordsOrigin += (zoomCoordsMousePos - _zoomCoordsOrigin) - (oldZoom / _zoom) * (zoomCoordsMousePos - _zoomCoordsOrigin);Event.current.Use();}// Allow moving the zoom area's origin by dragging with the middle mouse button or dragging// with the left mouse button with Alt pressed.if (Event.current.type == EventType.MouseDrag &&(Event.current.button == 0 && Event.current.modifiers == EventModifiers.Alt) ||Event.current.button == 2){Vector2 delta = Event.current.delta;delta /= _zoom;_zoomCoordsOrigin += delta;Event.current.Use();}}public void OnGUI(){HandleEvents();// The zoom area clipping is sometimes not fully confined to the passed in rectangle. At certain// zoom levels you will get a line of pixels rendered outside of the passed in area because of// floating point imprecision in the scaling. Therefore, it is recommended to draw the zoom// area first and then draw everything else so that there is no undesired overlap.DrawZoomArea();DrawNonZoomArea();}
}
using UnityEngine;
using System;// Helper Rect extension methods
public static class RectExtensions
{public static Vector2 TopLeft(this Rect rect){return new Vector2(rect.xMin, rect.yMin);}public static Rect ScaleSizeBy(this Rect rect, float scale){return rect.ScaleSizeBy(scale, rect.center);}public static Rect ScaleSizeBy(this Rect rect, float scale, Vector2 pivotPoint){Rect result = rect;result.x -= pivotPoint.x;result.y -= pivotPoint.y;result.xMin *= scale;result.xMax *= scale;result.yMin *= scale;result.yMax *= scale;result.x += pivotPoint.x;result.y += pivotPoint.y;return result;}public static Rect ScaleSizeBy(this Rect rect, Vector2 scale){return rect.ScaleSizeBy(scale, rect.center);}public static Rect ScaleSizeBy(this Rect rect, Vector2 scale, Vector2 pivotPoint){Rect result = rect;result.x -= pivotPoint.x;result.y -= pivotPoint.y;result.xMin *= scale.x;result.xMax *= scale.x;result.yMin *= scale.y;result.yMax *= scale.y;result.x += pivotPoint.x;result.y += pivotPoint.y;return result;}
}public class EditorZoomArea
{private const float kEditorWindowTabHeight = 21.0f;private static Matrix4x4 _prevGuiMatrix;public static Rect Begin(float zoomScale, Rect screenCoordsArea){GUI.EndGroup(); // End the group Unity begins automatically for an EditorWindow to clip out the window tab. This allows us to draw outside of the size of the EditorWindow.Rect clippedArea = screenCoordsArea.ScaleSizeBy(1.0f / zoomScale, screenCoordsArea.TopLeft());clippedArea.y += kEditorWindowTabHeight;GUI.BeginGroup(clippedArea);_prevGuiMatrix = GUI.matrix;Matrix4x4 translation = Matrix4x4.TRS(clippedArea.TopLeft(), Quaternion.identity, Vector3.one);Matrix4x4 scale = Matrix4x4.Scale(new Vector3(zoomScale, zoomScale, 1.0f));GUI.matrix = translation * scale * translation.inverse * GUI.matrix;return clippedArea;}public static void End(){GUI.matrix = _prevGuiMatrix;GUI.EndGroup();GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height));}
}
理解:
algorithm for scaling an image from a given pivot point:http://stackoverflow.com/questions/3598538/algorithm-for-scaling-an-image-from-a-given-pivot-point
Well, I don't know what framework/library you're using but you can think of it as:
- translation to make your pivot point the center point
- standard scaling
- opposite transalation to make the center point the original pivot point
Translation and scaling are isomorphismes so you can represent them as matrix. Each transformation is a matrix and you can multiply them for find the combined transformation matrix. So:
- T = transformation
- S = scalling
- T' = opposite transformation
If you apply T.x being x a point vector it gives you the new coordinates. The same for S.x.
So if you want to do that operations you have to do: T'. (S. (T.x))
I think you can associate operations so it's the same as (T'.S.T).x
If you are using a framework apply three operations (or combine operations and apply). If you are using crude math... go the matrix way :)
Unity Editor Window Zooming相关推荐
- Unity报错之 No Sprite Editor Window registered. Please download 2D Sprite package from Package Manager.
Unity报错之 No Sprite Editor Window registered. Please download 2D Sprite package from Package Manager. ...
- Unity editor 快速上手 quick start
Unity editor 快速上手 quick start Warning:非干货,是比较个人向的快速上手的代码例子,不一定是最好的方法,但能work. 主要目的是做一个可以动态添加和移除新的行的自定 ...
- 进化Unity Editor UX
A new design signifies the first step towards improving the user experience (UX) for an ever-evolvin ...
- Unity Editor 基础篇(四):Handles
本文参自: 克森http://mp.weixin.qq.com/s/qxsKDPjJS30S9OXeQ8WKTw 本文为本人学习上链接的笔记微有改动,请点击以上链接查看原文,尊重楼主知识产权. Uni ...
- Unity编辑器拓展之六:利用反射打开Unity Preferences Window
博客迁移 个人博客站点,欢迎访问,www.jiingfengji.tech 如何利用反射打开Unity Preferences Window Unity Preferences Window如下图所示 ...
- Unity Editor已停止工作
在更换系统之后,可能会出现打开刚安装好的Unity,显示Unity Editor已停止工作,这时候我们考虑是系统win7的问题.可以在原系统上升级,也可以重新安装,升级.文中所涉及到的软件,可在右侧加 ...
- Unity Editor Built-in Icons (Unity version: 2018.3.0f2)
Unity Editor Built-in Icons Unity version: 2018.3.0f2 Icons what can load using EditorGUIUtility.Ico ...
- 【Unity Editor工具制作-文本转UTF-8编码、用WPS表格打开表格、用WPS表格打开】
Unity Editor工具制作 文本转UTF-8编码 用NotePad打开 用WPS表格打开 文本转UTF-8编码 [MenuItem("Assets/文本转UTF-8编码")] ...
- Unity教程之-制作闪亮的星星Star(三):给Star创建Unity Editor编辑器
继续上篇文章<Unity教程之-制作闪亮的星星Star(二):创建Shader>,本篇我们来讲解 unity star editor的创建! 建立编辑器 Creating the Insp ...
最新文章
- 吹灭蛋糕上蜡烛的节能小车
- Objective-C中把URL请求的参数转换为字典
- C#-Stmp发邮件
- LeetCode:2. Add Two Numbers
- Android事件处理--读书笔记
- Eclipse Java注释模板设置
- idea整合jboos_在 idea 中 启动 jboss 后, 没有运行部署(通过idea部署)的ssm项目,打开后项目404...
- ES6/07/Array的扩展方法,...扩展运算符,Array.from(),(arr.find(),arr.findIndex()和arr.includes())模板字符串,Set数据结构
- 数学归纳法证明时间复杂度
- MySQL 5.6内存占用过高解决方案
- 【转】计算机键盘功能键作用
- 离散数学 习题篇 —— 关系的性质
- Eterm系统出-航空公司大系统-PID放大软件-IBE查询接口 QQ 799670351 15075679773
- Windows 10 微软拼音输入法无法输入中文标点符号
- 最难忘的一节计算机课,写最难忘的一节课作文8篇
- 想不到吧,实体类能自己CRUD,MyBatis-Plus AR模式了解下
- 统计学计算机数据输入,数据输入是什么意思
- https双向认证java
- java调用kettle脚本ktr
- react 返回上一页
热门文章
- 两年数据对比柱形图_2018年、2019年的数据对比图!想学习这种对比图的做法!安排...
- 中国科学院的研究所很难进吗?
- 获取矩阵内非零元素坐标
- win10自动关机设置_电脑小技巧设置自动关机
- debian之网易云音乐的安装
- 怎样写标题才能获得流量,写标题的技巧
- 计算机英语 教学大纲,计算机英语教学大纲
- Android电视开机进入AV,康佳电视如何设置开机成AV模式-康佳开机直接进电视
- (附源码)spring boot基于微信小程序的口腔诊所预约系统 毕业设计 201738
- 人脸识别嵌入式Linux芯片瑞芯微RV1109参数介绍