Unity3D常用脚本.docx
《Unity3D常用脚本.docx》由会员分享,可在线阅读,更多相关《Unity3D常用脚本.docx(24页珍藏版)》请在冰豆网上搜索。
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;//把您输出的字先赋值给word
Text="";//把您要显示的字先抹除,以便您可以在最初显示的时候显示为空,当然您也可以加上其她字,让她先显示,打字机效果打的字会显示在这个后面
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;//把这些字赋值给Text
if(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-Removethelastcharacter
if(c=="\b"){
if(guiText、text、Length!
=0)
guiText、text=guiText、text、Substring(0,guiText、text、Length-1);
}
//Endofentry
elseif(c=="\n"){
print("Userenteredhisname:
"+guiText、text);
}
//Normaltextinput-justappendtotheend
else{
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;//把这些字赋值给Text
if(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)
{
//Readthemouseinputaxis
rotationX+=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()
{
//Maketherigidbodynotchangerotation