unity3d中高级手册01基本3-7使用脚本_第1页
unity3d中高级手册01基本3-7使用脚本_第2页
unity3d中高级手册01基本3-7使用脚本_第3页
已阅读5页,还剩2页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、UsingScripts Unity Manual User Guide Working ssetsUsingUnity 手册- 用户指南 -与资产合作-使ThisisaroductiononhowtocreateandutilizescriptsinaUsingScripts Unity Manual User Guide Working ssetsUsingUnity 手册- 用户指南 -与资产合作-使Thisisaroductiononhowtocreateandutilizescriptsinaproject.FordetailedinformationaboutScripting A

2、PI, please view the Scripting Reference. For detailed information scripting,pleaseviewtheCreatingGameplaypageofthisreatinggameplayAPI 的详细信息,请查这是一个小的介绍如何在一个项目里建立和使。关玩家的详细信息,请查看本手册的建Scripting inside Unity is done by writing ehaviourscriptsinJavaScript,BooorC#.Youcanuseoneall scripting languages in a s

3、ingle project, there isno penalty for using none.Unlesss edallthescriptinglesareinUnity JavaScript, Boo 或 C#之内。你可以使用一个或所有简本的完成通过编写简单行例子是 JavaScript 的言在一个独立的项目中,使用多于一个没有损失。除非另有说明,所有Creatingnew scripts UnlikeotherassetslikeMeshesorTextures,ScriptfilescanbecreatedfromwithinUnity.Tocreateascript, open t

4、he Assets-Create-JavaScript (or Assets-Create-C Sharp Script or Assets-Create-Script)fromtheenu.ThiswillcreateanewscriptcalledNewBehaviourScriptandplaceselectedfolderin ProjectView.IfnofolderectedinProjectView,thescriptwillbecreatedatthe和其它资产不同,像网格或纹理文件可以 被建立。,从主菜单打Assets-Create-JavaScript(或 Assets-

5、Create-C Sharp Scrip 或 Assets-Create-Boo Script)建立一个称为 NewBehaviourScript 并放置它在项目视图里选择的文件夹里。如果在项目视图里没有文件选取将被建立在根Youcaneditthescriptbydouble-clickingonheProjectView.Thiswillitron,thescripteditorUnity.YoudoallyourscriptinginanexternaltexteditorlkeUnitron,andnotinYoucaneditthescriptbydouble-clickingonh

6、eProjectView.Thiswillitron,thescripteditorUnity.YoudoallyourscriptinginanexternaltexteditorlkeUnitron,andnotinUnitydirectly.Tosetthescript editor, change the drop-down item in Unity-External Script 你可以在项目视图里通过上双击编辑它。这将启动 Unitron,Unity 编辑器。在外部文本编辑器里理所有像 Unitron 一样,但不如在 Unity 里直接。设置默认编辑器,Unity-Externa

7、l Script editor 里(windows 上是 Eidt-External editor)These are the contents of a new, empty behaviour script: 这些是新的内容:Anew,emptyscriptdoesnotdoalotonitsown,soletsaddsomefunctionality.Changethescripttoread一个新的,空When executed, this code will o Worldto theconsole.But there is tcausesthecodetoexecutedyet.W

8、ehavetoattachthescripttoanactive heScenebeforeitwillbe当执行,这个代码将打印World到一个活动的 GameObjectAttachingscriptstoobjects Save theabove script and create a newThis will create a new GameObject called he Scene by selecting GameObject-Create Other-hecurrent保存上面并建立一个新的对象在场景里通过选取 GameObject-Other-Cube。这将建立一个的物体称

9、为Cube”在当前场景里functionUpdate()pr o functionUpdate()NowdragthescriptfromtheProjectViewtotheCubeheSceneorHierarchyView,itdoesntNowdragthescriptfromtheProjectViewtotheCubeheSceneorHierarchyView,itdoesntmatter).csoselecttheCubeandchooseComponent-Scripts-New Behaviour Script.Eitherofthesewillattachthescrip

10、ttotheCube.Everyscriptyoucreatewillhe Component-. Ifhavechanged the name of your script, you tname现在从项目视图里拖到这个立方体(Cube)(在场景里或层次视图里,这无关紧要)。你也可以选取这个 Cube 并选择 Component-Scripts-New Behaviour ScriptCube建立的每将显示在 Component-Scripts 菜单里。如果你已经修改了的名称,你将代替名称IfyouselecttheCubeandlookattheInspector,youwillbeen t

11、thescriptisnowvisible.Thismeanss如果你选取里 Cube 是现在可见。这意味着它已经被附加了PressPlaytotestyourcreation.Youshouldseethetextbuttons.Exitplaymodewhenyouseeo World appear beside the 按按钮去测试你的创造。你应该看到PressPlaytotestyourcreation.Youshouldseethetextbuttons.Exitplaymodewhenyouseeo World appear beside the 按按钮去测试你的创造。你应该看到o

12、 World文本显Play/Pause/Step 按钮旁边 (2.5 版的里,显示在底部状态栏)模式Manipulatingtheobject A()s ementcanbeveryhandywhendebuggingyourscript,butitdoesnotmanipulatetheitisattachedto.Letschangethescripttoaddsome一个 pr ()语句可以非常便利的当调试你时,但它不能操作被附属于的 物体)。让If youre new to scripting, its okay if this looks confusing. These are t

13、he important concepts to functionUpdate()isacontainerfortUnityexecutesmultipletimespersecondper frame). function Update () 是 Unity 多个时间每秒钟执行代码的容器(一次每帧)transform is a reference to the GameObjects Transform Component. Transform 是一个对象物。e() is a function器he Transform Component. e()是在变换组件里的功functionUpdat

14、e()e(0, ime, Thenumbersin-nthecommasrepresentthedegreesofro ionaroundeachaxisofspaceXYandZ. 在逗号之间的数字表示环绕每个3D空间空间轴:x,yz 的旋转的角度imeisamemberoftheTimet evens out movement over cond, so cubewillro eatthesamespeedThenumbersin-nthecommasrepresentthedegreesofro ionaroundeachaxisofspaceXYandZ. 在逗号之间的数字表示环绕每个

15、3D空间空间轴:x,yz 的旋转的角度imeisamemberoftheTimet evens out movement over cond, so cubewillro eatthesamespeednomatterhowmanyframespersecondyourmachineisTherefore,5*imemeans5degreesperime 是 Time 类的一个成员,使每秒移动平坦,因此这个Cube 器的每秒的多少帧被绘制。所以,5 * ime 意味着5 度。llind,wecanreadthiscodeaseveryframe,ro ethisGameObjectsTrans

16、formcomponentsmall amount titwillequalfivedegreesaroundtheYaxiseach可以阅读这个代码作为“每一个帧,旋转这个 GameObject 的变换组件一个小的数量因此所有在这一点将等5度环YYoucanslotsofdifferentComponentsthesamewayaswesed transform already. You have addComponentstotheGameObjectusingthe.AlltheComponentsyoucaneasilyarelistedunderVariablesontheGameO

17、bjectScriptingReference了 transform 的一样方法。你必须添加组件已GameObject 被在 For more information about the relationship n GameObjects, Scripts, and Components, please ahead to the GameObjects page or Using Components page of this GameObjects, Scripts Components Using Components 信息,请跳到前面的 GameObjects 页或本手册erofVar

18、iables Ourscriptsofarwillalwaysro etheCube5degreeseachsecond.Wemightwantittoro eanumberofdegreespersecond.Wecouldchangethenumberandsave,butthenwehavetowaitforthetoledandwehavetolaymodebeforeweseetheresults.ThereisamuchfasterwayWecanexperimentwiththespeedofro ioninreal-timeduringPlaymode,anditseasyto

19、的到目前为止将总每秒选择 Cube 5 度也许想要它每秒选择不同数量的角度可以改变数据并保存它,须等被从新编译以须进模式看到结果之前。有一个非常快速方法去做它。在实模式过程中Insteadofothee() function, we will declare a speed variable and theChangethescripttothefollowingcodeandsave替代在e()函数输入 定义一个 变量并在函数里使用它。修Now select the Cube and look at the Inspector. Notice how our speed variable 现

20、在,选取这个 Cube 的 speed 变量如何显示Now select the Cube and look at the Inspector. Notice how our speed variable 现在,选取这个 Cube 的 speed 变量如何显示ThisvariablecannowbemodifiedheInspector,justlikerenamingahe file explorer. Select press Return and change the value. You soright-oroption-clickonthevalueanddragthemouseup

21、down.Youcanchangethevariableatanytime,evenwhilethegameis并修改它的值。你也可以恰当的点击选项并左右拖动鼠标(修改值。你可以在任何时间更改变量,甚至当在HitPlayandtrymodifyingthespeedvalue.TheCubesonspeedwillchangeinstantly.WhenyouPlaymode,youlltyourchangesarerevertedbacktotheirvaluebeforeenteringPlaymode.Thisyoucanplay,adjust,andexperimenttofindthebestvalue,thentvalue击按钮并尝试修改 speed 变量的值。这个 Cube 的旋转速度的更改回复到进模式之前的它们值。这个方法你可、调整以及实验去找打最优的值,然后应用不varspeed =functionUpdate()e(0, ime, Using this method of changing a variables he Inspectortyoucanre-useonescriptonobjects, each different v

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论