Unity3D说明文档翻译-Using Components

本文介绍了游戏开发中组件的重要性和使用方法。详细解释了组件与游戏对象的关系,并演示了如何添加、编辑和删除组件。此外,还介绍了组件的灵活性及其在游戏开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Using Components

使用组件

Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObject. If you don’t yet understand the relationship between Components and GameObjects, read the GameObjects page before going any further.

组件是对象的螺帽和螺钉和游戏的表现者.它们是每个游戏对象的功能模块.如果你还不明白组件和游戏对象间的关系,在你做进一步操作前,先阅读游戏对象.

A GameObject is a container for many different Components. By default, all GameObjects automatically have a Transform Component. This is because the Transform dictates where the GameObject is located, and how it is rotated and scaled. Without a Transform Component, the GameObject wouldn’t have a location in the world. Try creating an empty GameObject now as an example. Click the GameObject->Create Empty menu item. Select the new GameObject, and look at the Inspector.

一个游戏对象是许多不同组件的一个容器.默认,所有游戏对象自动包含一个转换组件.这是因为转换组件规定了游戏物体在哪放置,如何旋转和缩放.没有转换组件,游戏对象将在世界内没有位置.现在尝试创建一个空游戏对象作为例子.点击菜单游戏对象>创建空对象选项.选定新游戏对象,查看检视面板.

Even empty GameObjects have a Transform Component

即使空游戏对象也有一个转换组件

 

Remember that you can always use the Inspector to see which Components are attached to the selected GameObject. As Components are added and removed, the Inspector will always show you which ones are currently attached. You will use the Inspector to change all the properties of any Component (including scripts).

记住,你可以总是使用检视面板查看哪些组件被附加到所选游戏对象上.当组件被添加和移除,检视面板总是显示哪些是当前附加着的.你可使用检视面板来改变任何组件(包括脚本)的所以属性.

Adding Components

添加组件

You can add Components to the selected GameObject through the Components menu. We’ll try this now by adding a Rigidbody to the empty GameObject we just created. Select it and chooseComponent->Physics->Rigidbody from the menu. When you do, you will see the Rigidbody’s properties appear in the Inspector. If you press Play while the empty GameObject is still selected, you might get a little surprise. Try it and notice how the Rigidbody has added functionality to the otherwise empty GameObject. (The Y position of the GameObject’s transform starts to decrease. This is because the physics engine in Unity is causing the GameObject to fall under gravity.)

你可以通过组件菜单添加组件到所选游戏对象上.你可以尝试添加一个刚体到我们刚创建的空游戏对象上.选定它并在菜单上选择组件>物理>刚体.当你这样做时,你将看到刚体的属性显现在检视面板上.如果你按下播放,你将获得一个小惊喜,虽然空游戏对象依然被选定.尝试它并注意刚体添加了怎样的功能到空对象上.(游戏对象的转换组件Y位置开始减少.这是因为Unity上的物理引擎通过重力引起游戏对象降落.)

An empty GameObject with a Rigidbody Component attached

一个空游戏物体附加了一个刚体组件

 

Another option is to use the Component Browser, which can be activated with the Add Component button in the object’s inspector.

另一个选择是使用组件浏览器,这可以通过在对象的检视面板上点击添加组件按钮激活.

The Component Browser

组件浏览器

 

The browser lets you navigate the components conveniently by category and also has a search box that you can use to locate components by name.

浏览器通过分类让你方便的导航组件且还有一个搜索框,你可以用它通过名称定位组件.

You can attach any number or combination of Components to a single GameObject. Some Components work best in combination with others. For example, the Rigidbody works with any Collider. The Rigidbody controls the Transform through the NVIDIA PhysX physics engine, and the Collider allows the Rigidbody to collide and interact with other Colliders.

你可以附加任意数量或任意组合组件到单独的游戏对象上.一些组件最好和另一些组合工作.例如,刚体与任何碰撞检测一起工作.刚体通过NVDIA PhysX(英伟达物理运算引擎)物理运算引擎控制转换组件,而碰撞检测允许刚体碰撞且和其他碰撞器相互作用.

If you want to know more about using a particular Component, you can read about any of them in the relevant Component Reference page. You can also access the reference page for a Component from Unity by clicking on the small ? on the Component’s header in the Inspector.

如果你想知道更多使用特定组件信息,你可以在相应组件参考页面阅读它们.你也可以在Unity中通过点击检视面板上组件头上的小问号进入参考页面.

Editing Components

编辑组件

One of the great aspects of Components is flexibility. When you attach a Component to a GameObject, there are different values or Properties in the Component that can be adjusted in the editor while building a game, or by scripts when running the game. There are two main types of Properties: Values and References.

组件的一个主要方面就是方便.当你附加一个组件到游戏对象,将有不同的值或属性在组件上,创建中的游戏可通过编辑器调整,而当运行游戏时通过脚本调整.有两种主要属性类型:值和引用.

Look at the image below. It is an empty GameObject with an Audio Source Component. All the values of the Audio Source in the Inspector are the default values.

查看下图.这是一个带有一个声音组件的空游戏对象.此声音在检视面板上所有的值都是默认值.

This Component contains a single Reference property, and seven Value properties. Audio Clip is the Reference property. When this Audio Source begins playing, it will attempt to play the audio file that is referenced in the Audio Clip property. If no reference is made, an error will occur because there is no audio to be played. You must reference the file within the Inspector. This is as easy as dragging an audio file from the Project View onto the Reference Property or using the Object Selector.

此组件包含一个单独引用属性和七个值属性.音频剪辑是引用属性.但这个声音组件开始播放时,它将试图播放的是声音剪辑属性引用的声音资源.如果没有引用,将发生一个错误,因为没有声音来播放.你不想使用检视面板引用文件.很容易从项目视图拖拽一个声音文件到引用属性上或使用对象选择器.

Now a sound effect file is referenced in the Audio Clip property

现在一个音响效果文件被引用到音频剪辑属性上.

 

Components can include references to any other type of Component, GameObjects, or Assets. You can read more about assigning references on the page about Editing Properties.

组件可以包含引用任何其他类型组件,游戏对象或资源.你可以在编辑属性页面阅读更多关于指定引用的信息.

The remaining properties on the Audio Clip are all Value properties. These can be adjusted directly in the Inspector. The Value properties on the Audio Clip are all toggles, numeric values, drop-down fields, but value properties can also be text strings, colors, curves, and other types. You can read more about these and about editing value properties on the page about editing value properties.

音频剪辑上的剩余属性都是值属性.它们可以直接在检视面板上调整.在音频剪辑上的值属性都是开关,数值,下拉列表.但值属性也可以是文本,颜色,曲线和其他类型.你可以在编辑值属性页面阅读更多关于这些及编辑值属性的信息.

Component Context Menu commands

组件上下文菜单命令

The context menu for a component has a number of useful commands.

组件的快捷菜单有许多实用命令

The Component Context Menu

组件快捷菜单

 

The same commands are also available from the “gear” icon in the extreme top-right of the component’s panel in the inspector.

相同命令也可以在检视面板上通过组件的面板右上角的齿轮图标找到.

Reset

重置

This command restores the values the component’s properties had before the most recent editing session.

此命令恢复组件属性的值到最近编辑会话上.

Remove

移除

Remove Component command is available for cases where you no longer need the component attached to the GameObject. Note that there are some combinations of components that depend on each other (eg, Hinge Joint only works when a Rigidbody is also attached); you will see a warning message if you try to remove components that others depend on.

移除组件命名适用于你不再需要此组件附加到此对象上的情况下.注意,一些组件的组合互相依赖(例如,枢纽关节仅在刚体也被添加时工作),如果你尝试移除被其他组件依赖的组件会收到一条警告信息.

Move Up/down

向上/下移动

The Image Effects components each apply a specific visual effect to the rendered view, but the order in which the effects are applied is important. The context menu has Move Up and Move Down

图片效果组件每个都应用一个特定视觉效果来渲染视图,但效果应用的顺序非常重要.快捷菜单上有向上移动和向下移动.

Copy/Paste

拷贝/黏贴

The Copy Component command stores the type and current property settings of a component. These can then be pasted into another component of the same type with Paste Component Values. You can also create a new component with the copied values on an object by using Paste Component As New

组件拷贝命令存储一个组件输入和当前属性设置.然后通过黏贴组件值命令被黏贴到另一个同类型组件上.你也可以创建一个新组件通过在拷贝一个对象再对新组件使用黏贴组件命令.

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.

即使你的游戏处在播放模式,你也可以在任何对象的检视面板上改变属性.例如,你可能想实验不同跳跃高度.如果你在脚本中创建一个跳跃高度属性,你可以进入播放模式,改变属性值,并按跳跃按钮来查看会发生什么.不需要退出播放模式你就可以再次改变它,并再次查看结果.当你退出播放模式,属性将恢复到它们播放前的值,所以你不会丢失任何工作.此工作流程给你难以置信的力量来实验,调整和改善你的游戏不需要投入大量时间到迭代周期.尝试在播放模式测试任何属性.我们想你会被打动的.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值