157 lines
5.4 KiB
C#
157 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Playables;
|
|
using UnityEngine.Timeline;
|
|
|
|
namespace Thousandto.Launcher.ExternalLibs
|
|
{
|
|
[Serializable]
|
|
public class TimelineAssetData
|
|
{
|
|
//路径
|
|
public string Path;
|
|
|
|
[NonSerialized]
|
|
private PlayableDirector _playableDirector = null;
|
|
|
|
//PlayableAsset资源
|
|
public PlayableAsset RefPlayableAsset = null;
|
|
|
|
//AnimClip资源
|
|
public AnimationClip[] RefAnimClipArray = null;
|
|
|
|
//AudioClip资源
|
|
public AudioClip[] RefAudioClipArray = null;
|
|
|
|
#region //运行时执行
|
|
//卸载
|
|
public void Unload()
|
|
{
|
|
if (RefAnimClipArray != null)
|
|
{
|
|
var cnt = RefAnimClipArray.Length;
|
|
for (int i = 0; i < cnt; i++)
|
|
{
|
|
Resources.UnloadAsset(RefAnimClipArray[i]);
|
|
}
|
|
}
|
|
|
|
if (RefPlayableAsset != null)
|
|
{
|
|
Resources.UnloadAsset(RefPlayableAsset);
|
|
}
|
|
|
|
if (RefAudioClipArray != null)
|
|
{
|
|
var cnt = RefAudioClipArray.Length;
|
|
for (int i = 0; i < cnt; i++)
|
|
{
|
|
Resources.UnloadAsset(RefAudioClipArray[i]);
|
|
}
|
|
}
|
|
|
|
if (_playableDirector)
|
|
{
|
|
UnityEngine.Object.Destroy(_playableDirector);
|
|
}
|
|
}
|
|
|
|
//设置PlayableDirector
|
|
public void ToPlayableDirector(Transform trans)
|
|
{
|
|
_playableDirector = trans.GetComponent<PlayableDirector>();
|
|
}
|
|
#endregion
|
|
|
|
#region//编辑器时执行--打包之前数据准备用
|
|
//从Animation中获取信息
|
|
public bool FromPlayableDirector(PlayableDirector playableDirector)
|
|
{
|
|
if (playableDirector != null)
|
|
{
|
|
playableDirector.playOnAwake = false;
|
|
HashSet<AnimationClip> animationClips = new HashSet<AnimationClip>();
|
|
HashSet<AudioClip> audioClips = new HashSet<AudioClip>();
|
|
|
|
//收集Timeline资源
|
|
RefPlayableAsset = playableDirector.playableAsset;
|
|
if (RefPlayableAsset != null)
|
|
{
|
|
if (RefPlayableAsset.outputs != null)
|
|
{
|
|
foreach (var item in RefPlayableAsset.outputs)
|
|
{
|
|
var _animationTrack = item.sourceObject as AnimationTrack;
|
|
if (_animationTrack)
|
|
{
|
|
var clips = _animationTrack.GetClips();
|
|
if (clips != null)
|
|
{
|
|
foreach (var clipsItem in clips)
|
|
{
|
|
var _animationClip = clipsItem.animationClip;
|
|
if (_animationClip && !animationClips.Contains(_animationClip))
|
|
{
|
|
animationClips.Add(_animationClip);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var _audioTrack = item.sourceObject as AudioTrack;
|
|
if (_audioTrack)
|
|
{
|
|
var clips = _audioTrack.GetClips();
|
|
if (clips != null)
|
|
{
|
|
foreach (var clipsItem in clips)
|
|
{
|
|
var _audioClip = clipsItem.asset as AudioPlayableAsset;
|
|
if (_audioClip)
|
|
{
|
|
if(_audioClip.clip && !audioClips.Contains(_audioClip.clip))
|
|
{
|
|
audioClips.Add(_audioClip.clip);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
RefAnimClipArray = null;
|
|
RefAudioClipArray = null;
|
|
|
|
var animClipCount = animationClips.Count;
|
|
if (animClipCount > 0)
|
|
{
|
|
RefAnimClipArray = new AnimationClip[animClipCount];
|
|
int index = 0;
|
|
foreach (var item in animationClips)
|
|
{
|
|
RefAnimClipArray[index++] = item;
|
|
}
|
|
}
|
|
|
|
var audioClipCount = audioClips.Count;
|
|
if (audioClipCount > 0)
|
|
{
|
|
RefAudioClipArray = new AudioClip[audioClipCount];
|
|
int index = 0;
|
|
foreach (var item in audioClips)
|
|
{
|
|
RefAudioClipArray[index++] = item;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|