本文主要是介绍【UnrealEngine】官方编程快速入门遇到的坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
官方编程快速入门文档链接
添加如下代码,VS出现红线错误,或输入过程VS不跳语句补全。
原因:没有include相应的头文件。
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
解决方法:
在.h文件中加入如下内容。必须注意头文件引用的顺序,详情请自行摸索。
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "UObject/ConstructorHelpers.h"
遇到相应情况,请自行到官方API中查找相应组件需要Include的头文件路径。
在完成编译后,C++类预览还是处于Actor的球体状态,请查看是否有导入StarterContent资源包。
解决方法,①创建项目时候就选择了StarterContent资源包。(创建项目前可以这样操作,否则按②的操作)
②Add New->AddFeature or Content Pack...->Content Pack ->StarterContent (如图)
https://blog.csdn.net/Terrell21
下面为能正常运行的源码:
FloatingActor.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "UObject/ConstructorHelpers.h"
#include "FloatingActor.generated.h"UCLASS()
class INICONFIG_API AFloatingActor : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAFloatingActor();UPROPERTY(VisibleAnyWhere)UStaticMeshComponent* VisualMesh;UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "FloatingActor") //编辑可见,蓝图可读写,标签名 float FloatSpeed = 20.0f;UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "FloatingActor")float RotationSpeed = 20.0f;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;};
FloatingActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "FloatingActor.h"// Sets default values
AFloatingActor::AFloatingActor()
{// 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;VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh")); //创建静态网格组件VisualMesh->SetupAttachment(RootComponent); //将静态网格组件设置到根组件下。static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));//调用结构帮助器查找目标静态网格体。//检测是否成功查找到if (CubeVisualAsset.Succeeded() ){VisualMesh->SetStaticMesh(CubeVisualAsset.Object); //查找成功,将查找到的静态网格设置到静态网格组件中。VisualMesh->SetRelativeLocation(FVector(0, 0, 0)); //然后对静态网格组件设置相对位置。}}// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{Super::BeginPlay();}// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{Super::Tick(DeltaTime);FVector NewLocation = GetActorLocation(); //获取该帧位置FRotator NewRotation = GetActorRotation(); //获取该帧旋转量float RunningTime = GetGameTimeSinceCreation(); //获取时间(场景开始了多久的时间)float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));NewLocation.Z += DeltaHeight * FloatSpeed; //DeltaHeight 在-1 到 1的范围float DeltaRotation = DeltaTime * RotationSpeed;NewRotation.Yaw -= DeltaRotation; //绕Z轴旋转, 0不转 + 向北转(顺时针) - 向南转(逆时针)SetActorLocationAndRotation(NewLocation, NewRotation);}
这篇关于【UnrealEngine】官方编程快速入门遇到的坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!