问题描述
我有很多影响游戏玩法的简单对象,其中有成千上万个!嗯,不是成千上万,而是很多.因此,如果我将它们制作为游戏对象,则FPS会降低,尤其是在生成它们时.即使有池.我应该尝试另一种方法.
I have lots of same simple objects that are affecting gameplay, Well, not thousands, but really many. So if I make them GameObjects, the FPS decreases, especially when spawning them. Even with pooling. I should try a different approach.
您知道Unity3D中的粒子系统可以非常快速地渲染许多粒子.它还会自动控制粒子,发射并删除它们.但就我而言,对象的位置和生存期是由游戏逻辑管理的,没有我的命令,粒子系统不允许做任何事情,甚至不能重新排列粒子.
You know that particle system in Unity3D can render many particles very fast. It also automatically controls particles, emits and removes them. But in my case, positions and lifetimes of objects are managed by game logic, and particle system is not allowed to do anything without my command, even reorder particles.
我正在尝试使用SetParticles
方法来控制粒子.它在测试项目中有效,我先使用GetParticles
.我什至可以删除将生存期设置为-1的粒子,但是无法生成新粒子.同样,它也不会阻止粒子系统控制粒子.
I am trying to use SetParticles
method to control particles. It works in test project, where I use GetParticles
first. I can even remove particles setting lifetime to -1, but can't spawn new ones. Also it does not prevent particle system from controlling particles.
我可以禁用发射,因此不会自动创建任何粒子.
我可以将粒子速度设置为0,这样它们就不会移动.
我可以将生命周期设置为大量,这样就不会删除它们.
I can disable emission so no particles will be created automatically.
I can set particles speed to 0 so they will not move.
I can set lifetime to huge number so they will not be removed.
我有一个Particle
实例池,以避免不必要的GC分配.生成对象时会收到对粒子的引用,更新时会对其进行更改,并将生存期设置为-1,删除时将其返回到池中.池中存储以下内容:
I have an pool of Particle
instances, to avoid unnessessary GC allocations. Objects receive a reference to particle when they are spawned, change it when updated, and set lifetime -1 and return it to pool when deleted. The pool stores this:
private ParticleSystem.Particle [] _unusedParticles;
private int _unusedCount;
private ParticleSystem.Particle [] _array;
_unused
数组和计数器用于池化,_array
存储所有已使用和未使用的粒子,并用于SetParticles
调用.
_unused
array and counter are needed for pooling, and _array
stores all particles, used and unused alike, and is used in SetParticles
call.
此方法的主要缺点是它不起作用,可能是因为SetParticles
不会创建新粒子.而且我猜想它对粒子绘制顺序没有任何作用,这就是为什么它不适合子弹地狱游戏的原因,子弹地狱游戏应该看起来不错.
The main disadvantage of this method is that it doesn't work, probably because SetParticles
does not create new particles. Also I guess it doesn't do anything with particles draw order, that's why it's poorly suited for bullet hell games where bullet patterns should look nice.
我应该怎么做才能正确禁用粒子的自动控制并正确设置直接控制(包括生成和移除)?
推荐答案
您正在寻找的可能是
List<Matrix4x4> matrixes=new List<Matrix4x4>();
for (...)
{
matrixes.Add(Matrix4x4.TRS( position,rotation,scale));
}
Graphics.DrawMeshInstanced(mesh,0,material, matrixes);
在每一帧上,您只需更新位置,旋转和比例,并通过一次绘制调用就可以在GPU上渲染所有实例(与单独的游戏对象相比,速度相当快).通过这种方式,您一次通话最多可以渲染1000个实例
On each frame you can just update positions, rotations and scales, and get all the instances rendered on the GPU in one drawcall (pretty darn fast compared to seperate gameobjects). You can render up to 1000 instances in one call using this way
这篇关于如何在防止粒子系统执行相同操作的同时直接控制粒子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!