Files
beyond/Assets/Scripts/UI/TestScroll.cs
2024-11-20 15:21:28 +01:00

232 lines
7.4 KiB
C#

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if USE_NEW_INPUT
using UnityEngine.InputSystem.UI;
#endif
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
namespace Beyond
{
public class TestScroll : MonoBehaviour
{
public UVInputModule m_uvInputModule;
#if USE_NEW_INPUT
public InputSystemUIInputModule m_inputSystemUIInputModule;
#endif
// Start is called before the first frame update
public Animator m_scrollAnimator;
public Canvas m_canvas;
public Camera m_GUICamera;
public Camera m_ScrollCamera;
public bool m_burnIn = false;
public bool m_isOpen = false;
public float m_burnInDelay = 1f;
public float m_moveInTime = 1f;
public float m_burnInTime = 1f;
public float m_burnThreshold;
[Tooltip("Motion curve for first move in")]
public AnimationCurve m_MotionCurve;
public AnimationCurve m_BurnInCurve;
public Vector3 m_startPositionShift = new Vector3(-3f, 0f, 0f);
private Vector3 m_startPosition;
private Vector3 m_endPosition;
private Transform m_scrollTransform;
private Material m_scrollMat;
public RectTransform[] m_pages;
public float lettersPerSec = 50f;
private int m_currentPage = -1;
private Volume m_volume;
public VolumeProfile m_volumeProfile;
private VolumeProfile m_defaultProfile;
public VideoPlayer m_videoPlayer;
public VideoClip m_clipToPlay;
void ActivateObjects(bool activate)
{
m_canvas.gameObject.SetActive(activate);
m_GUICamera.gameObject.SetActive(activate);
m_ScrollCamera.gameObject.SetActive(activate);
if (m_volume && m_volumeProfile != null)
m_volume.profile = activate ? m_volumeProfile : m_defaultProfile;
}
void BlockUI(bool block)
{
HideUI.InvokeSetActiveUI(!block);
if (m_uvInputModule != null)
m_uvInputModule.enabled = block;
#if USE_NEW_INPUT
if (m_inputSystemUIInputModule)
m_inputSystemUIInputModule.enabled = !block;
#endif
}
IEnumerator OpenAndBurnIn()
{
BlockUI(true);
ActivateObjects(true);
SetBurnInTh(0f);
float time = 0f;
while (time < m_moveInTime)
{
time += Time.deltaTime;
float val = m_MotionCurve.Evaluate(time / m_moveInTime);
m_scrollTransform.position = m_startPosition - m_startPositionShift * val;
yield return null;
}
m_scrollAnimator.SetBool("Open", true);
yield return new WaitForSeconds(m_burnInDelay);
time = 0f;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_isOpen = true;
if (m_videoPlayer)
{
m_videoPlayer.Play();
}
yield return true;
}
IEnumerator CloseAndBurnOut()
{
float time = 0f;
m_isOpen = false;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(1.0f - time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_scrollAnimator.SetBool("Open", false);
time = 0f;
yield return new WaitForSeconds(m_burnInDelay);
while (time < m_moveInTime)
{
time += Time.deltaTime;
float val = m_MotionCurve.Evaluate(1.0f - time / m_moveInTime);
m_scrollTransform.position = m_startPosition - m_startPositionShift * val;
yield return null;
}
BlockUI(false);
ActivateObjects(false);
yield return true;
}
const float timePerPage = 1000f;
IEnumerator DisplayPages()
{
float time = 0f;
for (int i=0; i < m_pages.Length-1; i++)
{
yield return new WaitForSeconds(timePerPage);
time = 0f;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(1.0f - time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_pages[i].gameObject.SetActive(false);
m_pages[i+1].gameObject.SetActive(true);
time = 0f;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
}
yield return new WaitForSeconds(timePerPage);
yield return CloseAndBurnOut();
yield return null;
}
void SetBurnInTh(float burnTh)
{
m_scrollMat.SetFloat("_Threshold", burnTh);
}
void Awake()
{
if (!m_scrollAnimator)
{
m_scrollAnimator = transform.GetComponentInChildren<Animator>();
}
m_scrollAnimator.SetBool("Open", false);
m_scrollTransform = m_scrollAnimator.transform.parent;
m_scrollMat = m_scrollAnimator.transform.GetComponentInChildren<Renderer>().material;
if (m_volume == null)
m_volume = GameObject.FindObjectOfType<Volume>();
if (m_volume != null)
m_defaultProfile = m_volume.profile;
if (m_videoPlayer == null)
m_videoPlayer = gameObject.FindComponentDownHierarchy<VideoPlayer>();
if (m_videoPlayer != null && m_clipToPlay != null)
{
m_videoPlayer.clip = m_clipToPlay;
}
}
[Button]
public void OpenCloseScroll(bool open)
{
if (open)
StartCoroutine(OpenAndBurnIn());
else
StartCoroutine(CloseAndBurnOut());
}
void Start()
{
m_endPosition = m_scrollTransform.position;
m_startPosition = m_endPosition + m_startPositionShift;
m_scrollTransform.position = m_startPosition;
var camera = Camera.main;
if (camera != null)
{
var cameraData = camera.GetUniversalAdditionalCameraData();
if (cameraData.cameraStack.Count == 0)
cameraData.cameraStack.Add(m_ScrollCamera);
}
//OpenCloseScroll(true);
//StartCoroutine(DisplayPages());
//ActivateObjects(false);
}
// Update is called once per frame
void Update()
{
if (m_videoPlayer)
{
if (m_isOpen && m_videoPlayer.frame >= (long)m_videoPlayer.frameCount-1)
{
OpenCloseScroll(false);
}
}
}
}
}