UnityEditorWindow做一个TimeLine的滑动块
最近在做一个基于TimeLine的动画编辑器,在制作TineLine滑动块时遇到问题,网上查了好久,试了好多GUI组件都不满意。
最后在一个unity TimeLine插件中找到合适的方法。 地址:https://github.com/sassembla/TimeFlowShiki .
一个TimeLine新开一个Window,单独监听一个Window的Drag事件。
下面是最终滑动条测试代码
using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; public class TimeLineWindowTest : EditorWindow { public static TimeLineWindowTest window; public int id = 1221; public GUISkin skin; [MenuItem("Window/TimeLineWindowTest")] static void OpenWindow() { window = GetWindow<TimeLineWindowTest>(); } Rect timeLineRect; Vector2 mouseLastPos = Vector2.zero; Vector2 mousePos = Vector2.zero; Rect trackRect = new Rect(200, 0, 150, 30); private void OnGUI() { BeginWindows(); GUI.BeginGroup(new Rect(200, 0, position.width, position.height)); { timeLineRect = new Rect(200, 0, position.width, 30); GUI.Window(id, timeLineRect, CallBack, "TimeLine1", skin.window); } GUI.EndGroup(); EndWindows(); } private void CallBack(int id) { bool useEvent = false; mousePos = Event.current.mousePosition; Vector2 mouseDownPos; switch (Event.current.type) { case EventType.MouseDown: if (trackRect.Contains(mousePos)) { mouseDownPos = mousePos; useEvent = true; } break; case EventType.MouseDrag: if (trackRect.Contains(mousePos)) { float xOffset = mousePos.x - mouseLastPos.x; trackRect.x += xOffset; Debug.Log("拖拽"); useEvent = true; } break; } DrawTracks(trackRect); GUI.DragWindow(); if (useEvent) { Event.current.Use(); } } private void Update() { mouseLastPos = mousePos; } private void DrawTracks(Rect timeLineRect) { GUI.Box(timeLineRect, "265645", skin.box); } }
注意一点Update(),只要是继承自EditorWindow中都可以调用每帧执行。
核心就这些。
运行效果如下: