`
aigo
  • 浏览: 2540043 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

[UE4]C++代码实现播放粒子特效

UE4 
阅读更多

函数参数说明(在GameplayStatics.h中)

将一个粒子放到指定位置上播放: 

/** Plays the specified effect at the given location and rotation, fire and forget. The system will go away when the effect is complete. Does not replicate.
	 * @param WorldContextObject - Object that we can obtain a world context from
	 * @param EmitterTemplate - particle system to create
	 * @param Location - location to place the effect in world space
	 * @param Rotation - rotation to place the effect in world space	
	 * @param bAutoDestroy - Whether the component will automatically be destroyed when the particle system completes playing or whether it can be reactivated
	 */
	UFUNCTION(BlueprintCallable, Category="Effects|Components|ParticleSystem", meta=(Keywords = "particle system", WorldContext="WorldContextObject", UnsafeDuringActorConstruction = "true"))
	static UParticleSystemComponent* SpawnEmitterAtLocation(UObject* WorldContextObject, class UParticleSystem* EmitterTemplate, FVector Location, FRotator Rotation = FRotator::ZeroRotator, bool bAutoDestroy = true);

 另一种重载形式:

static UParticleSystemComponent* SpawnEmitterAtLocation(UWorld* World, class UParticleSystem* EmitterTemplate, const FTransform& SpawnTransform, bool bAutoDestroy = true);

 

 

将一个粒子attach到某个Component上播放:

/** Plays the specified effect attached to and following the specified component. The system will go away when the effect is complete. Does not replicate.
	* @param EmitterTemplate - particle system to create
	 * @param AttachComponent - Component to attach to.
	 * @param AttachPointName - Optional named point within the AttachComponent to spawn the emitter at
	 * @param Location - Depending on the value of Location Type this is either a relative offset from the attach component/point or an absolute world position that will be translated to a relative offset
	 * @param Rotation - Depending on the value of LocationType this is either a relative offset from the attach component/point or an absolute world rotation that will be translated to a realative offset
	 * @param LocationType - Specifies whether Location is a relative offset or an absolute world position
	 * @param bAutoDestroy - Whether the component will automatically be destroyed when the particle system completes playing or whether it can be reactivated
	 */
	UFUNCTION(BlueprintCallable, Category="Effects|Components|ParticleSystem", meta=(Keywords = "particle system", UnsafeDuringActorConstruction = "true"))
	static UParticleSystemComponent* SpawnEmitterAttached(class UParticleSystem* EmitterTemplate, class USceneComponent* AttachToComponent, FName AttachPointName = NAME_None, FVector Location = FVector(ForceInit), FRotator Rotation = FRotator::ZeroRotator, EAttachLocation::Type LocationType = EAttachLocation::KeepRelativeOffset, bool bAutoDestroy = true);

 

 

具体使用实例

头文件,其中MuzzleFX在蓝图中设置为具体哪个粒子资源,Mesh是要AttachTo的Component

UPROPERTY(EditDefaultsOnly)
UParticleSystem* MuzzleFX;

/** weapon mesh: 3rd person view */
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
USkeletalMeshComponent* Mesh;


cpp中调用:

//此方法只播放一遍即结束
UGameplayStatics::SpawnEmitterAttached(MuzzleFX, Mesh, "MyAttachPoint");

 

 

 

 

 

=====================================================
上面粒子是用蓝图直接创建ParticleSystem(粒子系统),下面是用C++代码创建ParticleSystem:

头文件中定义:

UParticleSystem* TestParticle;
UParticleSystemComponent* LastPSC;

 

 

创建ParticleSystem:

void AHGameMode::LoadParticlesClass()
{
	static ConstructorHelpers::FObjectFinder<UParticleSystem> MyParticleSystem(TEXT("ParticleSystem'/Game/Particles/Oneshot/P_mc01CIrcle05_large_oneshot.P_mc01CIrcle05_large_oneshot'"));
	if (MyParticleSystem.Succeeded()) 
	{
		TestParticle = MyParticleSystem.Object;
	}
}

 

播放粒子:

void AHGameMode::PlayParticle_01()
{
	LastPSC = UGameplayStatics::SpawnEmitterAttached(TestParticle, MyCharacter->GetMesh(), "MyParticle");
}

 

  

如果是循环播放的粒子,手动控制粒子的播放和关闭(如果是一个只播放一次的粒子,可以不用手动停止):

//ActivateSystem的bool参数表示开启特效后是否重置特效(从头播放)
LastPSC->ActivateSystem(true);

 
关闭特效:

LastPSC->DeactivateSystem();

 
如果希望 DeactivateSystem 之后,再次开启 ActivateSystem 的时候能自动激活特效,需要禁用自动销毁:

UParticleSystemComponent->bAutoDestroy = false;

 

 

参考:

How can I get a particle to spawn in a specific location?

https://answers.unrealengine.com/questions/36451/particle-spawns-in-incorrect-location.html

 

How can I spawn an instance of an emitter during runtime?

https://answers.unrealengine.com/questions/42209/spawning-emitter-content-that-exists-in-the-conten.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics