Unity 分隔条 Spliter

本文介绍了一个使用Unity创建的自定义编辑器窗口SplitViewWindow,该窗口通过一个可调整大小的分隔条实现了上下两部分视图的展示,并允许用户通过鼠标拖动来调整分界线的位置。

splitview

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using UnityEditor;  
  3.   
  4. public class SplitViewWindow : EditorWindow  
  5. {  
  6.     private Vector2 scrollPos = Vector2.zero;  
  7.     float currentScrollViewHeight;  
  8.     bool resize = false;  
  9.     Rect cursorChangeRect;  
  10.   
  11.     [MenuItem("MyWindows/SplitView")]  
  12.     public static void Init()  
  13.     {  
  14.         EditorWindow t = GetWindow<SplitViewWindow>();  
  15.     }  
  16.   
  17.     void OnEnable()  
  18.     {  
  19.         this.position = new Rect(200, 200, 400, 300);  
  20.         currentScrollViewHeight = this.position.height / 2;  
  21.         cursorChangeRect = new Rect(0, currentScrollViewHeight, this.position.width, 5f);  
  22.     }  
  23.   
  24.     void OnGUI()  
  25.     {  
  26.         GUILayout.BeginVertical();  
  27.         scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Height(currentScrollViewHeight));  
  28.         for (int i = 0; i < 20; i++)  
  29.             GUILayout.Label("dfs");  
  30.         GUILayout.EndScrollView();  
  31.   
  32.         ResizeScrollView();  
  33.   
  34.         GUILayout.FlexibleSpace();  
  35.         GUILayout.Label("Lower part");  
  36.   
  37.         GUILayout.EndVertical();  
  38.         Repaint();  
  39.     }  
  40.   
  41.     private void ResizeScrollView()  
  42.     {  
  43.         GUI.color = Color.blue;  
  44.         GUI.DrawTexture(cursorChangeRect, EditorGUIUtility.whiteTexture);  
  45.         EditorGUIUtility.AddCursorRect(cursorChangeRect, MouseCursor.ResizeVertical);  
  46.   
  47.         if (Event.current.type == EventType.mouseDown && cursorChangeRect.Contains(Event.current.mousePosition))  
  48.         {  
  49.             resize = true;  
  50.         }  
  51.         if (resize)  
  52.         {  
  53.             currentScrollViewHeight = Event.current.mousePosition.y;  
  54.             cursorChangeRect.Set(cursorChangeRect.x, currentScrollViewHeight, cursorChangeRect.width, cursorChangeRect.height);  
  55.         }  
  56.         if (Event.current.type == EventType.MouseUp)  
  57.             resize = false;  
  58.     }  
  59. }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值