using System.Collections; using System.Collections.Generic; using UnityEngine; using BigSpace.XRCore.Event; using BigSpace.Logic; public class CameraGradient : MonoBehaviour { public GameObject cube; public MeshRenderer material; Color color; //bool isGoBlack = false; //变黑 //bool isGoTransparent = false; //透明 ////计时相关 //float waitTime = 2f; // Start is called before the first frame update void Start() { color = new Color(0, 0, 0, 0); GlobalEventMgr.Listen(GameEvent.EventChangeCameraTransparent, GameDataManage_EventChangeCameraTransparent); } // Update is called once per frame void Update() { //if (isGoBlack) //{ // //runTime += Time.deltaTime; // color.a = runTime/ waitTime; // material.materials[0].SetColor("_BaseColor", color); // if (runTime >= waitTime) // { // color.a = 1; // material.materials[0].SetColor("_BaseColor", color); // isGoBlack = false; // runTime = 0; // } //} //if (isGoTransparent) //{ // runTime += Time.deltaTime; // color.a = 1 - runTime/ waitTime; // material.materials[0].SetColor("_BaseColor", color); // if (runTime >= waitTime) // { // color.a = 0; // material.materials[0].SetColor("_BaseColor", color); // isGoTransparent = false; // runTime = 0; // cube.SetActive(false); // } //} } private IEnumerator Fade(float startAlpha, float endAlpha, float duration) { float time = 0; while (time < duration) { time += Time.deltaTime; color.a = Mathf.Lerp(startAlpha, endAlpha, time / duration); material.materials[0].SetColor("_BaseColor", color); yield return null; } color.a = endAlpha; if(endAlpha==0) cube.SetActive(false); } //private IEnumerator Fade(float startAlpha, float endAlpha, float duration) //{ // float time = 0; // while (time < duration) // { // time += Time.deltaTime; // canvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, time / duration); // yield return null; // } // canvasGroup.alpha = endAlpha; // fadeCompleteCallback?.Invoke(); // fadeCompleteCallback = null; //} private void GameDataManage_EventChangeCameraTransparent(bool isShow,float waitTime) { Debug.Log("Camera收到事件:"+isShow); if (isShow) { SetBlack(); } else { SetTransparent(); //StartCoroutine(WaitSet(waitTime)); } } //IEnumerator WaitSet(float waitTime) //{ // yield return new WaitForSeconds(waitTime); //;等待执行 // SetTransparent(); //} void SetBlack() { color.a = 0; material.materials[0].SetColor("_BaseColor", color); cube.SetActive(true); StartCoroutine(Fade(0,1,1.5f)); } void SetTransparent() { color.a = 1; material.materials[0].SetColor("_BaseColor", color); cube.SetActive(true); StartCoroutine(Fade(1, 0, 1.5f)); } }