当前位置: 首页 > news >正文

Unity数字可视化学校_昼夜(三)

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需要初始化,如下图所示:
 

dotween utilitypanel


点击Setup DOTween按钮即可完成配置,当然如果需要自定义一些参数,可以点击Preferences选项卡来进行设置,该选项卡如下图所示:

dotween utilitypanel 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);}
}

http://www.lryc.cn/news/112970.html

相关文章:

  • 使用罗技鼠标后 弹出当前页面的脚本发生错误AppData/Local/Temp/LogiUI/Pak/js/jquery-1.3.2.min.js解决
  • Kubernetes(K8s)从入门到精通系列之十四:安装工具
  • 【Python】Python元组学习
  • HTML 元素的属性有哪些?
  • Spring之事务实现方式及原理
  • 为独立服务器增加安全性的简单步骤
  • JavaScript--Cookie
  • 【单片机】晨启科技,酷黑版,简易电压采集装置
  • 任务12、Quality指令加持,Midjourney生成电影级数码作品
  • 第4章 字符串和格式化输入/输出
  • 嵌入式开发学习(STC51-7-矩阵按键)
  • WMS仓储管理系统的价值从哪些方面体现
  • 网站推荐第二期-沉浸式网页翻译
  • 【独立后台】快递小程序便宜寄快递系统小程序 对接易达
  • 【JVM】垃圾回收 ——自问自答2
  • MySQL数据库数据类型
  • 【力扣】27. 移除元素 <快慢指针、首尾指针>
  • 【每日一题】—— C. Challenging Cliffs(Codeforces Round 726 (Div. 2))
  • 想在金融界拥有一席之地吗—社科院杜兰大学金融管理硕士助你圆梦
  • Moonbeam与Nodle网络集成,增添物联网功能
  • 关于docker 在ubuntu16.04 上的安装
  • Cmder启动加速
  • 【windows】windows上如何使用linux命令?
  • Docker Compose: 集合管理Docker的工具安装
  • Jmeter(三) - 从入门到精通 - 测试计划(Test Plan)的元件(详解教程)
  • 20天学会rust(一)和rust say hi
  • 嵌入式传感器将是未来智能交通发展的关键
  • Vue3 实现产品图片放大器
  • 机器学习笔记 - 使用 YOLOv5、O​​penCV、Python 和 C++ 检测物体
  • 使用手机相机检测电脑屏幕刷新率Hz