Unity 公用函数整理【二】

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

cs 复制代码
    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、控制粒子特效数量变化

cs 复制代码
   [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;
    }
相关推荐
xcLeigh13 小时前
Unity基础:Game视图详解——游戏预览、分辨率模拟与性能显示
游戏·unity·游戏引擎·音频·视频·game·play模式
csdn_aspnet13 小时前
C# 提取、截取或匹配字符串内包含指定字符的一些方法分享
c#·字符串·正则·分割·提取·匹配
枳实-叶13 小时前
【Linux驱动开发】第23天:spi_driver 的 probe / remove 函数实现规范
linux·驱动开发·c#
长明14 小时前
C#项目组织与概念梳理
后端·c#
迷路爸爸18014 小时前
Python collections 入门+实战
windows·python·c#·collections·dict
csdn_aspnet14 小时前
C# 截取或匹配字符串内包含指定字符的一些方法
c#·字符串·分割·string·匹配·截取
Rotion_深15 小时前
C# 值类型与引用类型 详解
开发语言·jvm·c#
xcLeigh1 天前
Unity基础:Scene视图操作完全指南——视角控制、物体选择与场景导航
unity·游戏引擎·scene·试图·场景导航
影寂ldy1 天前
C# try-catch 异常处理全套笔记
服务器·数据库·c#
mxwin1 天前
Unity Shader exp 函数的算法与渲染应用
算法·unity·游戏引擎·shader