模拟灭火,内有粒子效果代码与material颜色改变代码。

30 篇文章 0 订阅
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

/// <summary>
/// 火焰控制模块
/// </summary>
public class FireCtrl : MonoBehaviour
{
    public ParticleSystem[] psChildren;//子对象的粒子系统
    public Collider touchCollider;//该火焰的碰撞体
    private Material fDJ,fDJ_Bottom;//设备材质
    private float oH, oS, oV,oBH, oBS, oBV;//材质初始的HSV值

    private void Awake()
    {
        fDJ = Resources.Load<Material>("FDJ");
        fDJ_Bottom = Resources.Load<Material>("FDJ_Bottom");

        Color.RGBToHSV(fDJ.GetColor("_TintColor"),out oH,out oS, out oV);
        Color.RGBToHSV(fDJ_Bottom.GetColor("_TintColor"), out oBH,out oBS,out oBV);
    }
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }

    /// <summary>
    ///  灭火
    /// </summary>
    public void PutoutFire()
    {
        ParticleSystem.EmissionModule emissionModule = transform.GetComponent<ParticleSystem>().emission;
        emissionModule.rateOverTime = 0;
        foreach (var item in psChildren)
        {
            ParticleSystem.EmissionModule emissionModuleChild = item.GetComponent<ParticleSystem>().emission;
            emissionModuleChild.rateOverTime = 0;
        }
        touchCollider.GetComponent<Collider>().enabled = false;

        DarkenDevice();
    }

    /// <summary>
    /// 使设备变暗
    /// </summary>
    private void DarkenDevice()
    {
        switch (transform.parent.name)
        {
            case "01":
                GameData.fire01Count += 1;
                break;
            case "02":
                GameData.fire02Count += 1;
                break;
            case "03":
                GameData.fire03Count += 1;
                break;
            case "04":
                GameData.fire04Count += 1;
                break;
            case "05":
                GameData.fire05Count += 1;
                break;
            default:
                break;
        }
        if (GameData.fire01Count == transform.parent.childCount || GameData.fire02Count == transform.parent.childCount || GameData.fire03Count == transform.parent.childCount || GameData.fire04Count == transform.parent.childCount)
            SetMaterialColor(fDJ,-0.04f,oH,oS);
        if (GameData.fire05Count == transform.parent.childCount)
            SetMaterialColor(fDJ_Bottom, -0.4f,oBH,oBS);
    }
        
    /// <summary>
    /// 设置材质属性
    /// </summary>
    /// <param name="mMaterial"></param>
    /// <param name="deltaHValue"></param>
    /// <param name="mOH"></param>
    /// <param name="mOS"></param>
    private void SetMaterialColor(Material mMaterial,float deltaHValue,float mOH,float mOS)
    {
        float tH, tS, tV;
        Color.RGBToHSV(mMaterial.GetColor("_TintColor"), out tH, out tS, out tV);
        float newV = tV + deltaHValue;  
        Color newColor = Color.HSVToRGB(mOH, mOS, newV);
        newColor.a = 0.17f;
        mMaterial.SetColor("_TintColor",newColor);
    }

    /// <summary>
    /// 重置材质球属性
    /// </summary>
    private void ResetMaterialColor()
    {
        Color newColor = Color.HSVToRGB(oH, oS, oV);
        newColor.a = 0.17f;
        fDJ.SetColor("_TintColor", newColor);

        Color newColor2 = Color.HSVToRGB(oBH, oBS, oBV);
        newColor2.a = 0.17f;
        fDJ_Bottom.SetColor("_TintColor", newColor2);
    }

    private void OnDisable()
    {
        ResetMaterialColor();    
    }

    private void OnDestroy()
    {
        
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值