UE5 C++(36-2):源代码,并编译生成 UE 蓝图,可见,蓝图里组件的参数已经对应上 c++ 代码里的参数了
(192)(193)
·
(192)头文件 :
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/projectileMovementComponent.h"
#include "MyBulletActor.generated.h"
UCLASS()
class UE5_CPP_YUXIANGROUSI_API AMyBulletActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyBulletActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "我的组件")
UStaticMeshComponent * ptrMySMComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "我的组件")
USphereComponent * ptrMyBallColliComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "我的组件")
UProjectileMovementComponent * ptrMyProjectileComp;
};
++

(193) 源文件 :
#include "MyBulletActor.h"
// Sets default values
AMyBulletActor::AMyBulletActor()
{
// 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;
//R* UObject::CreateDefaultSubobject<R>(FName SubobjectName, bool bTransient = false)
ptrMySMComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MySM"));
ptrMyBallColliComp = CreateDefaultSubobject<USphereComponent>(TEXT("MyBall"));
ptrMyProjectileComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("MyProjectile"));
ConstructorHelpers::FObjectFinder<UStaticMesh> tmpSM(TEXT( //加载静态网络体资源
"/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
ptrMySMComp->SetStaticMesh(tmpSM.Object); //加载球体资源并赋值给 SM 组件
//bool UStaticMeshComponent::SetStaticMesh(UStaticMesh* NewMesh)
ptrMySMComp->SetRelativeScale3D(FVector(0.4, 0.4, 0.4)); //缩放 SM 组件
//void USceneComponent::SetRelativeScale3D(FVector NewScale3D)
RootComponent = ptrMySMComp; //TObjectPtr<USceneComponent> AActor::RootComponent;
ptrMyBallColliComp->SetupAttachment(ptrMySMComp); //设置根组件并挂靠球形碰撞
//void USceneComponent::SetupAttachment(class USceneComponent* InParent, FName InSocketName = NAME_None)
ptrMyProjectileComp->SetUpdatedComponent(ptrMySMComp); //Assign the component we move and update
//void UMovementComponent::SetUpdatedComponent(USceneComponent* NewUpdatedComponent)
// Initial speed of projectile. If greater than zero,
// this will override the initial Velocity value and instead treat Velocity as a direction.
// 弹道的初始速度。如果大于零,这将覆盖初始速度值,并且将以速度作为方向来处理。
ptrMyProjectileComp->InitialSpeed = 1200; //float UProjectileMovementComponent::InitialSpeed
ptrMyProjectileComp->MaxSpeed = 2400; //float UProjectileMovementComponent::MaxSpeed
ptrMyProjectileComp->bRotationFollowsVelocity = true;
//uint8 UProjectileMovementComponent::bRotationFollowsVelocity:1;
ptrMyProjectileComp->bIsHomingProjectile = true;
//uint8 UProjectileMovementComponent::bIsHomingProjectile:1;
//如果为真,我们将加速朝向我们的归位目标。在弹道生成后必须设置 HomingTargetcomponent。
ptrMyProjectileComp->ProjectileGravityScale = 1.5;
//float UProjectileMovementComponent::ProjectileGravityScale;
//为该弹道设置自定义重力比例。设为0表示无重力。
}
// Called when the game starts or when spawned
void AMyBulletActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyBulletActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
++


(194)
谢谢
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)