1、删除不需要的
UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class EnvControl : MonoBehaviour
{//UIprivate Button btnTime;private Text txtTime; //材质public List<Material> matList=new List<Material>();private List<float> matValueList=new List<float>();// Start is called before the first frame updatevoid Awake(){btnTime = transform.Find("Canvas/Panel/btnTime").GetComponent<Button>();txtTime = transform.Find("Canvas/Panel/btnTime/Text").GetComponent<Text>();txtTime.text = "白天";btnTime.onClick.AddListener(onBtnTimeClick);}// Update is called once per framevoid Update(){}void onBtnTimeClick(){txtTime.text = txtTime.text == "白天" ? "晚上" : "白天";}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;public class EnvControl : MonoBehaviour
{//UIprivate Button btnTime;private Text txtTime; //材质public List<Material> matList=new List<Material>();private List<float> matValueList=new List<float>();//Postprivate PostProcessVolume postDay;private PostProcessVolume PostNight;//特效、光照private GameObject nightFx;private GameObject nightLight;// Start is called before the first frame updatevoid Awake(){btnTime = transform.Find("Canvas/Panel/btnTime").GetComponent<Button>();txtTime = transform.Find("Canvas/Panel/btnTime/Text").GetComponent<Text>();for (int i = 0; i < matList.Count; i++){matValueList.Add(matList[i].GetFloat("_E"));}postDay = transform.Find("Light/PostDay").GetComponent<PostProcessVolume>();PostNight = transform.Find("Light/PostNight").GetComponent<PostProcessVolume>();nightFx = transform.Find("Light/FX").gameObject;nightLight = transform.Find("Light/Night").gameObject;//初始化txtTime.text = "夜晚";btnTime.onClick.AddListener(onBtnTimeClick);}// Update is called once per framevoid Update(){}void onBtnTimeClick(){txtTime.text = txtTime.text == "白天" ? "夜晚" : "白天";//白天if (txtTime.text== "白天"){nightFx.SetActive(false);nightLight.SetActive(false);for (int i = 0; i < matList.Count; i++){matList[i].SetFloat("_E", 0f);}postDay.weight = 1.0f;PostNight.weight = 0f;UniStorm.UniStormManager.Instance.SetTime(10, 0);}//夜晚if (txtTime.text == "夜晚"){nightFx.SetActive(true);nightLight.SetActive(true);for (int i = 0; i < matList.Count; i++){//matList[i].SetFloat("_E", 1.0f);matList[i].SetFloat("_E", matValueList[i]);}postDay.weight = 0.0f;PostNight.weight = 11.0f;UniStorm.UniStormManager.Instance.SetTime(22, 0);}}private void onDestroy(){for (int i = 0; i < matList.Count; i++){matList[i].SetFloat("_E", matValueList[i]);}}
}
注意:
2、DOTween
DOTween (HOTween v2) | Animation Tools | Unity Asset Store
导入
下载完成后直接导入Unity,如果是新项目第一次导入Unity,会弹出提示框提示DoTween需要初始化,如下图所示:
点击Setup DOTween按钮即可完成配置,当然如果需要自定义一些参数,可以点击Preferences选项卡来进行设置,该选项卡如下图所示:
初始化完成后,在需要使用DoTween的地方需要引入命名空间DG.Tweening; 这里是一些官方的链接:
快速开始: http://dotween.demigiant.com/getstarted.php
官方文档: http://dotween.demigiant.com/documentation.php
3、属性变化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;
using DG.Tweening;public class EnvControl : MonoBehaviour
{//UIprivate Button btnTime;private Text txtTime; //材质public List<Material> matList=new List<Material>();private List<float> matValueList=new List<float>();//Postprivate PostProcessVolume postDay;private PostProcessVolume PostNight;//特效、光照private GameObject nightFx;private GameObject nightLight;// Start is called before the first frame updatevoid Awake(){btnTime = transform.Find("Canvas/Panel/btnTime").GetComponent<Button>();txtTime = transform.Find("Canvas/Panel/btnTime/Text").GetComponent<Text>();for (int i = 0; i < matList.Count; i++){matValueList.Add(matList[i].GetFloat("_E"));}postDay = transform.Find("Light/PostDay").GetComponent<PostProcessVolume>();PostNight = transform.Find("Light/PostNight").GetComponent<PostProcessVolume>();nightFx = transform.Find("Light/FX").gameObject;nightLight = transform.Find("Light/Night").gameObject;//初始化txtTime.text = "夜晚";btnTime.onClick.AddListener(onBtnTimeClick);}// Update is called once per framevoid Update(){}void onBtnTimeClick(){txtTime.text = txtTime.text == "白天" ? "夜晚" : "白天";//白天if (txtTime.text== "白天"){nightFx.SetActive(false);nightLight.SetActive(false);for (int i = 0; i < matList.Count; i++){//matList[i].SetFloat("_E", 0f);DoPropertyAnim(matList[i],"_E",0f,1f);}float weightDay = 0f;float weightNeight = 1f;DOTween.To(() => weightDay, (x) => { weightDay = x; postDay.weight = x; }, 1f, 1f);DOTween.To(() => weightNeight, (x) => { weightNeight = x; PostNight.weight = x; }, 0f, 1f);//postDay.weight = 1.0f;//PostNight.weight = 0f;UniStorm.UniStormManager.Instance.SetTime(10, 0);}//夜晚if (txtTime.text == "夜晚"){nightFx.SetActive(true);nightLight.SetActive(true);for (int i = 0; i < matList.Count; i++){//matList[i].SetFloat("_E", 1.0f);//matList[i].SetFloat("_E", matValueList[i]);DoPropertyAnim(matList[i], "_E", matValueList[i], 1f);}float weightDay = 1.0f;float weightNeight = 0f;DOTween.To(() => weightDay, (x) => { weightDay = x; postDay.weight = x; }, 0f, 1f);DOTween.To(() => weightNeight, (x) => { weightNeight = x; PostNight.weight = x; }, 1f, 1f);//postDay.weight = 0.0f;//PostNight.weight = 1.0f;UniStorm.UniStormManager.Instance.SetTime(22, 0);}}private void onDestroy(){for (int i = 0; i < matList.Count; i++){matList[i].SetFloat("_E", matValueList[i]);}}//属性动画void DoPropertyAnim(Material mat, string property, float value, float duration){float data = mat.GetFloat(property);DOTween.To(()=>data, (x) => { data=x;mat.SetFloat(property,x);},value,duration);}
}