Table of contents
We all know that the model can be imported into the static mesh in the blueprint, just drag it in.
But in c++, how should we find the model we want from the content and put this model into the static mesh?
First we created a simple actor header and cpp file.
won't create look at this:
Steps
As shown in the picture:
Then we need to create a static mesh component and place it under the root component, remember to add the header file at the header file:
Then we can find the required files under the resource folder content:
You can see that the following paragraph is the address of the model:
It is worth noting that our address cannot be filled in wrongly. Generally, if it is wrongly filled in, it will cause the compiler to crash, which is a very serious problem.
Another point worth noting is that the path starts with /Game instead of your project file. No matter what the name of your project file is, it must start with /Game, otherwise an error will be reported.
Then the addition is complete:
We don't need to set up the most primitive static mesh in the blueprint:
code:
head File:
// 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 "MyActor.generated.h"
UCLASS()
class UEDSTUDY_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* myMesh;
};
cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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;
//创建这个组件,并且放在根组件下面
myMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
myMesh->SetupAttachment(RootComponent);
//查找content里面的模型
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeAssist(TEXT("/Game/Mesh/Cube.Cube"));
//判断是否找到,是的话,将模型设置上去,然后设置相对位置是0,0,0
if (CubeAssist.Succeeded())
{
myMesh->SetStaticMesh(CubeAssist.Object);
myMesh->SetRelativeLocation(FVector(0, 0, 0));
}
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}