Fork me on GitHub

批量创建Timeline

创建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;


	
    }


	
 


	
}
posted @   ZTianming  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
欢迎阅读『批量创建Timeline』

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示