[UE4]C++中SpawnActor用法(动态创建Actor)

注:这里创建actor跟unity有明显不同

ue不能创建一个单独的actor,单独的actor也没意义,ue里的actor是用来继承的

这里创建的actor都是实例化actor的子类,细想下,并不影响动态创建对象。


ue417用下面的写法貌似直接挂了,看SpawnActor的具体参数来写最新参数



http://aigo.iteye.com/blog/2270177


C++中创建一个Level并添加的Runtime当中

Cpp代码   收藏代码
  1. Level.Add(GetWorld()->SpawnActor<ABuildingModLevel>());  

 

 

 

C++中Spawn一个基于蓝图的Actor

https://answers.unrealengine.com/questions/60897/spawn-actorobject-from-code.htm

Here is how I spawn a blueprint via C++. Note that the blueprint I spawn has a base class that was created in C++ also.

.h

Cpp代码   收藏代码
  1. TSubclassOf<YourClass> BlueprintVar; // YourClass is the base class that your blueprint uses  

 

 

.cpp(注意,这段代码必须放在构造函数中。UE4其他类型的蓝图,比如Widget蓝图,都可以通过下面这种方式加载。

Cpp代码   收藏代码
  1. ClassThatWillSpawnTheBlueprint::ClassThatWillSpawnTheBlueprint(const class FPostConstructInitializeProperties& PCIP)  
  2.     : Super(PCIP)  
  3. {  
  4.     static ConstructorHelpers::FObjectFinder<UBlueprint> PutNameHere(TEXT("Blueprint'/Path/To/Your/Blueprint/BP.BP'"));  
  5.     if (PutNameHere.Object)   
  6.     {  
  7.         BlueprintVar = (UClass*)PutNameHere.Object->GeneratedClass;  
  8.     }  
  9. }  

 

PutNameHere is just an arbitrary name you give to the constructor helper. The path to your blueprint is found by finding your blueprint in the content browser, right clicking it, and choosing Copy Reference. Then, just paste that in between the quotes.

Now, you're ready to spawn the blueprint. You can do it in BeginPlay() or wherever, just not in the constructor.(这段代码必须放在非构造函数中,比如BeginPlay()中

Cpp代码   收藏代码
  1. UWorld* const World = GetWorld(); // get a reference to the world  
  2. if (World)   
  3. {  
  4.     // if world exists  
  5.     YourClass* YC = World->SpawnActor<YourClass>(BlueprintVar, SpawnLocation, SpawnRotation);  
  6. }  

 

If you don't know your SpawnLocation or SpawnRotation you can just throw in FVector(0,0,0) and FRotator(0,0,0) instead.

So, since your blueprint base class was also created in C++ this makes it easy to interact with your blueprint from code. It's as simple as YC->SomeVariable = SomeValue. Hope that helps.

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的UE4 C++生成Actor的例子,假设你已经创建了一个新的C++项目: 1. 创建一个新的C++类,继承自AActor。例如,我们创建一个名为MyActor的类: ```cpp UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; }; ``` 2. 在MyActor.cpp文件实现构造函数和BeginPlay函数: ```cpp AMyActor::AMyActor() { // 设置Actor的根组件 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent")); // 创建一个静态网格组件 UStaticMeshComponent* MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); MeshComponent->SetupAttachment(RootComponent); // 加载静态网格 constructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); if (Mesh.Succeeded()) { MeshComponent->SetStaticMesh(Mesh.Object); } } void AMyActor::BeginPlay() { Super::BeginPlay(); // 在控制台输出Actor的位置 UE_LOG(LogTemp, Warning, TEXT("MyActor is at %s"), *GetActorLocation().ToString()); } ``` 3. 在MyActor.cpp文件实现Tick函数,使Actor在每帧旋转: ```cpp void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // 每帧旋转Actor FRotator Rotation = GetActorRotation(); Rotation.Yaw += DeltaTime * 100.f; SetActorRotation(Rotation); } ``` 4. 在MyActor.h文件添加头文件和构造函数声明: ```cpp #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Engine/StaticMesh.h" #include "Components/StaticMeshComponent.h" #include "MyActor.generated.h" UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true")) UStaticMeshComponent* MeshComponent; }; ``` 5. 在你的场景添加一个MyActor,例如在GameMode的BeginPlay函数: ```cpp void AMyProjectGameModeBase::BeginPlay() { Super::BeginPlay(); // 创建一个新的MyActor AMyActor* MyActor = GetWorld()->SpawnActor<AMyActor>(AMyActor::StaticClass(), FVector(0.f, 0.f, 0.f), FRotator(0.f, 0.f, 0.f)); } ``` 6. 运行游戏,你应该可以看到一个旋转的立方体Actor。 希望这个例子能够帮助你理解如何使用UE4 C++生成Actor

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值