Unity-----特效系统

文章介绍了Unity中的粒子系统如何模拟各种环境特效,包括粒子力场和拖尾效果。还详细讲解了如何通过脚本控制粒子系统的播放、停止、暂停、碰撞检测和触发事件,以及如何使用TrailRenderer实现拖尾效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

特效系统
粒子系统:可以用来模拟刀光,火焰,爆炸,水滴,风雨等各种环境特效
拖尾:通常用来制作一些人物或者武器移动的拖尾特效。
粒子力场:在一个区域范围内模拟一个力场钢的效果,改变粒子的表现。


粒子系统:作用:在Unity中,粒子系统可以用来模拟刀光,火焰,爆炸,水滴,风雨等各种环境特效,
通常由负责制作特效的同事制作好,提供给我们程序员,然后导入到Unity使用。
创建粒子:在层级面板中右键---效果---粒子系统完成创建。


脚本和粒子组件交互
克隆:可以先做成预设,然后从Resources文件夹加载出来,跟实例其他物体一样进行克隆即可。
播放:ParticleSystem.Play();
停止:ParticleSystem.Stop();
暂停:ParticleSystem.Pause();
销毁:跟其他物体一样使用Destory();方法销毁即可。
设置属性:首先获得要设置的模块,然后设置模块里面的属性
碰撞回调:OnParticleCollision(GameObject go);
 

对粒子控制脚本进行编辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleTest : MonoBehaviour
{
    GameObject particleGo;
    ParticleSystem particle;

    // Start is called before the first frame update
    void Start()
    {
        particleGo = GameObject.Instantiate(Resources.Load<GameObject>("22_RFX_Fire_Campfire1"));
        particleGo.transform.position = transform.position;
        particle = particleGo.GetComponent<ParticleSystem>();

        ParticleSystem.MainModule mainModule = particle.main;
        mainModule.loop = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            particle.Play();
        }
        if (Input.GetKeyDown("b"))
        {
            particle.Stop();
        }
        if (Input.GetKeyDown("c"))
        {
            particle.Pause();
        }
        if (Input.GetKeyDown("d"))
        {
            Destroy(particleGo);
        }
    }
}

粒子碰撞回调

脚本控制程序

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleTest : MonoBehaviour
{
    //GameObject particleGo;
    //ParticleSystem particle;

    // Start is called before the first frame update
    void Start()
    {
        //particleGo = GameObject.Instantiate(Resources.Load<GameObject>("22_RFX_Fire_Campfire1"));
        //particleGo.transform.position = transform.position;
        //particle = particleGo.GetComponent<ParticleSystem>();

        //ParticleSystem.MainModule mainModule = particle.main;
        //mainModule.loop = true;
    }

    private void OnParticleCollision(GameObject go)
    {
        Debug.Log("粒子发生了碰撞,碰撞到物体:" + go.name);
    }

    // Update is called once per frame
    void Update()
    {
        //if (Input.GetKeyDown("a"))
        //{
        //    particle.Play();
        //}
        //if (Input.GetKeyDown("b"))
        //{
        //    particle.Stop();
        //}
        //if (Input.GetKeyDown("c"))
        //{
        //    particle.Pause();
        //}
        //if (Input.GetKeyDown("d"))
        //{
        //    Destroy(particleGo);
        //}
    }
}


粒子系统触发回调
触发回调方法OnParticleTrigger();触发函数比较特殊,每帧都会执行的,大多是用在于需要对发生触发的粒子进行一些处理的时候用的。
获取发生触发的粒子:GetTriggerParticles();参数是触发类型和List<ParticleSystem.Particle>,其中List是用来缓存这一帧发生触发的粒子的。

脚本控制程序触发回调

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleTest : MonoBehaviour
{
    //GameObject particleGo;
    ParticleSystem particle;

    // Start is called before the first frame update
    void Start()
    {
        particle = transform.GetComponent<ParticleSystem>();
        //particleGo = GameObject.Instantiate(Resources.Load<GameObject>("22_RFX_Fire_Campfire1"));
        //particleGo.transform.position = transform.position;
        //particle = particleGo.GetComponent<ParticleSystem>();

        //ParticleSystem.MainModule mainModule = particle.main;
        //mainModule.loop = true;
    }

    private void OnParticleTrigger()
    {
        //用来缓存这一帧触发的粒子的
        List<ParticleSystem.Particle> particles = new List<ParticleSystem.Particle>();
        //获取每一帧发生触发的粒子
        int numEnter = particle.GetTriggerParticles(ParticleSystemTriggerEventType.Enter,particles);
        for(int i =0; i < numEnter; i++)
        {
            ParticleSystem.Particle pt= particles[i];
            pt.startColor = Color.red;
            particles[i] = pt;
        }
        particle.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, particles);
    }

    // Update is called once per frame
    void Update()
    {
        //if (Input.GetKeyDown("a"))
        //{
        //    particle.Play();
        //}
        //if (Input.GetKeyDown("b"))
        //{
        //    particle.Stop();
        //}
        //if (Input.GetKeyDown("c"))
        //{
        //    particle.Pause();
        //}
        //if (Input.GetKeyDown("d"))
        //{
        //    Destroy(particleGo);
        //}
    }
}

特效系统之拖尾
拖尾:作用:拖尾经常会用于做一些人物或者武器等移动后产生的拖尾效果。
脚本中常用的属性:TrailRenderer.emitting属性,是一个布尔值,true代表开始根据位置变化产生拖尾,false代表暂停产生拖尾。
 

编写脚本控制拖尾,脚本名字为RenderTest.cs,挂载到机器人身上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RenderTest : MonoBehaviour
{
    GameObject go;
    // Start is called before the first frame update
    void Start()
    {
        go = GameObject.Instantiate(Resources.Load<GameObject>("Accelerate"));
        go.transform.SetParent(transform);
        go.transform.localPosition = new Vector3(0,0.5f,0f);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            go.GetComponent<TrailRenderer>().emitting = true;
        }
        if (Input.GetKeyDown("b"))
        {
            Destroy(go);
        }
    }
}

特效系统之粒子力场

### 关于Unity特效的教程和资源 #### Unity中的Shader与特效开发 在Unity中,`SpecialEffects`模块提供了一个基于Shader库的功能集,其中包含了多种UI特效以及模型网格特效。这些效果主要用于增强视觉表现力并改善用户体验[^1]。 #### 完整的艺术资产包支持 对于希望深入研究角色动画和其他复杂场景构建的人来说,《战斗准备艺术包》提供了超过500种独特的美术素材,并附带演示场景以激发灵感。通过此项目可以在Unity Learn Premium上继续探索更多有关人物动作的知识[^2]。 #### 学习如何利用Unity创造互动体验 官方文档不仅涵盖了从入门级概念到高级技巧的内容,还指导开发者们掌握创建最优质的交互式娱乐产品所需的一切技能。无论是作为新手还是有经验的专业人士都能从中受益匪浅[^3]。 #### Shaders的作用及其应用领域 着色器程序位于计算机图形渲染流水线之中,负责定义物体表面的颜色、光照以及其他属性。它们广泛应用于现代游戏内的各种特殊效果制作当中,比如景深模糊、光影追踪等[^4]。 #### 更容易的学习路径对比其他框架 相较于SceneKit而言,许多程序员反馈说Unity拥有更平缓的学习曲线,尤其是在三维内容创作方面更为友好。这使得初学者能够更快地上手实践AR应用程序之前先熟悉基本的三维建模和技术[^5]。 ```csharp // 示例C#脚本片段展示如何加载预制件 Prefab 并设置其材质 Material 属性 using UnityEngine; public class ApplyMaterialToPrefab : MonoBehaviour { public GameObject prefab; public Material newMaterial; void Start() { Renderer rendererComponent = Instantiate(prefab).GetComponent<Renderer>(); if (rendererComponent != null) { rendererComponent.material = newMaterial; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_41392061

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值