首页 技术 正文
技术 2022年11月19日
0 收藏 469 点赞 2,673 浏览 1544 个字

UnityEditorWindow做一个TimeLine的滑动块

最近在做一个基于TimeLine的动画编辑器,在制作TineLine滑动条时遇到问题,网上查了好久,试了好多GUI组件都不满意。
最后在一个开源unity TimeLine插件中找到合适的方法。 地址:https://github.com/sassembla/TimeFlowShiki .
大致上是将Window当做一个滑动块,可以监听到鼠标Event.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中都可以调用每帧执行。

核心就这些。很简单,是不是!

运行效果如下:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,034
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,149
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,782
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,863