Ue0:从零开始的虚幻生活

Posted 才极

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ue0:从零开始的虚幻生活相关的知识,希望对你有一定的参考价值。

Ue0从零开始的虚幻生活(七):制作一个云彩

目标

制作一个可以自动生成样式并且玩家碰撞后会自我销毁并播放销毁动画的云彩

1.完成云彩的头文件

创建一个cloud类并声明变量

protect:
	UPROPERTY(VisibleAnyWhere, Category = "Conllision")
		UBoxComponent* BoxConllision;

	UPROPERTY(VisibleAnyWhere,BlueprintReadOnly, Category = "Show")
		UStaticMeshComponent* CloudPlane;
		
	UPROPERTY(EditAnyWhere, Category = "Show")
		TArray<UTexture*> Textures;

	UPROPERTY(BlueprintReadOnly, Category = "Show")
	UMaterialInstanceDynamic* MatInstance;

	UMaterialInterface* MatInterface;
	
	UPROPERTY(VisibleAnyWhere, Category = "Show")
	UTextRenderComponent* ScoreText;

	AMuffin* Muffin;//与之碰撞的角色

	UPROPERTY(EditAnyWhere, Category = "Sound")
	USoundCue* CloudSound;
	
	UPROPERTY(VisibleAnyWhere, Category = "Show")
	UTextRenderComponent* ScoreText;

声明需要用到的函数

protect:
	void SetRandomCloudeTexture();//设置云彩样式
	
	UFUNCTION(BlueprintImplementableEvent)
	void FadeOut();//播放销毁效果(在蓝图内完成)

public:
	virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;//检测碰撞

2.在cpp文件中实现构造函数并完成BeginPlay()

BoxConllision = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxConllision"));
	RootComponent = BoxConllision;

	CloudPlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CloudPlane"));
	CloudPlane->SetupAttachment(RootComponent);

	RainPlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RainPlane"));
	RainPlane->SetupAttachment(CloudPlane);

	ScoreText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("ScoreText"));
	ScoreText->SetupAttachment(RootComponent);

	AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComponent"));
	AudioComponent->SetupAttachment(CloudPlane);
void ACloud::BeginPlay()

	Super::BeginPlay();
	SetRandomCloudeTexture();
	EnableRain();

3.实现SetRandomCloudeTexture()函数

MatInterface = CloudPlane->GetMaterial(0);
	MatInstance = CloudPlane->CreateDynamicMaterialInstance(0,MatInterface);
	int Index = FMath::RandRange(0,2);
	if (Textures[Index])
	
		MatInstance->SetTextureParameterValue(FName(TEXT("Texture")), Textures[Index]);
		CloudPlane->SetMaterial(0, MatInstance);
	

4.实现NotifyActorBeginOverlap()函数

Super::NotifyActorBeginOverlap(OtherActor);
	Muffin = Cast<AMuffin>(OtherActor);
	if (Muffin)
	
		Muffin->Lauanch();
		DisplayScore();
		UGameplayStatics::PlaySoundAtLocation(this, CloudSound, GetActorLocation());
		FadeOut();
	

5.在蓝图中实现FadeOut()函数


以上是关于Ue0:从零开始的虚幻生活的主要内容,如果未能解决你的问题,请参考以下文章

Ue0:从零开始的虚幻生活

Ue0:从零开始的虚幻生活

Ue0:从零开始的虚幻生活

Ue0:从零开始的虚幻生活

Ue0:从零开始的虚幻生活

Ue0:从零开始的虚幻生活