您的位置:首页 > 科技 > IT业 > Unity 公用函数整理【二】

Unity 公用函数整理【二】

2024/10/5 14:05:08 来源:https://blog.csdn.net/U3DCoder/article/details/139863723  浏览:    关键词:Unity 公用函数整理【二】

1、在规定时间时间内将一个值变化到另一个值,使用Mathf.Lerp实现

private float timer;[Tooltip("当前温度")]private float curTemp;[Tooltip("开始温度")]private float startTemp = 20;private float maxTemp = 100;/// <summary>/// 升温时设置温度计度数/// </summary>/// <param name="duration"></param>private void HeatingUp(float duration = 10f){if (timer <= duration){timer += Time.deltaTime;float t = timer / duration;curTemp = Mathf.Lerp(startTemp, maxTemp, t);Debug.Log(" 时间=" + timer.ToString("0.000") + "  <color=red>升温=</color>" + curTemp.ToString("0.000"));//thermometerFill.fillAmount = curTemp / 100f;//thermometerText.text = "温度 " + curTemp.ToString("0") + " ℃";}}/// <summary>/// 降温时设置温度计度数/// </summary>/// <param name="duration"></param>private void Cooling(float duration = 10f){if (timer >= 0){timer -= Time.deltaTime;float t = timer / duration;curTemp = Mathf.Lerp(startTemp, maxTemp, t);Debug.Log("时间=" + timer.ToString("0.00") + "  <color=yellow>降温=</color>" + curTemp.ToString("0.00"));//thermometerFill.fillAmount = curTemp / 100f;//thermometerText.text = "温度 " + curTemp.ToString("0") + " ℃";}}

2、控制粒子特效数量变化

   [Tooltip("水蒸气")] private ParticleSystem waterVapor;private float waterVaporValue = 0f;//水蒸气初始值private float targetValue = 1000f;//目标值/// <summary>/// 水泡粒子特效变化/// </summary>/// <param name="isAdd"></param>/// <param name="duration"></param>public void WaterVaporValueChanges(bool isAdd = true, float duration = 10f){if (isAdd){waterVaporValue += Time.deltaTime * (targetValue / duration);if (waterVaporValue >= targetValue){waterVaporValue = targetValue;}}else{waterVaporValue -= Time.deltaTime * (targetValue / duration);if (waterVaporValue <= 0){waterVaporValue = 0;}}SetWaterVaporParticles(waterVaporValue);}/// <summary>/// 水蒸气特效/// </summary>/// <param name="value"></param>public void SetWaterVaporParticles(float value=0){var mainWaterVapor = waterVapor.main;mainWaterVapor.maxParticles = (int)value;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com