创建Prefab
static void CreatePrefab(GameObject go, string path, string name)
{
//先创建一个空的预制物体
//预制物体保存在工程中路径,可以修改("Assets/" + name + ".prefab");
GameObject tempPrefab = PrefabUtility.CreatePrefab(path + name + ".prefab", go);
}
创建Playable
static TimelineAsset CreateTimelineAsset(string path, string name)
{
var asset = TimelineAsset.CreateInstance<TimelineAsset>();
AssetDatabase.CreateAsset(asset, path + name +".playable");
var track = asset.CreateTrack<InitializeTrackAsset>(null);
var clip = track.CreateClip<InitializeClipAsset>();
AssetDatabase.SaveAssets();
return asset;
}
完整代码
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public class TimelineCreateEditor: EditorWindow
{
static string _timelineAssetRootPath = "Assets/AssetBundleRes/Base/TimelineAsset";
private int createNums = 0;
private static string savePath;
[MenuItem("Assets/创建Timeline", true)]
[MenuItem("Assets/创建Timelines", true)]
public static bool ValidateMakeSelected()
{
string selectPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
savePath = selectPath+"/";
var isParent = selectPath.Equals(_timelineAssetRootPath);
selectPath = selectPath.Substring(0, selectPath.LastIndexOf("/")+1);
return selectPath.Contains(_timelineAssetRootPath) || isParent;
}
[MenuItem("Assets/创建Timeline")]
public static void CreateTimelineMenu()
{
CreateTimeline();
}
[MenuItem("Assets/创建Timelines")]
public static void CreateTimelineDialog()
{
GetWindow(typeof(TimelineCreateEditor));
}
void OnGUI()
{
//创建要显示在编辑窗口的内容
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
createNums = EditorGUILayout.IntField("请输入个数", createNums);
if (GUILayout.Button("创建"))
{
Debug.Log("--------------------- Timeline Create start -----------------------");
for (int i = 0; i < createNums; i++)
{
CreateTimeline();
}
Debug.Log("--------------------- Timeline Create finish -----------------------");
}
}
static void CreateTimeline()
{
Debug.Log($"save path {savePath}");
var dateTime = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff");
// 1.创建对象
var go = new GameObject();
var sub = new GameObject();
// 2.创建playable
var playable = CreateTimelineAsset(savePath, dateTime);
// 3.创建PlayableDirector
var dir = sub.AddComponent<UnityEngine.Playables.PlayableDirector>();
dir.timeUpdateMode = UnityEngine.Playables.DirectorUpdateMode.GameTime;
dir.extrapolationMode = UnityEngine.Playables.DirectorWrapMode.Hold;
dir.playOnAwake = false;
dir.playableAsset = playable;
var helper = sub.AddComponent<TimelinePlayHelper>();
helper.m_director = dir;
sub.name = "_theDirector";
sub.transform.parent = go.transform;
go.name = dateTime;
// 保存成预制体
CreatePrefab(go, savePath, dateTime);
}
static void CreatePrefab(GameObject go, string path, string name)
{
//先创建一个空的预制物体
//预制物体保存在工程中路径,可以修改("Assets/" + name + ".prefab");
GameObject tempPrefab = PrefabUtility.CreatePrefab(path + name + ".prefab", go);
}
static TimelineAsset CreateTimelineAsset(string path, string name)
{
var asset = TimelineAsset.CreateInstance<TimelineAsset>();
AssetDatabase.CreateAsset(asset, path + name +".playable");
// 自定义初始Track
var track = asset.CreateTrack<InitTrackAsset>(null);
var clip = track.CreateClip<InitClipAsset>();
AssetDatabase.SaveAssets();
return asset;
}
}
|