Unity3D基础教程2-2:使用组件(Using Components)

2014-08-06 07:45:14|?次阅读|上传:huigezrx【已有?条评论】发表评论

关键词:Unity3D, 游戏, 虚拟现实|来源:唯设编程网

Testing out Properties 测试属性

While your game is in Play Mode, you are free to change properties in any GameObject's Inspector. For example, you might want to experiment with different heights of jumping. If you create a Jump Height property in a script, you can enter Play Mode, change the value, and press the jump button to see what happens. Then without exiting Play Mode you can change it again and see the results within seconds. When you exit Play Mode, your properties will revert to their pre-Play Mode values, so you don't lose any work. This workflow gives you incredible power to experiment, adjust, and refine your gameplay without investing a lot of time in iteration cycles. Try it out with any property in Play Mode. We think you'll be impressed.

当你的游戏是播放模式时,在任意GameObject的检视器里自由的修改属性。例如,你也许想去实验不同的高度跳跃。如果你创建一个Jump Heigh属性在脚本里,你可以进入播放模式,修改此值,然后按跳跃键去看发生了什么。不用退出播放模式,你可以再次修改它并看结果在几秒钟内。当你退出播放模式,你的属性将恢复它们播放前的值,因此你就不会丢失任何工作。工作流给你难以置信的能力去实验、调整和完善你的游戏可玩性不用投入大量的时间反复循环操作。在播放模式利用任意属性实验它。我们认为你将会留下深刻印象。

Removing Components 删除组件

If you want to remove a Component, option- or right-click on its header in the Inspector, and choose Remove Component. Or you can left-click the options icon next to the ? on the Component header. All the property values will be lost and this cannot be undone, so be completely sure you want to remove the Component before you do.

如果你想删除一个组件,在它的头上点鼠标右键单击在检视器里然后选择Remove Component或者,你可以在?号旁边的选项图标上点鼠标左键在组件头上。所有属性值将被丢失,此项操作不能恢复(回退),因此,在你做之前,要十分确定你想删除组件。

The Component-Script Relationship 组件-脚本关系

Although Scripts seem very different from Components at first, the truth is that a script is a type of Component. Basically, it is a Component that you are creating yourself. You will define its members to be exposed in the Inspector, and it will execute whatever functionality you've written.


Scripting Components 脚本组件

There are many Components that can be directly accessed in any script. For example, if you want to access the Translate function of the Transform component, you can just write transform.Translate() or gameObject.transform.Translate(). This works because all scripts are attached to a GameObject. So when you write transform you are implicitly accessing the Transform Component of the GameObject that is being scripted. To be explicit, you write gameObject.transform. There is no advantage in one method over the other, it's all a matter of preference for the scripter.

有许多组件可以被直接在任何脚本里访问。例如,如果你想去访问Transform 组件的Translate函数,你可以仅书写transform.Translate()gameObject.transform.Translate()。这样写因为所有的脚本是连接到一个GameObject。因此,当你写transform时,你是暗地里连接存在的脚本GameObject 的Transform组件。明确的表达式,你写gameObject.transform有没有一个比其它更好的方法,这一些都是脚本偏好问题。

To see a list of all the Components you can access implicitly, take a look at the GameObject page in the Scripting Reference.

查看所有你可以暗地里访问组件的列表, 在脚本引用里查看GameObject页。

Using GetComponent() 使用GetComponent()

There are many Components which are not enabled as members of the GameObject class. So you cannot access them implicitly, you have to access them explicitly. You do this by calling the GetComponent("component name") and storing a reference to the result. This is most common when you want to make a reference to another script attached to the GameObject.

有一些组件不作为GameObject类的成员。因此,你不能暗地里访问它们,你必须明确的访问它们。你可以通过调用GetComponent("component name")并存储一个引用返回值。这是最常见的当你想进行一个引用其它脚本去连接GameObject时。

Pretend you are writing Script B and you want to make a reference to Script A, which is attached to the same GameObject. You would have to use GetComponent() to make this reference. In Script B, you would simply write: scriptA = GetComponent("ScriptA");

假定你正在编写Script B你想要进行一个引用Script A,它是连接同样的GameObject。你应当必须使用GetComponent()进行这个引用。在Script B里,你应当简单书写:scriptA = GetComponent("ScriptA");

Then you would be able access any of the Script A variables by writing scriptA.variableName within Script B. For more help with using GetComponent(), take a look at the GetComponent() Script Reference page.

然后你将能访问任何Script A的变量,通过书写scriptA.variableName Script B里。更过使用GetComponent()帮组,查看GetComponent() 脚本引用页。

发表评论0条 】
Unity3D基础教程2-2:使用组件(Using Components)