Unreal Cook Book:创建对象

转自:http://blog.csdn.net/neil3d

DEMO源代码

这个DEMO演示了在C++代码中,创建UE4的常见类型的对象,包括Actor,ActorComponent,加载资源等。

源代码请从这里下载:https://code.csdn.net/neil3d/unrealcookbook

使用的引擎版本为:4.11.2

打开项目后,启动“CreateObjectDemo/CreateObjectDemoMap.umap”地图文件即可运行。


创建组件

在UE4中,为Actor创建组件,可以使用UObject::CreateDefaultSubobject()模板函数, 这个函数只能在构造函数中调用。如下所示:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* <CreateObjectDemo> 
  2.     * 创建Component对象,要使用CreateDefaultSubobject模板函数 
  3.     */  
  4.     MyComponent = CreateDefaultSubobject<UMyActorComponent>(TEXT("MyComponent"));  


加载资源对象

在UE4中,项目中的所有资源文件,不要看做是文件,而要理解为“静态对象”:也就是对象序列化的产物。加载项目资源可以使用“UObject::StaticLoadObject()”函数,其中重要的参数为对象的Name,而不是文件路径。UE底层提供文件读取功能,无论资源文件是存储我独立的.uasset文件,还是存储到.PAK文件中,对于上层都不需要关心。
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* <CreateObjectDemo> 
  2.     * 加载模型、贴图等对象,使用StaticLoadObject函数 
  3.     */  
  4.     UStaticMesh* SM_Vase = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(),  
  5.         NULL,  
  6.         TEXT("/Game/Assets/StaticMeshes/SM_Vase"))  
  7.         );  
  8.   
  9.     StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));  
  10.     StaticMeshComponent->SetStaticMesh(SM_Vase);  


创建Actor对象

创建Actor对象,需要使用UWorld::SpawnActor()接口,如下所示:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* <CreateObjectDemo> 
  2.      * 创建AActor派生类对象不要用NewObject或者new,而要用UWorld::SpawnActor() 
  3.     */  
  4.     UWorld* World = GetWorld();  
  5.     FVector pos(150, 0, 20);  
  6.   
  7.     AMyActor* MyActor = World->SpawnActor<AMyActor>(pos, FRotator::ZeroRotator);  


创建UObject对象

如果你有UObject的派生类(非Actor、非ActorComponent),那你可以使用NewObject()模板函数来创建其实例对象。
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* <CreateObjectDemo> 
  2.  * 使用NewObject模板函数,来创建UObject派生类对象 
  3. */  
  4. MyObject = NewObject<UMyObject>();  
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值