当前位置: 首页 > news >正文

Unity3D仿星露谷物语开发42之粒子系统

1、目标

使用例子系统,实现割草后草掉落的特效。

通过PoolManager获取特效预制体,通过VFXManager来触发特效。

2、配置例子特效

在Hierarchy -> PersistentScene下创建新物体命名为Reaping。

给该物体添加Particle System组件。

配置例子系统参数:

  • Duration:1
  • Looping:不勾选
  • Start Lifetime:1
  • Start Speed:0.2
  • Start Size:0.3
  • Start Rotation:选择Random Between Two Constants模式,然后两个值分别为0和360
  • Gravity Modifier:0.3
  • Simulation Space:World
  • Emiter Velocity:Transform
  • Max Particles:25

Emission参数:

  • Rate Over Time:25
  • Bursts:修改Count为25

Shape参数:

  • Shape:Sphere
  • Radius:0.26
  • Radius Thickness:0.09

Velocity over Lifetime参数:

Color over Lifetime参数:

Size over Lifetime参数:

Rotation over Lifetime参数:

  • Angular velocity:180

Noise参数:

  • Strength:0.02
  • Quality:Medium

Texture Sheet Animation参数:

  • Mode:Sprites,图片选择effects_0

Renderere参数:

  • Material:选择Sprites-Default
  • Min Particle Size:0.005
  • Max Particle Size:0.009
  • Sorting Layer ID:Instances
  • Order in Layer:1

效果如上(此时Looping按钮被打开):可以看到不停放射出绿色长条,犹如割完草之后叶子掉落。

3、制作粒子预制体

将上述步骤制作的粒子保存为预制体。

在Assets -> Prefabs下创建目录命名为Crop,此目录下创建新目录命名为Effects。

然后将Hierarchy -> PersistentScene下的Reaping物体拖到该目录下,并且删除Hierarchy下的Reaping。

4、创建VFX管理器

VFX(Visual Effects),视觉特效。

在该模块下管理割草后草掉落的视觉特效。

(1)优化EventHandler.cs脚本

添加如下事件:

// Harvest Action Effect Event
public static event Action<Vector3, HarvestActionEffect> HarvestActionEffectEvent;public static void CallHarvestActionEffectEvent(Vector3 effectPosition, HarvestActionEffect harvestActionEffect)
{if(HarvestActionEffectEvent != null){HarvestActionEffectEvent(effectPosition, harvestActionEffect);}
}

(2)优化Enum.cs脚本

public enum HarvestActionEffect
{deciduousLeavesFalling,pipeConesFalling,choppingTreeTrunk,breakingStone,reaping,none
}

(3)创建VFXManager.cs脚本

在Assets -> Scripts -> VFX下创建新脚本命名为VFXManager.cs。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class VFXManager : SingletonMonobehaviour<VFXManager>
{private WaitForSeconds twoSeconds;[SerializeField] private GameObject reapingPrefab = null;protected override void Awake(){base.Awake();twoSeconds = new WaitForSeconds(2f);}private void OnDisable(){EventHandler.HarvestActionEffectEvent -= displayHarvestActionEffect;}private void OnEnable(){EventHandler.HarvestActionEffectEvent += displayHarvestActionEffect;}private IEnumerator DisableHarvestActionEffect(GameObject effectGameObject, WaitForSeconds secondsToWait){yield return secondsToWait;effectGameObject.SetActive(false);}private void displayHarvestActionEffect(Vector3 effectPosition, HarvestActionEffect harvestActionEffect){switch(harvestActionEffect){case HarvestActionEffect.reaping:GameObject reaping = PoolManager.Instance.ReuseObject(reapingPrefab, effectPosition, Quaternion.identity);reaping.SetActive(true); StartCoroutine(DisableHarvestActionEffect(reaping, twoSeconds));break;case HarvestActionEffect.none:break;default:break;}}
}

(4)优化Player.cs脚本

优化UseToolInPlayerDirection函数,添加如下一行代码:

(5)创建VFXManager对象

在Hierarchy -> PersistentScene下创建新的目录命名为VFXManager。

添加VFXManager组件,并且将Reaping预制体加到Reaping Prefab选项中。

(6)修改PoolManager信息

点击PoolManager物体,Pool还是保持1,修改Element0的Pool Size为10,将Reaping拖到Prefab选项。

5、运行效果

http://www.xdnf.cn/news/386587.html

相关文章:

  • 使用FastAPI和React以及MongoDB构建全栈Web应用05 FastAPI快速入门
  • Problem C: 异常1
  • 在Java项目中实现本地语音识别与热点检测,并集成阿里云智能语音服务(优化版)
  • 基于Qt的app开发第七天
  • leetcode 454. 4Sum II
  • 【数据库知识】Mysql进阶-高可用MHA(Master High Availability)方案
  • Git标签
  • 多模态大语言模型arxiv论文略读(六十八)
  • 各类有关NBA数据统计数据集大合集
  • Hibernate 性能优化:告别慢查询,提升数据库访问性能
  • 《Effective Python》第1章 Pythonic 思维详解——item03-05
  • C# 高效处理海量数据:解决嵌套并行的性能陷阱
  • 深入理解 JavaScript 中的 FileReader API:从理论到实践
  • React 从零到一执行原理 (2025 最新版)
  • 23、DeepSeek-V2论文笔记
  • 嵌入式硬件篇---IIC
  • 牛客周赛 Round 92 题解 Java
  • 力扣2680题解
  • D. Explorer Space(dfs+剪枝)
  • Kubernetes生产实战(二十七):精准追踪Pod数据存储位置
  • 【Beat Saber 节奏光剑】全身动捕直播搭建指南
  • 1688 API 自动化采集实践:商品详情实时数据接口开发与优化
  • SpEL(Spring Expression Language)使用详解
  • 从0开始学习大模型--Day06--大模型的相关网络架构
  • vs2022配置opencv
  • Linux511SSH连接 禁止root登录 服务任务解决方案 scp Vmware三种模式回顾
  • 数据分析预备篇---NumPy数组
  • postgres--MVCC
  • ARP协议
  • 【Python】异步优势演员-评论家(A3C)算法在Python中的实现与应用