Unity 파티클 시스템의 트리거 모듈 사용법
개요
Unity의 파티클 시스템의 트리거 모듈은 씬 내 콜라이더와의 상호작용을 기반으로 파티클을 제어할 수 있게 해주는 기능입니다. 이를 통해 파티클 리스트에 접근하고, 상태를 변경하거나 파괴할 수 있습니다.
트리거 모듈 활성화 방법
GameObject > Effects > Particle System
을 클릭하여 새로운 파티클 시스템을 만듭니다.- 인스펙터에서
Particle System
컴포넌트를 찾습니다. Particle System
컴포넌트 내의 트리거 모듈 섹션을 펼칩니다.- 트리거 모듈 체크박스를 활성화します.
콜라이더 추가
트리거 모듈이 활성화되면, 콜라이더를 추가해야 합니다. - Colliders List
속성에 콜라이더를 할당합니다. - 리스트에 더 많은 콜라이더를 추가하려면 추가(+) 버튼을 클릭합니다. - 리스트에서 콜라이더를 제거하려면 해당 콜라이더를 선택하고 제거(-) 버튼을 클릭합니다.
파티클 행동 설정
트리거 모듈은 네 가지 트리거 이벤트 타입에 대한 파티클 행동을 정의할 수 있습니다.
이벤트 타입 | 설명 |
---|---|
Inside | 파티클이 콜라이더의 경계 안에 있습니다. |
Outside | 파티클이 콜라이더의 경계 밖에 있습니다. |
Enter | 파티클이 콜라이더의 경계 안으로 들어갑니다. |
Exit | 파티클이 콜라이더의 경계 밖으로 나갑니다. |
각 이벤트 타입마다 수행할 작업을 설정할 수 있습니다.
작업 | 설명 |
---|---|
Callback | OnParticleTrigger() 콜백 함수에서 파티클에 액세스할 수 있습니다. |
Kill | 파티클을 파괴합니다. |
Ignore | 파티클을 무시합니다. |
API 사용법
트리거 모듈은 Particle System
컴포넌트의 일부로, ParticleSystem
클래스를 통해 접근합니다. 이 모듈에 대한 값 변경 방법은 다음 API 문서를 참조하십시오.
OnParticleTrigger()에서 파티클에 액세스
OnParticleTrigger()
함수를 추가하여 이벤트 조건을 만족하는 파티클에 액세스합니다. 다음과 같은 코드 조각을 사용할 수 있습니다:
void OnParticleTrigger()
{
// Your implementation here
}
예제: 콜라이더와의 상호작용
아래 코드는 파티클이 콜라이더의 경계에 들어가거나 나갈 때 색상을 변경하는 기능을 구현합니다.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class TriggerScript : MonoBehaviour
{
ParticleSystem ps;
List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle>();
List<ParticleSystem.Particle> exit = new List<ParticleSystem.Particle>();
void OnEnable()
{
ps = GetComponent<ParticleSystem>();
}
void OnParticleTrigger()
{
int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
int numExit = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
for (int i = 0; i < numEnter; i++)
{
ParticleSystem.Particle p = enter[i];
p.startColor = new Color32(255, 0, 0, 255);
enter[i] = p;
}
for (int i = 0; i < numExit; i++)
{
ParticleSystem.Particle p = exit[i];
p.startColor = new Color32(0, 255, 0, 255);
exit[i] = p;
}
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
}
}
결론
Unity의 파티클 시스템에서 트리거 모듈을 활용하면 상호작용을 통해 파티클을 다양한 방식으로 제어할 수 있습니다. 위의 방법과 예제를 통해 파티클 시스템을 개선하고 더 생동감 있게 만들 수 있습니다.