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

206 lines
6.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
namespace Beyond
{
public class PrefaceScroll : MonoBehaviour
{
// 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 TMPro.TextMeshProUGUI[] m_pages;
public float lettersPerSec = 50f;
private int m_currentPage = -1;
public string m_sceneName = "Land_01";
void ActivateObjects(bool activate)
{
m_canvas.gameObject.SetActive(activate);
m_GUICamera.gameObject.SetActive(activate);
m_ScrollCamera.gameObject.SetActive(activate);
}
IEnumerator OpenAndBurnIn()
{
//BlockUI(true);
ActivateObjects(true);
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;
}
yield return true;
}
IEnumerator CloseAndBurnOut()
{
float 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_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;
}
IEnumerator DisplayPages()
{
float time = 0f;
for (int i=0; i < m_pages.Length-1; i++)
{
yield return new WaitForSeconds(m_pages[i].text.Length/ lettersPerSec);
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(m_pages[m_pages.Length-1].text.Length / lettersPerSec);
yield return CloseAndBurnOut();
SceneManager.LoadScene(m_sceneName, LoadSceneMode.Single);
yield return null;
/*
float 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_panels[(int)m_activePanel].SetActive(false);
m_panels[(int)panel].SetActive(true);
time = 0f;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_activePanel = (PanelEnum)panel;
*/
yield return null;
}
/*
public void SwitchPanel(int panel)
{
if (panel == (int)m_activePanel)
return;
StartCoroutine(SwitchPanelCoroutine((PanelEnum)panel));
}
*/
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;
}
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;
OpenCloseScroll(true);
StartCoroutine(DisplayPages());
//ActivateObjects(false);
}
// Update is called once per frame
void Update()
{
}
}
}