436 lines
15 KiB
C#
436 lines
15 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
//using UnityEngine.InputSystem.UI;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class MenuScroll : MonoBehaviour
|
|
{
|
|
private const float dimAlpha = 0.5f;
|
|
private const string bgColorName = "_BaseColor";
|
|
|
|
/// <summary>
|
|
/// Input that is used to pass touch/click events from/to 3D scroll
|
|
/// </summary>
|
|
/// [
|
|
///
|
|
// public UVInputModule m_uvInputModule;
|
|
// public StandaloneInputModule playerInput;
|
|
|
|
public EventSystem eventSystem;
|
|
|
|
/// <summary>
|
|
/// Animates scroll opening and closing
|
|
/// </summary>
|
|
public Animator m_scrollAnimator;
|
|
|
|
/// <summary>
|
|
/// Canvas used to render 2D scroll elements
|
|
/// </summary>
|
|
public Canvas m_canvas;
|
|
|
|
// public Camera m_CanvasCamera;
|
|
public Camera m_CanvasCamera;
|
|
|
|
public Camera m_ScrollCamera;
|
|
private Camera m_mainCamera;
|
|
|
|
public bool m_burnIn = false;
|
|
public bool m_isOpen = false;
|
|
public float m_burnInDelay = 0.23f;
|
|
public float m_moveInTime = 0.85f;
|
|
public float m_bgFadeDuration = 0.35f;
|
|
public const float m_defaultBurnTime = 0.25f;
|
|
public float openCloseBurnTime = 0.8f;
|
|
private const float constantDeltaTime = 0.01666667f;
|
|
public float m_burnThreshold;
|
|
public bool m_dimElements = false;
|
|
|
|
[Tooltip("Motion curve for first move in")]
|
|
public AnimationCurve m_MotionCurve;
|
|
|
|
public AnimationCurve m_BurnInOutCurve, m_BurnChangeCurve;
|
|
private AnimationCurve m_CurrentBurnCurve;
|
|
public Vector3 m_startPositionShift = new Vector3(-3f, 0f, 0f);
|
|
private Vector3 m_startPosition;
|
|
private Vector3 m_endPosition;
|
|
private Transform m_scrollTransform;
|
|
|
|
//this material is used to animate page effect
|
|
public Material m_scrollMat;
|
|
|
|
[SerializeField] private Texture openCloseTexture, changeTexture;
|
|
|
|
private Material m_bgScrollMat;
|
|
[SerializeField] private Renderer m_bgRenderer;
|
|
|
|
private Volume m_volume;
|
|
public VolumeProfile m_volumeProfile;
|
|
private VolumeProfile m_defaultProfile;
|
|
|
|
public event Action OnOpened, OnClosed, OnNewCanvaBurnStart, OnNewCanvaBurnEnd, OnNewCanvaBurnMiddle;
|
|
|
|
public SubmenuElementsContainer m_currentlyOpenedElementsContainer;
|
|
public GameObject m_currentCanva;
|
|
|
|
[SerializeField] private Texture m_RTTexture;
|
|
|
|
private void ActivateObjects(bool activate)
|
|
{
|
|
m_canvas.gameObject.SetActive(activate);
|
|
m_CanvasCamera.gameObject.SetActive(activate);
|
|
m_ScrollCamera.gameObject.SetActive(activate);
|
|
|
|
if (m_volume && m_volumeProfile != null)
|
|
m_volume.profile = activate ? m_volumeProfile : m_defaultProfile;
|
|
}
|
|
|
|
private void BlockUI(bool block)
|
|
{
|
|
Debug.Log("MenuScroll: BlockUI " + block);
|
|
HideUI.InvokeSetActiveUI(!block);
|
|
/*
|
|
if (m_uvInputModule != null)
|
|
{
|
|
Debug.Log("MenuScroll: Enable UV input module ");
|
|
m_uvInputModule.enabled = block;
|
|
}
|
|
if (playerInput)
|
|
{
|
|
playerInput.enabled = !block;
|
|
Debug.Log("MenuScroll: block player input ");
|
|
}
|
|
*/
|
|
}
|
|
|
|
protected virtual void OnScrollOpened()
|
|
{
|
|
}
|
|
|
|
protected virtual void OnScrollClosed()
|
|
{
|
|
}
|
|
|
|
public void SetupCamera()
|
|
{
|
|
m_mainCamera = Camera.main;
|
|
/*
|
|
var mainCameraData = m_mainCamera.GetUniversalAdditionalCameraData();
|
|
mainCameraData.cameraStack.Add(m_ScrollCamera);
|
|
|
|
var scrollCameraData = m_ScrollCamera.GetUniversalAdditionalCameraData();
|
|
scrollCameraData.renderType = CameraRenderType.Overlay;
|
|
*/
|
|
}
|
|
|
|
public void DisableMainCamera(bool disable)
|
|
{
|
|
m_mainCamera.enabled = !disable;
|
|
/*
|
|
var scrollCameraData = m_ScrollCamera.GetUniversalAdditionalCameraData();
|
|
if (disable)
|
|
{
|
|
scrollCameraData.renderType = CameraRenderType.Base;
|
|
m_mainCamera.enabled = false;
|
|
}
|
|
else
|
|
{
|
|
scrollCameraData.renderType = CameraRenderType.Overlay;
|
|
//var mainCameraData = Camera.main.GetUniversalAdditionalCameraData();
|
|
//mainCameraData.cameraStack.Add(m_ScrollCamera);
|
|
m_mainCamera.enabled = true;
|
|
}
|
|
*/
|
|
}
|
|
|
|
public void OpenScroll()
|
|
{
|
|
StartCoroutine(OpenScrollCoroutine());
|
|
}
|
|
|
|
[Button]
|
|
public void OpenCloseScroll(bool open)
|
|
{
|
|
if (open)
|
|
StartCoroutine(OpenAndBurnIn());
|
|
else
|
|
StartCoroutine(CloseAndBurnOut());
|
|
}
|
|
|
|
private IEnumerator OpenScrollCoroutine()
|
|
{
|
|
yield return StartCoroutine(OpenAndBurnIn());
|
|
}
|
|
|
|
private IEnumerator OpenScrollCoroutine(SubmenuElementsContainer canvaToEnable)
|
|
{
|
|
m_currentlyOpenedElementsContainer = canvaToEnable;
|
|
List<CanvasGroup> interactables = m_currentlyOpenedElementsContainer.interactableCanvas;
|
|
if (m_dimElements)
|
|
{
|
|
interactables.ForEach(group => group.alpha = dimAlpha);
|
|
}
|
|
yield return StartCoroutine(OpenScrollCoroutine());
|
|
|
|
if (m_dimElements)
|
|
{
|
|
interactables.ForEach(group => group.alpha = 1f);
|
|
}
|
|
}
|
|
|
|
private IEnumerator OpenAndBurnIn()
|
|
{
|
|
BlockUI(true);
|
|
ActivateObjects(true);
|
|
SetBurnInTh(0f);
|
|
OnOpened?.Invoke();
|
|
eventSystem.enabled = false;
|
|
float time = 0f;
|
|
float val = 0f;
|
|
while (time < m_moveInTime)
|
|
{
|
|
time += constantDeltaTime;
|
|
val = time / m_moveInTime;
|
|
val = m_MotionCurve.Evaluate(val);
|
|
float bgVal = time / m_bgFadeDuration;
|
|
m_bgScrollMat.SetColor(bgColorName, new Color(0, 0, 0, bgVal));
|
|
m_scrollTransform.localPosition = m_startPosition - m_startPositionShift * val;
|
|
yield return new WaitForSecondsRealtime(constantDeltaTime);
|
|
}
|
|
|
|
//Camera.main.enabled = false;
|
|
m_scrollAnimator.SetBool("Open", true);
|
|
yield return new WaitForSecondsRealtime(m_burnInDelay);
|
|
m_scrollMat.SetTexture("Texture2D_37fb532d63a84494b37265ae923f74c5", openCloseTexture);
|
|
//cant change scale, because the burn effect will go outside paper
|
|
SetNoiseDefaultScale();
|
|
m_CurrentBurnCurve = m_BurnInOutCurve;
|
|
yield return StartCoroutine(BurnInCoroutine(0, openCloseBurnTime));
|
|
DisableMainCamera(true);
|
|
|
|
//Wait until
|
|
|
|
m_isOpen = true;
|
|
|
|
eventSystem.enabled = true;
|
|
OnScrollOpened();
|
|
|
|
yield return true;
|
|
}
|
|
|
|
private IEnumerator BurnInCoroutine(float startingTime = 0f, float burnTime = m_defaultBurnTime)
|
|
{
|
|
Player.Instance.PlayChangePageSound();
|
|
float time = startingTime;
|
|
//
|
|
//Start Coroutine play burn in effect
|
|
while (time < burnTime)
|
|
{
|
|
time += constantDeltaTime;
|
|
float val = m_CurrentBurnCurve.Evaluate(time / burnTime);
|
|
SetBurnInTh(val);
|
|
yield return new WaitForSecondsRealtime(constantDeltaTime);
|
|
}
|
|
}
|
|
|
|
public void CloseScroll()
|
|
{
|
|
StartCoroutine(CloseScrollCoroutine());
|
|
}
|
|
|
|
private IEnumerator CloseScrollCoroutine()
|
|
{
|
|
yield return StartCoroutine(CloseAndBurnOut());
|
|
}
|
|
|
|
private IEnumerator CloseScrollCoroutine(SubmenuElementsContainer canvaToEnable)
|
|
{
|
|
m_currentlyOpenedElementsContainer = canvaToEnable;
|
|
List<CanvasGroup> interactables = m_currentlyOpenedElementsContainer.interactableCanvas;
|
|
if (m_dimElements)
|
|
{
|
|
interactables.ForEach(group => group.alpha = dimAlpha);
|
|
}
|
|
yield return StartCoroutine(CloseScrollCoroutine());
|
|
|
|
if (m_dimElements)
|
|
{
|
|
interactables.ForEach(group => group.alpha = 1f);
|
|
}
|
|
}
|
|
|
|
private IEnumerator CloseAndBurnOut()
|
|
{
|
|
float time = 0f;
|
|
m_isOpen = false;
|
|
eventSystem.enabled = false;
|
|
if (m_dimElements)
|
|
{
|
|
List<CanvasGroup> interactables = m_currentlyOpenedElementsContainer.interactableCanvas;
|
|
interactables.ForEach(group => group.alpha = 0.5f);
|
|
}
|
|
|
|
SetNoiseDefaultScale();
|
|
m_CurrentBurnCurve = m_BurnInOutCurve;
|
|
m_scrollAnimator.SetBool("Open", false);
|
|
m_scrollMat.SetTexture("Texture2D_37fb532d63a84494b37265ae923f74c5", openCloseTexture);
|
|
yield return StartCoroutine(BurnOutCoroutine(0, openCloseBurnTime));
|
|
DisableMainCamera(false);
|
|
time = 0f;
|
|
float fadeStartTime = m_moveInTime - m_bgFadeDuration;
|
|
float val = 0;
|
|
while (time < m_moveInTime)
|
|
{
|
|
time += constantDeltaTime;
|
|
val = m_MotionCurve.Evaluate(1.0f - time / m_moveInTime);
|
|
float bgVal = 1f;
|
|
if (time > (fadeStartTime))
|
|
{
|
|
bgVal = (1f - (time - fadeStartTime) / m_bgFadeDuration);
|
|
}
|
|
|
|
m_bgScrollMat.SetColor(bgColorName, new Color(0, 0, 0, bgVal));
|
|
m_scrollTransform.localPosition = m_startPosition - m_startPositionShift * val;
|
|
yield return new WaitForSecondsRealtime(constantDeltaTime);
|
|
}
|
|
|
|
BlockUI(false);
|
|
ActivateObjects(false);
|
|
Resources.UnloadUnusedAssets();
|
|
OnScrollClosed();
|
|
OnClosed?.Invoke();
|
|
eventSystem.enabled = true;
|
|
|
|
yield return true;
|
|
}
|
|
|
|
private IEnumerator BurnOutCoroutine(float shift = 0f, float burnTime = m_defaultBurnTime)
|
|
{
|
|
Player.Instance.PlayChangePageSound();
|
|
|
|
float time = 0f;
|
|
while (time < burnTime)
|
|
{
|
|
time += constantDeltaTime;
|
|
float val = m_CurrentBurnCurve.Evaluate(1.0f - time / burnTime);
|
|
SetBurnInTh(val);
|
|
yield return new WaitForSecondsRealtime(constantDeltaTime);
|
|
}
|
|
}
|
|
|
|
private void SetBurnInTh(float burnTh)
|
|
{
|
|
m_scrollMat.SetFloat("_Threshold", burnTh);
|
|
}
|
|
|
|
private void SetNoiseRandomScale()
|
|
{
|
|
SetNoiseScale(Random.Range(0.5f, 2.0f));
|
|
}
|
|
|
|
private void SetNoiseDefaultScale()
|
|
{
|
|
SetNoiseScale(1f);
|
|
}
|
|
|
|
private void SetNoiseScale(float scale)
|
|
{
|
|
m_scrollMat.SetFloat("_NoiseScale", scale);
|
|
}
|
|
|
|
public IEnumerator ChangeVisibleCanvaCoroutine(SubmenuElementsContainer submenuToDisable, SubmenuElementsContainer submenuToEnable)
|
|
{
|
|
m_currentlyOpenedElementsContainer = submenuToEnable;
|
|
List<CanvasGroup> interactablesToEnable = submenuToEnable.interactableCanvas;
|
|
List<CanvasGroup> interactablesToDisable = submenuToDisable.interactableCanvas;
|
|
if (m_dimElements)
|
|
{
|
|
interactablesToEnable.ForEach(group => group.alpha = 0.5f);
|
|
interactablesToDisable.ForEach(group => group.alpha = 0.5f);
|
|
}
|
|
yield return StartCoroutine(ChangeVisibleCanvaCoroutine(submenuToDisable.parentObject, submenuToEnable.parentObject));
|
|
if (m_dimElements)
|
|
{
|
|
interactablesToEnable.ForEach(group => group.alpha = 1f);
|
|
}
|
|
}
|
|
|
|
public void ChangeVisibleCanva(GameObject submenuToDisable, GameObject submenuToEnable)
|
|
{
|
|
StartCoroutine(ChangeVisibleCanvaCoroutine(submenuToDisable, submenuToEnable));
|
|
}
|
|
|
|
public IEnumerator ChangeVisibleCanvaCoroutine(GameObject submenuToDisable, GameObject submenuToEnable)
|
|
{
|
|
SetNoiseRandomScale();
|
|
m_CurrentBurnCurve = m_BurnChangeCurve;
|
|
// m_uvInputModule.enabled = false;
|
|
eventSystem.enabled = false;
|
|
OnNewCanvaBurnStart?.Invoke();
|
|
m_scrollMat.SetTexture("Texture2D_37fb532d63a84494b37265ae923f74c5", changeTexture);
|
|
|
|
yield return StartCoroutine(BurnOutCoroutine(0.1f));
|
|
|
|
submenuToDisable.SetActive(false);
|
|
OnNewCanvaBurnMiddle?.Invoke();
|
|
submenuToEnable.SetActive(true);
|
|
|
|
yield return StartCoroutine(BurnInCoroutine(0.1f));
|
|
OnNewCanvaBurnEnd?.Invoke();
|
|
// m_uvInputModule.enabled = true;
|
|
eventSystem.enabled = true;
|
|
// canvaToEnable.GetComponent<CanvasGroup>().alpha = 1f;
|
|
|
|
m_currentCanva = submenuToEnable;
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
m_RTTexture.width = Screen.width;
|
|
m_RTTexture.height = Screen.height;
|
|
if (!m_scrollAnimator)
|
|
{
|
|
m_scrollAnimator = transform.GetComponentInChildren<Animator>();
|
|
}
|
|
m_scrollAnimator.SetBool("Open", false);
|
|
m_scrollTransform = m_scrollAnimator.transform.parent;
|
|
if (m_scrollMat == null)
|
|
m_scrollMat = m_scrollAnimator.transform.GetComponentInChildren<Renderer>().material;
|
|
if (m_bgScrollMat == null)
|
|
m_bgScrollMat = m_bgRenderer.material;
|
|
if (m_volume == null)
|
|
m_volume = GameObject.FindObjectOfType<Volume>();
|
|
if (m_volume != null)
|
|
m_defaultProfile = m_volume.profile;
|
|
}
|
|
|
|
protected virtual void Start()
|
|
{
|
|
m_endPosition = m_scrollTransform.localPosition;
|
|
m_startPosition = m_endPosition + m_startPositionShift;
|
|
m_scrollTransform.localPosition = m_startPosition;
|
|
/*
|
|
var camera = Camera.main;
|
|
if (camera != null)
|
|
{
|
|
var cameraData = camera.GetUniversalAdditionalCameraData();
|
|
if (cameraData.cameraStack.Count == 0)
|
|
cameraData.cameraStack.Add(m_ScrollCamera);
|
|
}
|
|
*/
|
|
SetupCamera();
|
|
m_ScrollCamera.gameObject.SetActive(false);
|
|
m_CanvasCamera.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |