登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 软件工程 > 正文

创建和使用脚本外文翻译资料

 2022-07-21 03:07  

Creating and Using Scripts

The behavior of GameObjects is controlled by the Components that are attached to them. Although Unityrsquo;s built-in Components can be very versatile, you will soon find you need to go beyond what they can provide to implement your own gameplay features. Unity allows you to create your own Components using scripts. These allow you to trigger game events, modify Component properties over time and respond to user input in any way you like.

Unity supports two programming languages natively:

bull;C# (pronounced C-sharp), an industry-standard language similar to Java or C ;

bull;UnityScript, a language designed specifically for use with Unity and modelled after JavaScript;

In addition to these, many other .NET languages can be used with Unity if they can compile a compatible DLL - see here for further details.

Learning the art of programming and the use of these particular languages is beyond the scope of this introduction. However, there are many books, tutorials and other resources for learning how to program with Unity. See the Learning section of our website for further details.

Creating Scripts

Unlike most other assets, scripts are usually created within Unity directly. You can create a new script from the Create menu at the top left of the Project panel or by selecting Assets gt; Create gt; C# Script (or JavaScript) from the main menu.

The new script will be created in whichever folder you have selected in the Project panel. The new script filersquo;s name will be selected, prompting you to enter a new name.

It is a good idea to enter the name of the new script at this point rather than editing it later. The name that you enter will be used to create the initial text inside the file, as described below.

Anatomy of a Script file

When you double-click a script asset in Unity, it will be opened in a text editor. By default, Unity will use MonoDevelop, but you can select any editor you like from the External Tools panel in Unityrsquo;s preferences.

The initial contents of the file will look something like this:

using UnityEngine;

using System.Collections;

public class MainPlayer : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

}

A script makes its connection with the internal workings of Unity by implementing a class which derives from the built-in class called MonoBehaviour. You can think of a class as a kind of blueprint for creating a new Component type that can be attached to GameObjects. Each time you attach a script component to a GameObject, it creates a new instance of the object defined by the blueprint. The name of the class is taken from the name you supplied when the file was created. The class name and file name must be the same to enable the script component to be attached to a GameObject.

The main things to note, however, are the two functions defined inside the class. The Update function is the place to put code that will handle the frame update for the GameObject. This might include movement, triggering actions and responding to user input, basically anything that needs to be handled over time during gameplay. To enable the Update function to do its work, it is often useful to be able to set up variables, read preferences and make connections with other GameObjects before any game action takes place. The Start function will be called by Unity before gameplay begins (ie, before the Update function is called for the first time) and is an ideal place to do any initialization.

Note to experienced programmers: you may be surprised that initialization of an object is not done using a constructor function. This is because the construction of objects is handled by the editor and does not take place at the start of gameplay as you might expect. If you attempt to define a constructor for a script component, it will interfere with the normal operation of Unity and can cause major problems with the project.

A UnityScript script works a bit differently to C# script:

#pragma strict

function Start () {

}

function Update () {

}

Here, the Start and Update functions have the same meaning but the class is not explicitly declared. The script itself is assumed to define the class; it will implicitly derive from MonoBehaviour and take its name from the filename of the script asset.

Controlling a GameObject

As noted above, a script only defines a blueprint for a Component and so none of its code will be activated until an instance of the script is attached to a GameObject. You can attach a script by dragging the script asset to a GameObject in the hierarchy panel or to the inspector of the GameObject that is currently selected. There is also a Scripts submenu on the Component menu which will contain all the scripts available in the project, including those you have created yourself. The script instance looks much like any other Component in the Inspector:

Once attached, the script will start working when you press Play and run the game. You can check this by adding the following code in the Start function:-

// Use this for initialization

void Start () {

Debug.Log('I am alive!');

}

Debug.Log is a simple command that just prints a message to Unityrsquo;s console output. If you press Play now, you should see the message at the bottom of the main Unity editor window and in the Console window (menu: Window gt; Console).

Controlling GameObjects Using Components

In the Unity editor, you make changes to Component properties using the Inspector. So, for example, changes to the position values of the Transform Component will result in a change to the GameObjectrsquo;s position. Similarly,

全文共31692字,剩余内容已隐藏,支付完成后下载完整资料


创建和使用脚本

GameObjects的行为是由附加到他们的组件进行控制。虽然统一的内置组件可以非常灵活,你很快就会发现,你需要超越他们可以提供实现自己的游戏功能。团结允许您使用脚本来创建自己的组件。这允许你触发游戏事件,随着时间的推移修改组件属性,并在任何你喜欢的方式来响应用户输入。

团结支持两种编程语言本身:

bull;C#(发音C-锐利),一个行业标准语言类似于Java或C ;

bull;UnityScript,专门为统一设计的,模仿的JavaScript后的语言;

除了这些,许多其他.NET语言可以与Unity一起使用,如果他们能编兼容的DLL - 在这里看到进一步的细节。

学习编程的艺术和使用这些特定的语言超出了本介绍的范围。不过,也有许多书籍,教程和其他资源,学习如何使用Unity编程。请参阅我们的网站了解更多详情学习区间。

创建脚本

与大多数其他资产,脚本通常是内部的团结创造直接。您可以在项目面板的左上角或选择资产从主菜单中创建从创建菜单中一个新的脚本gt;新建gt; C#脚本(或JavaScript)。

新的脚本将在哪个文件夹中的项目面板中选择创建。新的脚本文件的名称将被选中,提示您输入新的名称。

正是在这一点上输入新脚本的名称,而不是后期编辑是个好主意。您输入的名称将用于创建文件中的初始文本,如下所述。

一个脚本文件剖析

在Unity当你双击一个脚本资产,它将在一个文本编辑器打开。默认情况下,统一将使用MonoDevelop的,但你可以选择你从团结的喜好外部工具面板喜欢的编辑器。

该文件的初始内容会是这个样子:

using UnityEngine;

using System.Collections;

public class MainPlayer:MonoBehaviour {

//使用这个初始化

void start(){

}

//更新是每帧调用一次

void Update () {

}

}

脚本使得其通过实现从所谓MonoBehaviour内置类派生的类统一内部工作连接。你可以把一个类作为一种蓝图,创建一个可以连接到GameObjects一个新的组件类型。每次添加一个脚本组件到一个游戏物体时,它会创建蓝图定义的对象的新实例。类的名称是从创建文件时,您提供的名义采取。类名和文件名必须是相同的,以使脚本组件附加到一个游戏物体。

要注意的主要的事情,但是,是类内所定义的两种功能。更新功能是把代码将处理的游戏对象帧更新的地方。这可能包括运动,触发动作和响应用户输入,基本上任何一个需要在游戏中随着时间的推移处理。要启用更新功能来完成其工作,它往往是能够成立的变量,阅读偏好和任何游戏动作发生之前做出与其他GameObjects连接非常有用。启动功能将被统一的游戏开始之前被调用(即更新函数被调用之前的第一次),并做初始化的理想场所。

注意经验的程序员:你可能会惊讶,一个对象的初始化没有使用构造函数来完成。这是因为对象的构造是由编辑处理,并在游戏的一开始就不会发生,你可能期望。如果试图定义脚本部件的构造,将与统一的正常运行,并可能导致与该项目的主要问题。

一个UnityScript脚本作品有点不同,以C#脚本:

#pragma strict

function Start () {

}

function Update (){

}

在这里,开始和更新功能具有相同的含义,但类没有显式声明。脚本本身被假设为定义类;它会隐式地从MonoBehaviour派生并从脚本资产的文件名取的名字。

控制游戏物体

如上所述,脚本只定义一个蓝图组件,因此没有它的代码将被激活,直到脚本的一个实例是连接到一个游戏物体。您可以通过在层次结构面板或当前选择的游戏对象的检查脚本资产拖动到游戏物体连接的脚本。也有组件菜单,其中将包含所有项目中可用的脚本,包括您自己创建的一个脚本子菜单。该脚本实例,看起来很像在检查任何其他组件:

一旦连接,该脚本将启动按下播放和运行游戏的工作。您可以通过添加在开始功能下面的代码检查: -

//使用这个初始化

void start(){

debug.log(“I am alive!”);

}

是的debug.log一个简单的命令,只是打印一条消息,以统一的控制台输出。如果按Play现在,你应该可以看到消息在主团结编辑器窗口的底部,并在控制台窗口(菜单:窗口gt;控制台)。

控制GameObjects使用组件

在Unity编辑器中,您更改使用检查组件属性。因此,例如,改变为变换组件的位置值将导致改变游戏对象的位置。同样,您可以更改渲染的材料的颜色或刚体的质量与对游戏物体的外观或行为产生相应的影响。在大多数情况下,脚本也有关修改组件属性来操纵GameObjects。的差异,不过,是一个脚本可以随时间逐渐或响应于来自用户的输入而改变的属性的值。通过改变,创建和销毁在正确的时间对象,任何一种玩法都可以实现。

访问组件

最简单和最常见的情况是,其中一个脚本需要访问连接到同一游戏对象的其他组件。正如引言部分中提到,组件实际上是一个类的实例,所以第一步是要得到你想要使用的组件实例的引用。这是用GetComponent函数来完成。通常情况下,要对组件对象赋值给一个变量,这是在C#中使用以下语法来完成:

void Start () {

Rigidbody rb = GetComponentlt;Rigidbodygt;();

}

在UnityScript,语法是微妙的不同:

function Start () {

var rb = GetComponent.lt;Rigidbodygt;();

}

一旦你有一个组件实例的引用,可以多设置其属性的值,你会在Inspector:

Void start(){

Rigidbody rb = GetComponentlt;Rigidbodygt;();

//改变对象的刚体的质量。

rb.mass = 10F;

}

一个额外的功能,是不是在检查可用呼吁组件实例功能的可能性:

void start(){

Rigidbody rb = GetComponentlt;Rigidbodygt;();

//力添加到刚体。

rb.AddForce(Vector3.up * 10f);}

还要注意,没有任何理由,你为什么不能连接到同一个对象的多个自定义脚本。如果你需要从另一个访问一个脚本,您可以使用GetComponent像往常一样,只是使用的脚本类的名称(或文件名)指定所需的组件类型。

如果你试图找回实际上并没有被添加到游戏对象,然后GetComponent将返回null组件;如果你试图改变一个空对象的任何值,你会得到在运行时一个空引用错误。

访问其他对象

尽管他们有时在隔离操作,它是常见的脚本来跟踪的其他对象。例如,一个追求敌人可能需要知道的运动员的位置。统一提供了许多不同的方式来检索其它的目的,每一个适合于某些情况。

链接与变量的对象

找一个相关的游戏对象的最直接的办法就是公开游戏物体变量添加到脚本:

public class Enemy : MonoBehaviour {

public GameObject player;

//其它变量和函数...

}

这个变量将在Inspector像任何其他可见:

现在,您可以拖动从场景或层次面板对象到这个变量分配给它。该GetComponent功能和组件访问变量可用于该对象与任何其他,这样你就可以像下面这样使用代码:

public class Enemy : MonoBehaviour {

public GameObject player;

void Start() {

//开始敌方玩家角色背后的10个单位。

transform.position = player.transform.position - Vector3.forward * 10F;

}

}

此外,如果在你的脚本声明一个组件类型的公共变量,可以将具有该组件连接到任何游戏物体。这将访问组件,而不是直接的游戏对象本身。

公共变换playerTransform;

与变量链接的对象在一起的时候,你正在处理的有永久连接各个对象是最有用的。可以使用一个数组变量连结的多个相同类型的对象,但是连接必须仍然在Unity编辑,而不是在运行时进行。常常是方便在运行时定位对象和统一提供了两种基本的方法来做到这一点,如下面所述。

查找子对象

有时,游戏场景会利用许多相同类型的对象,如敌人,航点和障碍的。这些可能需要通过监督或反应以它们(例如,所有路点可能需要提供给一个寻路脚本)的特定脚本进行跟踪。使用变量将这些对象链接是一个可能性,但它使设计过程繁琐,如果每一个新的航点已被拖到一个脚本变量。同样,如果一个航路点被删除,那么这是一个滋扰必须卸下变量引用缺少的对象。在这样的情况下,它往往是更好地使它们一个父对象的所有子来管理一组对象。子对象可以通过转换组件的父的(因为所有GameObjects隐含有一个变换)来retreived:

using UnityEngine;

public class WaypointManager:MonoBehaviour {

public Transform[] waypoints;

void Start() {

waypoints = new Transform[transform.childCount];

int i = 0;

foreach (Transform t in transform) {

waypoints[i ] = t;

}

}

}

您还可以通过名称使用Transform.Find功能查找特定的子对象:

transform.Find(“枪”);

当一个对象具有可以添加和玩游戏期间除去的孩子这可能是有用的。可以拿起和放下武器就是一个很好的例子。

按名称或标签查找对象

它总是可以在场景层次,只要你有一些信息,以确定他们在任何地方找到GameObjects。单个对象可以通过名称使用GameObject.Find功能进行检索:

GameObject player;

Void start(){

player= GameObject.Find(“MainHeroCharacter”);

}

一个或多个对象的集合还可以位于通过使用GameObject.FindWithTag和GameObject.FindGameObjectsWithTag功能及其标记: -

GameObject player;

GameObject[] enemies;

Void start(){

player = GameObject.FindWithTag(“Player”);

enemies= GameObject.FindGameObjectsWithTag(“Enemy”);

}

事件功能

在Unity的脚本是不喜欢那里的代码将继续运行在一个循环,直到其完成任务的程序的传统观念。相反,统一间歇通过调用都在其内声明某些功能将控制传递给脚本。一旦函数执行完毕,控制被传递回统一。这些功能被称为事件的功能,因为它们被统一响应于游戏中发生的事件被激活。统一采用的命名方案,以确定哪个函数调用特定事件。例如,你应该已经看到了更新功能(一帧更新之前调用)和启动功能(只是对象的第一帧更新之前调用)。还有更多的活动功能都统一提供;完整的列表可以在与他们使用的详细沿MonoBehaviour类脚本参考页中找到。以下是一些最常见的重要事件。

定期更新活动

一个游戏很像其中动画帧实时生成的动画。在游戏编程中的一个关键概念是,在每一帧渲染之前的比赛进行更改的位置,状态和对象的行为。该更新功能对于这种代码在Unity的主要场所。之前被渲染的帧计算动画之前也更新被调用。

void Update(){

float distance = speed * Time.deltaTime * Input.GetAxis('Horizontal');

transform.Translate(Vector3.right * distance);

}

物理引擎还更新在以类似的方式向所述帧渲染离散时间步。所谓FixedUpdate一个单独的事件函数只是每次物理更新之前调用。由于物理更新和帧更新不会以相同的频率出现,你会

全文共12652字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[154917],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图