发布时间:2023-11-27 18:56:30 浏览量:181次
UE5编写的3D魔方益智小游戏。游戏界面和游戏控制是很值得学习的。借助本游戏源码你可了解一些用UE5编写游戏的基础技巧。
一、绘制魔方
我们需要绘制一个3x3的魔方,共9个,每个方块的大小为1x1x1,并且魔方的中心点在坐标(0, 0, 0)。
代码如下:
class MOFANG2_API AMoFangActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMoFangActor();
int CubeColor[6][10][10][10];
UPROPERTY(EditAnywhere)
int Jie =3;
UPROPERTY(VisibleAnywhere)
TArray<UStaticMeshComponent*> CubeComponent;
UPROPERTY(EditAnywhere)
UStaticMesh* CubePlaneStaticMesh;
//设置六面材质
UPROPERTY(EditAnywhere)
TArray<UMaterialInterface*> MediaTextureMat;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
virtual void Create3DMofang();
};
AMoFangActor::AMoFangActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//三维魔方生成
for (int i = 0; i < Jie * Jie * Jie; i++)
{
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 1%d"), (i)))));
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 2%d"), (i)))));
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 3%d"), (i)))));
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 4%d"), (i)))));
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 5%d"), (i)))));
CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 6%d"), (i)))));
}
}
// Called when the game starts or when spawned
void AMoFangActor::BeginPlay()
{
Super::BeginPlay();
Create3DMofang();
for (int i = 0; i < Jie * Jie * Jie; i++)
{
FVector center = FVector((i % Jie) * 100 - 100, ((i / Jie) % Jie) * 100 - 100, ((i / Jie / Jie) % Jie) * 100 - 100);
//上
if (CubeColor[0][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 0]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 0]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 0]->SetMaterial(0, MediaTextureMat[CubeColor[0][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 0]->SetRelativeLocationAndRotation(center + FVector(0, 0, 50), FRotator(0, 0, 0));
CubeComponent[i * 6 + 0]->RegisterComponent();
}
//下
if (CubeColor[1][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 1]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 1]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 1]->SetMaterial(0, MediaTextureMat[CubeColor[1][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 1]->SetRelativeLocationAndRotation(center + FVector(0, 0, -50), FRotator(180, 0, 0));
CubeComponent[i * 6 + 1]->RegisterComponent();
}
//←
if (CubeColor[2][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 2]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 2]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 2]->SetMaterial(0, MediaTextureMat[CubeColor[2][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 2]->SetRelativeLocationAndRotation(center + FVector(0, -50, 0), FRotator(0, 0, 270));
CubeComponent[i * 6 + 2]->RegisterComponent();
}
//→
if (CubeColor[3][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 3]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 3]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 3]->SetMaterial(0, MediaTextureMat[CubeColor[3][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 3]->SetRelativeLocationAndRotation(center + FVector(0, 50, 0), FRotator(0, 0, 90));
CubeComponent[i * 6 + 3]->RegisterComponent();
}
//前
if (CubeColor[4][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 4]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 4]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 4]->SetMaterial(0, MediaTextureMat[CubeColor[4][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 4]->SetRelativeLocationAndRotation(center + FVector(-50, 0, 0), FRotator(90, 0, 0));
CubeComponent[i * 6 + 4]->RegisterComponent();
}
//后
if (CubeColor[5][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)
{
CubeComponent[i * 6 + 5]->SetupAttachment(RootComponent);
CubeComponent[i * 6 + 5]->SetStaticMesh(CubePlaneStaticMesh);
CubeComponent[i * 6 + 5]->SetMaterial(0, MediaTextureMat[CubeColor[5][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);
CubeComponent[i * 6 + 5]->SetRelativeLocationAndRotation(center + FVector(50, 0, 0), FRotator(270, 0, 0));
CubeComponent[i * 6 + 5]->RegisterComponent();
}
}
}
// Called every frame
void AMoFangActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMoFangActor::Create3DMofang()
{
for (int col = 0; col < 6; col++)
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
for (int k = 0; k< Jie; k++)
CubeColor[col][i][j][k] = -1;
//上九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[0][i][j][2] = 0;
//下九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[1][i][j][0] = 1;
//左九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[2][i][0][j] = 2;
//右九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[3][i][2][j] = 3;
//九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[4][0][i][j] = 4;
//九个面
for (int i = 0; i < Jie; i++)
for (int j = 0; j < Jie; j++)
CubeColor[5][2][i][j] = 5;
}
热门资讯
探讨游戏引擎的文章,介绍了10款游戏引擎及其代表作品,涵盖了RAGE Engine、Naughty Dog Game Engine、The Dead Engine、Cry Engine、Avalanche Engine、Anvil Engine、IW Engine、Frostbite Engine、Creation引擎、Unreal Engine等引擎。借此分析引出了游戏设计领域和数字艺术教育的重要性,欢迎点击咨询报名。
游戏中玩家将面临武侠人生的挣扎抉择,战或降?杀或放?每个抉定都将触发更多爱恨纠葛的精彩奇遇。《天命奇御》具有多线剧情多结局,不限主线发展,高自由...
3. B站视频剪辑软件「必剪」:免费、炫酷特效,小白必备工具
B站视频剪辑软件「必剪」,完全免费、一键制作炫酷特效,适合新手小白。快来试试!
4. 手机游戏如何开发(如何制作传奇手游,都需要准备些什么?)
如何制作传奇手游,都需要准备些什么?提到传奇手游相信大家都不陌生,他是许多80、90后的回忆;从起初的端游到现在的手游,说明时代在进步游戏在更新,更趋于方便化移动化。而如果我们想要制作一款传奇手游的
5. 3D动画软件你知道几个?3ds Max、Blender、Maya、Houdini大比拼
当提到3D动画软件或动画工具时,指的是数字内容创建工具。它是用于造型、建模以及绘制3D美术动画的软件程序。但是,在3D动画软件中还包含了其他类型的...
三昧动漫对于著名ARPG游戏《巫师》系列,最近CD Projekt 的高层回应并不会推出《巫师4》。因为《巫师》系列在策划的时候一直定位在“三部曲”的故事框架,所以在游戏的出品上不可能出现《巫师4》
UI设计师、动画设计师、特效设计师每一个职位的功能和负责的方面都不同。所以,3D美术这方面需要大量的人才。但是要成为一个3D游戏建模师,也不是那么...
众所周知,虚幻引擎5(下面简称UE5)特别占用存储空间,仅一个版本安装好的文件就有60G,这还不包括我们在使用时保存的工程文件和随之产生的缓存文件。而...
一、声音优化在绝地求生游戏中能够提前听到脚步声往往能提前取得战机,主要有两种方法:1、利用SoundLock软件,软件的功能主要是限制最大音量。百度搜索...
想让你的3D打印模型更坚固?不妨尝试一下Cura参数设置和设计技巧,让你轻松掌握!
最新文章