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;
    }
相关推荐
用户788477316346 小时前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船11 小时前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261351 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船1 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无2 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
huaqianzkh3 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261353 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
17岁的勇气3 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
公子小六3 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技4 天前
第6章第4节:自定义分词器
开发语言·c#