本文主要是介绍Timeline基本操作的代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文地址:https://blog.csdn.net/t163361/article/details/114371386
最近准备申请新星创作者,需要2000个粉丝关注,觉得文章有用的,请点一下左侧边栏的关注,谢谢。
19年左右调研Timeline的相关文档
PlayDirector
通过TimelineEditor可以获取当前主运行Timeline的PlayDirector的句柄。
可以实现运行时针对此Timeline的播放,结束,暂停,恢复,前后帧移动,刷新等功能
RefreshGUI刷新Timeline的编辑器窗口
代码如下
using System.Reflection;
using UnityEditor;
using UnityEditor.Timeline;
using UnityEngine;public class TimelineControl : EditorWindow
{[MenuItem("Tools/TimelineControl")]private static void ShowWindow(){var self = GetWindow(typeof(TimelineControl)) as TimelineControl;}private void OnGUI(){GUILayout.Space(7f);var dir = TimelineEditor.masterDirector;if (dir == null)return;if (GUILayout.Button("播放", GUILayout.Width(60f))){dir.Play();}if (GUILayout.Button("结束", GUILayout.Width(60f))){dir.Stop();}if (GUILayout.Button("暂停", GUILayout.Width(60f))){dir.Pause();}if (GUILayout.Button("恢复", GUILayout.Width(60f))){dir.Resume();}if (GUILayout.Button("前跳", GUILayout.Width(60f)))//根据播放模式不同,增加对应的帧或秒{dir.time += 1;RefreshData();}if (GUILayout.Button("后退", GUILayout.Width(60f)))//根据播放模式不同,减少对应的帧或秒{dir.time -= 1;RefreshData();}if (GUILayout.Button("重载刷新", GUILayout.Width(60f))){ReloadRefreshData();}if (GUILayout.Button("刷新界面", GUILayout.Width(60f))){RefreshGUI();}}/// <summary>/// 只刷新数据/// </summary>public void RefreshData(){var dir = TimelineEditor.masterDirector;if (dir == null)return;dir.Evaluate();}/// <summary>/// 重新加载Timeline数据,并恢复到当前帧/// </summary>public static void ReloadRefreshData(){var dir = TimelineEditor.masterDirector;if (dir == null)return;double oldT = dir.time;dir.Stop();dir.time = oldT;dir.Evaluate();}/// <summary>/// 通过反射刷新Timeline编辑窗口/// </summary>public static void RefreshGUI(){Assembly asmTE = Assembly.GetAssembly(typeof(TimelineEditor));var tw = asmTE.GetType("UnityEditor.Timeline.TimelineWindow");var gui = EditorWindow.GetWindow(tw, false);var twState = tw.GetProperty("state", BindingFlags.Instance | BindingFlags.Public);var stateValue = twState.GetValue(gui, null);var ws = asmTE.GetType("UnityEditor.Timeline.WindowState");var refreshMethod = ws.GetMethod("Refresh", BindingFlags.Instance | BindingFlags.Public);refreshMethod.Invoke(stateValue, new Object[] { });}
}
Track操作
实现动态添加一个ActivationTrack以及ActivationClip的功能
调用TimelineControl中的刷新函数,可以实现实时更新的效果
using UnityEditor;
using UnityEditor.Timeline;
using UnityEngine;
using UnityEngine.Timeline;public class TrackControl : EditorWindow
{[MenuItem("Tools/TrackControl")]private static void ShowWindow(){var self = GetWindow(typeof(TrackControl)) as TrackControl;}private void OnGUI(){if (GUILayout.Button("添加Track", GUILayout.Width(70f))){AddTrack();}if (GUILayout.Button("添加Clicp", GUILayout.Width(60f))){AddClip();} }/// <summary>/// 添加时间轨/// </summary>private void AddTrack(){var t = TimelineEditor.masterAsset;var x = t.CreateTrack<ActivationTrack>(null, "Track") as ActivationTrack;TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);}/// <summary>/// 添加剪辑/// </summary>private void AddClip(){var t = TimelineEditor.masterAsset;foreach(var tttt in t.GetRootTracks()){if (tttt is ActivationTrack)//获得第一个ActivationTrack轨道{var clip = tttt.CreateDefaultClip();clip.start = 6;clip.duration = 1;TimelineControl.ReloadRefreshData();break;}} }
}
这篇关于Timeline基本操作的代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!