UE4 C++ 创建对象的几种方法

Unreal Engine4 C++ 创建对象的几种方法

好记性不如烂笔头啊,还是记录一下!


1.创建Actor对象

创建Actor对象,需要使用UWorld::SpawnActor()接口,如下所示:

* 创建AActor派生类对象不要用NewObject或者new,而要用UWorld::SpawnActor() 
*/</span>  
UWorld* World = GetWorld();  
FVector pos(<span style="color:#006666 !important">150</span>, <span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">20</span>);  

AMyActor* MyActor = World->SpawnActor<AMyActor>(pos, FRotator::ZeroRotator);   </code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.创建组件

在UE4中,为Actor创建组件,可以使用UObject::CreateDefaultSubobject()模板函数,这个函数只能在构造函数中调用。如下所示:

<span style="color:#000000"><code class="language-cpp"><span style="color:#880000 !important">/* <CreateObjectDemo> 
* 创建Component对象,要使用CreateDefaultSubobject模板函数 
*/</span>  
MyComponent = CreateDefaultSubobject<UMyActorComponent>(TEXT(<span style="color:#009900 !important">"MyComponent"</span>));  </code></span>
  • 1
  • 2
  • 3
  • 4

注意:这里有坑,TEXT(“MyComponent”)的名字不能重复!!


3.加载资源对象

在UE4中,项目中的所有资源文件,不要看做是文件,而要理解为“静态对象”:也就是对象序列化的产物。加载项目资源可以使用“UObject::StaticLoadObject()”函数,其中重要的参数为对象的Name,而不是文件路径。UE底层提供文件读取功能,无论资源文件是存储我独立的.uasset文件,还是存储到.PAK文件中,对于上层都不需要关心。

<span style="color:#000000"><code class="language-cpp"><span style="color:#880000 !important">/* <CreateObjectDemo> 
* 加载模型、贴图等对象,使用StaticLoadObject函数 
*/</span>  
UStaticMesh* SM_Vase = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(),  
    NULL,  
    TEXT(<span style="color:#009900 !important">"/Game/Assets/StaticMeshes/SM_Vase"</span>))  
    );  

StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(<span style="color:#009900 !important">"StaticMeshComponent"</span>));  
StaticMeshComponent->SetStaticMesh(SM_Vase);</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.创建UObject对象

如果你有UObject的派生类(非Actor、非ActorComponent),那你可以使用NewObject()模板函数来创建其实例对象。

<span style="color:#000000"><code class="language-cpp"><span style="color:#880000 !important">/* <CreateObjectDemo> 
 * 使用NewObject模板函数,来创建UObject派生类对象 
*/</span>  
MyObject = NewObject<UMyObject>(); </code></span>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and problems Carefully organized instructions to solve problems efficiently Clear explanations of what you did Solutions that can be applied to solve real-world problems Book Description Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to use the power of C++ scripting while developing games with UE4. It will start with adding and editing C++ classes from within the Unreal Editor. It will delve into one of Unreal's primary strengths, the ability for designers to customize programmer-developed actors and components. It will help you understand the benefits of when and how to use C++ as the scripting tool. With a blend of task-oriented recipes, this book will provide actionable information about scripting games with UE4, and manipulating the game and the development environment using C++. Towards the end of the book, you will be empowered to become a top-notch developer with Unreal Engine 4 using C++ as the scripting language. What you will learn Build function libraries (Blueprints) containing reusable code to reduce upkeep Move low-level functions from Blueprint into C++ to improve performance Abstract away complex implementation details to simplify designer workflows Incorporate existing libraries into your game to add extra functionality such as hardware integration Implement AI tasks and behaviors in Blueprints and C++ Generate data to control the appearance and content of UI elements
UE4中,根据不同的类别,有不同的方法创建对象。 对于继承自UObject但不继承自Actor的类,我们可以使用NewObject函数来创建对象。例如,我们可以创建一个UObjectClass类的对象,代码如下: ```cpp UObjectClass* MyClass = NewObject<UObjectClass>(); ``` 这样创建的对象将由UE4引擎来管理。 对于纯C++类(即非继承自UObject的类,一般以F开头),我们可以使用new关键字来创建对象,并使用TSharedPtr和TSharedRef来管理对象。例如,我们可以创建一个MyClass类的对象,代码如下: ```cpp TSharedPtr<MyClass> MyClassPtr = MakeShareable(new MyClass()); ``` 这样创建的对象将由TSharedPtr来进行引用计数,并在不再需要时自动释放内存。 对于继承自UObject类的组件类,我们也可以像创建UObject子类一样使用NewObject函数来创建组件对象。但是,创建组件后,我们需要使用RegisterComponent或RegisterAllComponents函数将其注册才能使其生效。例如,我们可以创建一个UStaticMeshComponent类的组件对象,并注册到当前对象上,代码如下: ```cpp UStaticMeshComponent* MyMeshComp = NewObject<UStaticMeshComponent>(this, TEXT("MyMeshComp")); MyMeshComp->SetupAttachment(RootComponent); MyMeshComp->SetRelativeLocation(FVector(0.f, 0.f, 0.f)); UStaticMesh* StaticMesh = LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/StaticMesh.StaticMesh'")); MyMeshComp->SetStaticMesh(StaticMesh); MyMeshComp->RegisterComponent(); ``` 这样创建的组件对象将由UE4引擎来管理,并在适当的时候进行更新和渲染。 总结起来,UE4创建对象方法根据类别的不同而不同。对于继承自UObject的类,我们可以使用NewObject函数来创建对象;对于纯C++类,我们可以使用new关键字来创建对象,并使用TSharedPtr和TSharedRef来管理对象;对于继承自UObject的组件类,我们可以使用NewObject函数来创建组件对象,并使用RegisterComponent或RegisterAllComponents函数将其注册。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值