您当前的位置:首页 VR开发 UE4 正文

从Unity引擎转向Unreal4引擎的学习(三)

查看: 1915| 评论: 4 2016-1-8 07:14 PM| 发布者: administrator |原作者: netfoucs

How to write code in UE4  如何在UE4中编码?

So, I'm used to programming in MonoDevelop� 在Unity中我已经习惯了使用MonoDevelop编译器编码

For Blueprint scripting you'll only need Unreal Editor -- everything is built in! To write code in C++, download the free version of Visual Studio on Windows, or install Xcode on Mac. When you first create a new project (or add code to an existing project), UE4 will automatically create Visual Studio project files for you. You can open Visual Studio by double-clicking on a C++ class inside Content Browser, or by clicking the "Open Visual Studio" button in the main File menu.

蓝图脚本的编码是在UE4的编辑器中就可以完成,C++编码使用最新版的vs。苹果用户使用Xcode。如何打开就不用多说了把....(⊙﹏⊙)b


One important difference in UE4: You'll sometimes have to manually(手动) refresh your Visual Studio project files (for example, after downloading a new version of UE4, or when manually making changes to source file locations on disk.) You can do this by clicking "Refresh Visual Studio Project" in the main menu, or by right clicking on the .uproject file in your project's directory and selecting "Generate Visual Studio project files"。有一点得注意,我们有时候需要手动去更新vs文件。(比如下载了新版本的UE4,或者是做了修改)。右键点选.uproject文件选中生成文件就行,如下图。


Writing Event Functions (Start, Update, etc.) 写事件函数

If you are used to working with MonoBehaviours you are used to such methods as Start, Update, and OnDestroy. Here is a comparison between a Unity behaviour and its counterpart in UE4 Actors and components.

使用Unity我们都清楚一些start(),update(),OnDestroy()这些函数,下面我们就做一个UE4和Unity的比较。

In Unity, we might have a simple component that looks like this:

Unity中简单的组件看上去张这个样子:

  1. public class MyComponent : MonoBehaviour{    void Start() {}    void OnDestroy() {}    void Update() {}}  

But remember, in UE4 you can write code right on the Actor than only coding new component types. This is actually very common and useful.

但是请记住,在UE4中,我们可以直接在Actor本身上写代码,而不是创建新的组件类型,这非常重要。

Similar to Unity's Start, OnDestroy and Update functions, we have a similar set of methods for Actors in UE4:

UE4中和Unity里面的那三个函数类似的函数:

C++ :

  1. UCLASS()class AMyActor : public AActor{    GENERATED_BODY()    // Called at start of game.    void BeginPlay();    // Called when destroyed.    void EndPlay(const EEndPlayReason::Type EndPlayReason);    // Called every frame to update this actor.    void Tick(float DeltaSeconds);};  
Blueprint :


Components in UE4 contain different functions. Here is a simple example:

UE4中的组件包含一些不同的函数,下面列举了一些简单例子:

C++ :

  1. UCLASS()class UMyComponent : public UActorComponent{    GENERATED_BODY()    // Called after the owning Actor was created    void InitializeComponent();    // Called when the component or the owning Actor is being destroyed    void UninitializeComponent();    // Component version of Tick    void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction);};  
Blueprint :


Remember in UE4 It's important to call the parent class' version of method.

请记住,在UE4中,调用父类的函数的版本是非常重要的。

For example in Unity C# this would be called base.Update(), but in UE4 C++ we'll use Super::TickComponent():

比如说,在unity里面这样写:base.Update();而在UE4中则需要这样写:Super::TickComponent();

  1. void UMyComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){    // Custom tick stuff here    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);}  

You may have noticed some things begin with "A" and others with "U" in C++. The prefix "A" indicates an Actor sub-class. Where as the prefix "U" indicates an Object sub-class. There are some other prefixes too, for example "F" is used for most plain data structures or non-UObject classes.UE4的C++代码中,字母前缀'A'表示是Actor的子类,'U'表示Object的子类。当然还有一些其他的前缀,比如‘F’,表示是一些空白的数据结构或者不是Object类。

Writing gameplay code in UE4  在UE4中编写游戏代码

Okay, things are going to get a bit deeper from here on out. We'll be talking about programming topics that are critical for creating games. Because you know Unity, we'll be explaining features with slant toward C# users learning Unreal C++, but you can use Blueprint Scripting for pretty much everything if you want! We've added examples in both C++ and Blueprint where possible.

Let's talk about some of the most common gameplay programming patterns and how you'd approach them in Unreal. Many functions in Unity have a comparable function in Unreal that should seem familiar. We'll go through the most common functions one by one.

好的,从这里开始我们将更加深入了。下面我们一一介绍一些最常用的函数。


52VR.COM微信扫一扫
52vr公众号
专注于VR的学习、开发和人才交流

52VR开发交流

相关推荐

已有 4 人参与

发表评论

您需要登录才可以回帖 登录 | 立即注册

手机版|VR开发网 统计 津ICP备18009691号
网安备12019202000257

GMT+8, 2022-1-25 07:51 AM

返回顶部