当前位置: 首页 > 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、运行效果

相关文章:

  • OBS studio 减少音频中的杂音(噪音)
  • OSCP备战-kioptrixvm3详细解法
  • CMOS内存的地址空间在主内存空间中吗?
  • sunset:Solstice靶场
  • C++学习之STL学习
  • PowerShell 脚本中文乱码处理
  • 力扣热题100之合并两个有序链表
  • 软件架构师知识点总结
  • Java常用类-比较器
  • 华为云Flexus+DeepSeek征文|DeepSeek-V3/R1商用服务开通教程以及模型体验
  • [Java实战]Spring Boot 静态资源配置(十三)
  • ARM GIC(七)亲和路由:GICD_IROUTER寄存器具体如何与MPIDR配合使用?
  • 2050年的世界是怎样的?
  • tensorflow 1.x
  • AI智慧公园管理方案:用科技重塑市民的“夜游体验”
  • kubectl系列(十二):查询pod的resource 配置
  • C++编程中,什么是野指针?
  • Linux系统编程之消息队列
  • 关于TIAV20 PLCSIM仿真错误的原因
  • ST表(稀疏表)
  • 安徽省委副秘书长、省委政研室主任余三元调任省社科院院长
  • “水运江苏”“航运浙江”,江浙两省为何都在发力内河航运?
  • 技术派|更强的带刀侍卫:从054B型战舰谈谈世界护卫舰发展
  • 马上评丨75万采购300元设备,仅仅终止采购还不够
  • 《审判》|“被告”的魅力:K在等什么?
  • 网信部门曝光网络谣言典型案例,“AI预测彩票号码百分百中奖”等在列