39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(ParticleSystem))]
|
|
public class ParticleGridSnap : MonoBehaviour
|
|
{
|
|
[SerializeField, Min(0.01f)] private float _cellSize = 0.2f;
|
|
|
|
private ParticleSystem _particleSystem;
|
|
private ParticleSystem.Particle[] _particles = Array.Empty<ParticleSystem.Particle>();
|
|
|
|
private void Awake()
|
|
{
|
|
_particleSystem = GetComponent<ParticleSystem>();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
int count = _particleSystem.particleCount;
|
|
if (count == 0)
|
|
return;
|
|
|
|
if (_particles.Length < count)
|
|
Array.Resize(ref _particles, count);
|
|
|
|
count = _particleSystem.GetParticles(_particles);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector3 position = _particles[i].position;
|
|
position.x = Mathf.Round(position.x / _cellSize) * _cellSize;
|
|
position.y = Mathf.Round(position.y / _cellSize) * _cellSize;
|
|
_particles[i].position = position;
|
|
}
|
|
|
|
_particleSystem.SetParticles(_particles, count);
|
|
}
|
|
}
|