首页 Unity3D常用脚本

Unity3D常用脚本

举报
开通vip

Unity3D常用脚本如果您需要使用本文档,请点击下载按钮下载!如果您需要使用本文档,请点击下载按钮下载!如果您需要使用本文档,请点击下载按钮下载!场景转换:varscreenbutton:boolean;functionStart(){DontDestroyOnLoad(this);screenbutton=true;}functionUpdate(){if(Input.GetKeyDown(KeyCode.Escape)){screenbutton=true;}}functionOnGUI(){if(screenbutton==tr...

Unity3D常用脚本
如果您需要使用本文档,请点击下载按钮下载!如果您需要使用本文档,请点击下载按钮下载!如果您需要使用本文档,请点击下载按钮下载!场景转换:varscreenbutton:boolean;functionStart(){DontDestroyOnLoad(this);screenbutton=true;}functionUpdate(){if(Input.GetKeyDown(KeyCode.Escape)){screenbutton=true;}}functionOnGUI(){if(screenbutton==true)if(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-190,60,50),"Scene1")){print("YouclickScene1");Application.LoadLevel(1);screenbutton=false;}elseif(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-130,60,50),"Scene2")){print("YouclickScene2");Application.LoadLevel(2);screenbutton=false;}elseif(GUI.Button(Rect(Screen.width/2-375,Screen.height/2-0,50,50),"Quit")){print("YouclickQuit");Application.Quit();}}心得:场景转换时,这个程序贴在按钮界面场景中。在BuildSetting(导出)(在File下)菜单中加入所需场景。0为按钮界面。可以无限增加场景。}elseif(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-130,60,50),"Scene2")){print("YouclickScene2");Application.LoadLevel(2);screenbutton=false;复制此段程序并做修改。运动控制键:上下左右varTankSpeed:int=20;functionUpdate(){if(Input.GetKey(KeyCode.W)){print("go");transform.Translate(Vector3.forward*Time.deltaTime*TankSpeed);}elseif(Input.GetKey(KeyCode.S)){print("goback");transform.Translate(Vector3.forward*Time.deltaTime*-TankSpeed);}elseif(Input.GetKey(KeyCode.A)){print("turnleft");transform.Rotate(Vector3.up*Time.deltaTime*-TankSpeed);}elseif(Input.GetKey(KeyCode.D)){print("turnright");transform.Rotate(Vector3.up*Time.deltaTime*TankSpeed);}}需要放在第一视角的物体或者摄像机上。加天空背景:菜单栏Component下的Rendering下的Skybox。将他加在所需场景的摄像机上,选择所需的天空。运行时自然出现。各种按钮汇总:鼠标碰触后显示说明文字。functionOnGUI(){GUI.Button(Rect(0,0,100,20),GUIContent("Clickme","Thisisthetooltip"));GUI.Label(Rect(0,40,100,40),GUI.tooltip);}多项选择,以上都选择。varallOptions=true;varextended1=true;varextended2=true;functionOnGUI(){allOptions=GUI.Toggle(Rect(0,120,150,20),allOptions,"EditAllOptions");GUI.enabled=allOptions;extended1=GUI.Toggle(Rect(20,70,130,20),extended1,"ExtendedOption1");extended2=GUI.Toggle(Rect(20,90,130,20),extended2,"ExtendedOption2");GUI.enabled=true;if(GUI.Button(Rect(0,160,150,20),"Ok"));print("userclickedok");}鼠标碰触第一个按钮有解释,鼠标碰触第三个对话框有解释。碰第二个没有解释。functionOnGUI(){GUI.Box(Rect(5,300,110,75),GUIContent("Box","Thisboxhasatooltip"));GUI.Button(Rect(10,230,100,20),"Notooltiphere");GUI.Button(Rect(10,200,100,20),GUIContent("Ihaveatooltip","Thebuttonoverridesthebox"));GUI.Label(Rect(10,260,100,40),GUI.tooltip);}半透明,说明性文字对话框。无选择。functionUpdate(){}functionOnGUI(){GUI.BeginGroup(newRect(Screen.width/2-400,Screen.height/2-300,800,600));GUI.Box(newRect(0,0,800,600),"Thisboxisnowcentered!-hereyouwouldputyourmainmenu");GUI.EndGroup();}滚动条,选择按钮。可以放多个按钮并且节省位置。functionUpdate(){}varscrollPosition=Vector2.zero;functionOnGUI(){scrollPosition=GUI.BeginScrollView(Rect(10,400,100,100),scrollPosition,Rect(0,0,220,200));GUI.Button(Rect(0,0,100,20),"Top-left");GUI.Button(Rect(120,0,100,20),"Top-right");GUI.Button(Rect(0,180,100,20),"Bottom-left");GUI.Button(Rect(120,180,100,20),"Bottom-right");GUI.EndScrollView();}循环的字幕。varletterPause=0.2;//定义每个字出现的时间间隔varsound:AudioClip;//打字机的声音privatevarword;//存储你打字机效果想要输出的字的变量privatevarText:String="MynameisRayYang.Keepitinyourheart!";//你希望打字机效果输出的字functionStart(){word=Text;//把你输出的字先赋值给wordText="";//把你要显示的字先抹除,以便你可以在最初显示的时候显示为空,当然你也可以加上其他字,让他先显示,打字机效果打的字会显示在这个后面yieldWaitForSeconds(2);TypeText();}functionOnGUI(){GUI.Label(Rect(150,15,250,25),"Pleaseanswermyquestion,whoamI?");GUI.Box(Rect(150,40,250,25),Text);}functionTypeText(){for(varletterinword.ToCharArray()){//做一个循环,定义一个变量letter逐次等于word中的每一个字符Text+=letter;//把这些字赋值给Textif(sound)//控制声音,出现一个字,发一个声音audio.PlayOneShot(sound);yieldWaitForSeconds(letterPause);}}functionUpdate(){if(Text==word){Text="";TypeText();}}有打字声音的字幕循环。varscrenbutton:boolean;varicon:Texture;varallOptions=true;varextended1=true;varextended2=true;varletterPause=0.2;//定义每个字出现的时间间隔varsound:AudioClip;//打字机的声音privatevarword;//存储你打字机效果想要输出的字的变量staticvarText:String="Thisiszcxfirstproject!Icandoit.";vardel:boolean=false;varparticle:GameObject;functionStart(){DontDestroyOnLoad(this);screnbutton=true;word=Text;Text="";yieldWaitForSeconds(2);TypeText();}functionUpdate(){if(Input.GetKeyDown(KeyCode.Escape)){screnbutton=true;}if(Text=="Thisiszcxfirstproject!Icandoit."){Text="";TypeText();}if(Input.GetMouseButtonDown(0)){Text="";for(varc:charinInput.inputString){//Backspace-Removethelastcharacterif(c=="\b"){if(guiText.text.Length!=0)guiText.text=guiText.text.Substring(0,guiText.text.Length-1);}//Endofentryelseif(c=="\n"){print("Userenteredhisname:"+guiText.text);}//Normaltextinput-justappendtotheendelse{guiText.text+=c;}}}}functionOnGUI(){if(screnbutton==true){if(GUI.Button(Rect(Screen.width/2-30,Screen.height/2-30,60,50),"scene1")){print("youclickScene1");Application.LoadLevel(1);screnbutton=false;}elseif(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+30,60,50),"scene2")){print("youclickScene2");Application.LoadLevel(2);screnbutton=false;}elseif(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+90,60,50),"scene3")){print("youclickScene3");Application.LoadLevel(3);screnbutton=false;}elseif(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+150,60,50),"Quit")){print("youclickQuit");Application.Quit();}}//GUI.Button(Rect(0,0,100,20),"ClickMe");GUI.Button(Rect(0,0,100,20),GUIContent("Clickme","Thisisthetooltip"));GUI.Label(Rect(0,20,100,40),GUI.tooltip);allOptions=GUI.Toggle(Rect(40,40,150,20),allOptions,"EditAllOptions");GUI.enabled=allOptions;extended1=GUI.Toggle(Rect(60,60,130,20),extended1,"ExtendedOption1");extended2=GUI.Toggle(Rect(60,80,130,20),extended2,"ExtendedOption2");GUI.enabled=true;if(GUI.Button(Rect(40,100,150,20),"Ok"))print("userclickedok");GUI.BeginGroup(newRect(Screen.width/2-400,Screen.height/2-300,800,600));GUI.Box(newRect(0,0,800,600),"Thisboxisnowcentered!-hereyouwouldputyourmainmenu");GUI.EndGroup();GUI.Label(Rect(250,75,250,25),"Pleaseintroduceyourobjec!");GUI.Box(Rect(250,100,250,25),Text);}functionTypeText(){for(varletterinword.ToCharArray()){//做一个循环,定义一个变量letter逐次等于word中的每一个字符Text+=letter;//把这些字赋值给Textif(sound)//控制声音,出现一个字,发一个声音audio.PlayOneShot(sound);yieldWaitForSeconds(letterPause);}}主板游戏系列程序:CameraMoveRotate(第一视角界面场景自控程序)使用时点击鼠标右键可以自由转换视角。同时点击AWSD键可以控制前进后退,向左向右。C#程序。usingUnityEngine;usingSystem.Collections;///MouseLookrotatesthetransformbasedonthemousedelta.///MinimumandMaximumvaluescanbeusedtoconstrainthepossiblerotation///TomakeanFPSstylecharacter:///-Createacapsule.///-Addarigidbodytothecapsule///-AddtheMouseLookscripttothecapsule.///->SetthemouselooktouseLookX.(Youwanttoonlyturncharacterbutnottiltit)///-AddFPSWalkerscripttothecapsule///-Createacamera.Makethecameraachildofthecapsule.Resetit'stransform.///-AddaMouseLookscripttothecamera.///->SetthemouselooktouseLookY.(Youwantthecameratotiltupanddownlikeahead.Thecharacteralreadyturns.)[AddComponentMenu("Camera-Control/MouseLook")]publicclassCameraMoveRotate:MonoBehaviour{publicenumRotationAxes{MouseXAndY=0,MouseX=1,MouseY=2}publicRotationAxesaxes=RotationAxes.MouseXAndY;publicfloatsensitivityX=15F;publicfloatsensitivityY=15F;publicfloatminimumX=-360F;publicfloatmaximumX=360F;publicfloatminimumY=-60F;publicfloatmaximumY=60F;publicfloatmoveSpeed=5.0F;floatrotationX=0F;floatrotationY=0F;QuaternionoriginalRotation;voidUpdate(){//鼠标控制的转向if(!Input.GetMouseButton(1)){return;}else{if(axes==RotationAxes.MouseXAndY){//ReadthemouseinputaxisrotationX+=Input.GetAxis("MouseX")*sensitivityX;rotationY+=Input.GetAxis("MouseY")*sensitivityY;rotationX=ClampAngle(rotationX,minimumX,maximumX);rotationY=ClampAngle(rotationY,minimumY,maximumY);QuaternionxQuaternion=Quaternion.AngleAxis(rotationX,Vector3.up);QuaternionyQuaternion=Quaternion.AngleAxis(rotationY,-Vector3.right);transform.localRotation=originalRotation*xQuaternion*yQuaternion;}elseif(axes==RotationAxes.MouseX){rotationX+=Input.GetAxis("MouseX")*sensitivityX;rotationX=ClampAngle(rotationX,minimumX,maximumX);QuaternionxQuaternion=Quaternion.AngleAxis(rotationX,Vector3.up);transform.localRotation=originalRotation*xQuaternion;}else{rotationY+=Input.GetAxis("MouseY")*sensitivityY;rotationY=ClampAngle(rotationY,minimumY,maximumY);QuaternionyQuaternion=Quaternion.AngleAxis(-rotationY,Vector3.right);transform.localRotation=originalRotation*yQuaternion;}}//键盘控制的运动floatx=Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed;floatz=Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;transform.Translate(x,0,z);}voidStart(){//Maketherigidbodynotchangerotationif(rigidbody)rigidbody.freezeRotation=true;originalRotation=transform.localRotation;}publicstaticfloatClampAngle(floatangle,floatmin,floatmax){if(angle<-360F)angle+=360F;if(angle>360F)angle-=360F;returnMathf.Clamp(angle,min,max);}}Unity3d闪烁灯光脚本(​http:​/​​/​bbs.9ria.com​/​thread-155680-1-1.html​)也可以用unity3d做舞厅效果。。把脚本扔给灯光就可以了。1.usingUnityEngine;2.usingSystem.Collections;3.[RequireComponent(typeof(Light))]4.publicclassflickeringLight:MonoBehaviour5.{6.  //FlickeringStyles7.  publicenumflickerinLightStyles{CampFire=0,Fluorescent=1};8.  publicflickerinLightStylesflickeringLightStyle=flickerinLightStyles.CampFire;9.  //CampfireMethods10.  publicenumcampfireMethods{Intensity=0,Range=1,Both=2};11.  publiccampfireMethodscampfireMethod=campfireMethods.Intensity;12.  //IntensityStyles13.  publicenumcampfireIntesityStyles{Sine=0,Random=1};14.  publiccampfireIntesityStylescampfireIntesityStyle=campfireIntesityStyles.Random;15.  //RangeStyles16.  publicenumcampfireRangeStyles{Sine=0,Random=1};17.  publiccampfireRangeStylescampfireRangeStyle=campfireRangeStyles.Random;18.  //BaseIntensityValue19.  publicfloatCampfireIntensityBaseValue=0.5f;20.  //IntensityFlickeringPower21.  publicfloatCampfireIntensityFlickerValue=0.1f;22.  //BaseRangeValue23.  publicfloatCampfireRangeBaseValue=10.0f;24.  //RangeFlickeringPower25.  publicfloatCampfireRangeFlickerValue=2.0f;26.  27.  //IfStyleisSine28.  privatefloatCampfireSineCycleIntensity=0.0f;29.  privatefloatCampfireSineCycleRange=0.0f;30.  //"Glow"Speeds31.  publicfloatCampfireSineCycleIntensitySpeed=5.0f;32.  publicfloatCampfireSineCycleRangeSpeed=5.0f;33.  publicfloatFluorescentFlickerMin=0.4f;34.  publicfloatFluorescentFlickerMax=0.5f;35.  publicfloatFluorescentFlicerPercent=0.95f;36.  //NOTIMPLEMENTEDYET!!!!37.  publicboolFluorescentFlickerPlaySound=false;38.  publicAudioClipFluorescentFlickerAudioClip;39.  //------------------------40.41.42.      //Usethisforinitialization43.      voidStart(){44.      45.      }46.      47.      //Updateiscalledonceperframe48.      voidUpdate(){49.      switch(flickeringLightStyle)50.      {51.        //IfFlickeringStyleisCampfire52.        caseflickerinLightStyles.CampFire:53.          //IfcampfiremethodisIntesityORBoth54.          if(campfireMethod==campfireMethods.Intensity||campfireMethod==campfireMethods.Both)55.          {56.              //IfIntensitystyleisSine57.              if(campfireIntesityStyle==campfireIntesityStyles.Sine)58.              {59.                //CycletheCampfireangle60.                CampfireSineCycleIntensity+=CampfireSineCycleIntensitySpeed;61.                if(CampfireSineCycleIntensity>360.0f)CampfireSineCycleIntensity=0.0f;62.                //Base+Values63.                light.intensity=CampfireIntensityBaseValue+((Mathf.Sin(CampfireSineCycleIntensity*Mathf.Deg2Rad)*(CampfireIntensityFlickerValue/2.0f))+(CampfireIntensityFlickerValue/2.0f));64.              }65.              elselight.intensity=CampfireIntensityBaseValue+Random.Range(0.0f,CampfireIntensityFlickerValue);66.          }67.          //IfcampfiremethodisRangeORBoth68.          if(campfireMethod==campfireMethods.Range||campfireMethod==campfireMethods.Both)69.          {70.              //IfRangestyleisSine71.              if(campfireRangeStyle==campfireRangeStyles.Sine)72.              {73.                //CycletheCampfireangle74.                CampfireSineCycleRange+=CampfireSineCycleRangeSpeed;75.                if(CampfireSineCycleRange>360.0f)CampfireSineCycleRange=0.0f;76.                //Base+Values77.                light.range=CampfireRangeBaseValue+((Mathf.Sin(CampfireSineCycleRange*Mathf.Deg2Rad)*(CampfireSineCycleRange/2.0f))+(CampfireSineCycleRange/2.0f));78.              }79.              elselight.range=CampfireRangeBaseValue+Random.Range(0.0f,CampfireRangeFlickerValue);80.          }81.          break;82.        //IfFlickeringStyleisFluorescent83.        caseflickerinLightStyles.Fluorescent:84.          if(Random.Range(0.0f,1.0f)>FluorescentFlicerPercent)85.          {86.              light.intensity=FluorescentFlickerMin;87.              //CheckAudio-NOTIMPLEMENTEDYET88.              if(FluorescentFlickerPlaySound)89.              {90.              }91.          }92.          elselight.intensity=FluorescentFlickerMax;93.          break;94.        default:95.          //Youshouldnotbehere.96.          break;97.      }98.      99.      }100.}101.102.103.104.(注:可编辑下载,若有不当之处,请指正,谢谢!)105.106.107.108.109.110.111.
本文档为【Unity3D常用脚本】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: ¥20.0 已有0 人下载
最新资料
资料动态
专题动态
机构认证用户
夕夕资料
拥有专业强大的教研实力和完善的师资团队,专注为用户提供合同简历、论文写作、PPT设计、计划书、策划案、各类模板等,同时素材和资料部分来自网络,仅供参考.
格式:doc
大小:2MB
软件:Word
页数:0
分类:成人教育
上传时间:2021-03-30
浏览量:86