Skip to content

Instantly share code, notes, and snippets.

@tkymx
Created February 17, 2021 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkymx/3dae072b2932cd55491a2431d82df2ed to your computer and use it in GitHub Desktop.
Save tkymx/3dae072b2932cd55491a2431d82df2ed to your computer and use it in GitHub Desktop.
Timeline の実験
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Timeline;
using System;
[Serializable]
public class PageTransitionInfo
{
public string NextAddress;
public TimelineAsset PageTransitionTimelineAsset;
}
public class BasePage : MonoBehaviour
{
[SerializeField]
GameObject _childrenRoot;
[SerializeField]
AnimationHandler _animationHandler;
[SerializeField]
private List<PageTransitionInfo> _pageTransitionInfos;
public int CurrentLayerNumber { get; private set; }
public void SetLayer(int layerNumber)
{
CurrentLayerNumber = layerNumber;
var cameras = GetComponentsInChildren<Camera>();
foreach (var ca in cameras)
{
ca.depth = CurrentLayerNumber;
}
}
public bool ReserverTimelinePlaying = false;
private void Awake()
{
CurrentLayerNumber = 0;
if (!ReserverTimelinePlaying)
{
_animationHandler.Play(AnimationType.Appear);
}
}
public void ShowPage(string address)
{
var handler = Addressables.InstantiateAsync(address, _childrenRoot.transform, false);
handler.Completed += _ =>
{
OnComplate(address, _);
};
}
private void OnComplate(string nextAddress, UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
{
// 初回は見えていない状態からスタート
var canvasGroup = obj.Result.GetComponent<CanvasGroup>();
canvasGroup.alpha = 0;
// レイヤー情報を設定
var basePage = obj.Result.GetComponentInChildren<BasePage>();
basePage.SetLayer(CurrentLayerNumber + 1);
// ページ遷移
var pageTransitionInfo = _pageTransitionInfos.Find(_ => _.NextAddress == nextAddress);
if (pageTransitionInfo != null)
{
basePage.ReserverTimelinePlaying = true;
PageTransitionHandler.Instance.StartTransition(pageTransitionInfo.PageTransitionTimelineAsset, this, basePage);
}
}
public void HideThis()
{
_animationHandler.Play(AnimationType.Disappear);
_animationHandler.AnimationComplateListener += OnComplateAnimation;
}
private void OnComplateAnimation(AnimationType animationType)
{
// 終了していたら消す
if (animationType == AnimationType.Disappear)
{
Addressables.ReleaseInstance(this.gameObject);
}
_animationHandler.AnimationComplateListener -= OnComplateAnimation;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using System.Linq;
public class PageTransitionHandler : MonoBehaviour
{
public static PageTransitionHandler Instance
{
get
{
return FindObjectOfType<PageTransitionHandler>();
}
}
[SerializeField]
private PlayableDirector _pageTransitionPlayableDirector;
public void StartTransition(TimelineAsset pageTransitionTimeline, BasePage prevPage, BasePage nextPage)
{
_pageTransitionPlayableDirector.playableAsset = pageTransitionTimeline;
Transition(prevPage.gameObject, nextPage.gameObject);
_pageTransitionPlayableDirector.Play();
}
public void Transition(GameObject prevPage, GameObject nextPage)
{
var timelineAsset = _pageTransitionPlayableDirector.playableAsset as TimelineAsset;
timelineAsset.GetOutputTracks();
foreach (var track in timelineAsset.GetOutputTracks())
{
if(track.name == "NextPage")
{
var clip = track.GetClips().First().asset as ControlPlayableAsset;
var guid = System.Guid.NewGuid().ToString();
clip.sourceGameObject.exposedName = guid;
_pageTransitionPlayableDirector.SetReferenceValue(guid, nextPage.gameObject);
}
else if (track.name == "PrevPage")
{
var clip = track.GetClips().First().asset as ControlPlayableAsset;
var guid = System.Guid.NewGuid().ToString();
clip.sourceGameObject.exposedName = guid;
_pageTransitionPlayableDirector.SetReferenceValue(guid, prevPage.gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
public class TimelineInfo
{
public string AnimaitonKey;
public PlayableAsset PlayerableAsset;
}
public class TimelineHandler : MonoBehaviour
{
[SerializeField]
private PlayableDirector _playableDirector;
[SerializeField]
private List<TimelineInfo> _timelineInfos;
public PlayableDirector PlayableDirector => _playableDirector;
public void SetTimeline(string animationKey)
{
_playableDirector.playableAsset = _timelineInfos.Find(_ => _.AnimaitonKey == animationKey).PlayerableAsset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment