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;
    }
相关推荐
JIes__14 小时前
Unity(二)——核心系统
unity·游戏引擎
gc_229914 小时前
C#学习调用OpenMcdf模块解析ole数据的基本用法(1)
c#·ole·openmcdf
独处东汉14 小时前
freertos开发空气检测仪之按键输入事件管理系统设计与实现
人工智能·stm32·单片机·嵌入式硬件·unity
天人合一peng17 小时前
Unity 中 Text-TextMeshPro的获取与赋值
unity·游戏引擎
MM_MS18 小时前
Halcon图像点运算、获取直方图、直方图均衡化
图像处理·人工智能·算法·目标检测·计算机视觉·c#·视觉检测
老骥伏枥~20 小时前
C# 控制台:Console.ReadLine / WriteLine
开发语言·c#
PfCoder1 天前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
天人合一peng2 天前
Unity中button 和toggle监听事件函数有无参数
前端·unity·游戏引擎
_乐无2 天前
Unity加载gly 点云 高斯泼溅渲染
unity