收藏 分销(赏)

Unity3D常用脚本.doc

上传人:丰**** 文档编号:3112491 上传时间:2024-06-18 格式:DOC 页数:16 大小:2.80MB
下载 相关 举报
Unity3D常用脚本.doc_第1页
第1页 / 共16页
Unity3D常用脚本.doc_第2页
第2页 / 共16页
Unity3D常用脚本.doc_第3页
第3页 / 共16页
Unity3D常用脚本.doc_第4页
第4页 / 共16页
Unity3D常用脚本.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

1、场景转换:var screenbutton: boolean;function Start() DontDestroyOnLoad(this);screenbutton = true;function Update () if(Input.GetKeyDown(KeyCode.Escape) screenbutton = true;function OnGUI() if(screenbutton=true) if(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-190,60,50),Scene1) print(You click Scene

2、1); Application.LoadLevel (1); screenbutton = false; else if(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-130,60,50),Scene2) print(You click Scene2); Application.LoadLevel (2); screenbutton = false; else if( GUI.Button(Rect(Screen.width/2-375,Screen.height/2-0,50,50),Quit) print(You click Quit

3、); Application.Quit(); 心得:场景转换时,这个程序贴在按钮界面场景中。在Build Setting(导出)(在File下)菜单中加入所需场景。0为按钮界面。可以无限增加场景。 else if(GUI.Button(Rect(Screen.width/2-380,Screen.height/2-130,60,50),Scene2) print(You click Scene2); Application.LoadLevel (2); screenbutton = false;复制此段程序并做修改。运动控制键:上下左右var TankSpeed:int = 20;functi

4、on Update () if(Input.GetKey(KeyCode.W) print(go); transform.Translate(Vector3.forward * Time.deltaTime * TankSpeed);else if(Input.GetKey(KeyCode.S) print(go back); transform.Translate(Vector3.forward * Time.deltaTime * -TankSpeed);else if(Input.GetKey(KeyCode.A) print(turn left); transform.Rotate(V

5、ector3.up * Time.deltaTime * -TankSpeed);else if(Input.GetKey(KeyCode.D) print(turn right); transform.Rotate(Vector3.up * Time.deltaTime * TankSpeed);需要放在第一视角的物体或者摄像机上。加天空背景:菜单栏Component下的Rendering下的Skybox。将他加在所需场景的摄像机上,选择所需的天空。运行时自然出现。各种按钮汇总:鼠标碰触后显示说明文字。function OnGUI () GUI.Button (Rect (0, 0, 100

6、, 20), GUIContent (Click me, This is the tooltip); GUI.Label (Rect (0, 40, 100, 40), GUI.tooltip); 多项选择,以上都选择。var allOptions = true;var extended1 = true;var extended2 = true;function OnGUI () allOptions = GUI.Toggle (Rect (0,120,150,20), allOptions, Edit All Options);GUI.enabled = allOptions;extende

7、d1 = GUI.Toggle (Rect (20,70,130,20), extended1, Extended Option 1);extended2 = GUI.Toggle (Rect (20,90,130,20), extended2, Extended Option 2);GUI.enabled = true;if (GUI.Button (Rect (0, 160, 150, 20), Ok);print (user clicked ok);鼠标碰触第一个按钮有解释,鼠标碰触第三个对话框有解释。碰第二个没有解释。function OnGUI () GUI.Box (Rect (5

8、, 300, 110, 75), GUIContent (Box, This box has a tooltip);GUI.Button (Rect (10, 230, 100, 20), No tooltip here);GUI.Button (Rect (10, 200, 100, 20),GUIContent (I have a tooltip, The button overrides the box);GUI.Label (Rect (10,260,100,40), GUI.tooltip); 半透明,说明性文字对话框。无选择。function Update () function

9、OnGUI () GUI.BeginGroup (new Rect (Screen.width / 2 - 400, Screen.height / 2 - 300, 800, 600);GUI.Box (new Rect (0,0,800,600),This box is now centered! - here you would put your main menu);GUI.EndGroup ();滚动条,选择按钮。可以放多个按钮并且节省位置。function Update () var scrollPosition = Vector2.zero;function OnGUI () s

10、crollPosition = 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 (); 循

11、环的字幕。var letterPause = 0.2;/定义每个字出现的时间间隔 var sound : AudioClip;/打字机的声音 private var word;/存储你打字机效果想要输出的字的变量 private var Text : String = My name is Ray Yang.Keep it in your heart!;/你希望打字机效果输出的字 function Start () word = Text;/把你输出的字先赋值给word Text = ;/把你要显示的字先抹除,以便你可以在最初显示的时候显示为空,当然你也可以加上其他字,让他先显示,打字机效果打

12、的字会显示在这个后面 yield WaitForSeconds (2); TypeText (); function OnGUI () GUI.Label (Rect(150,15,250,25),Please answer my question,who am I?); GUI.Box (Rect(150,40,250,25),Text); function TypeText () for (var letter in word.ToCharArray() /做一个循环,定义一个变量letter逐次等于word中的每一个字符 Text += letter;/把这些字赋值给Text if (s

13、ound)/控制声音,出现一个字,发一个声音 audio.PlayOneShot (sound); yield WaitForSeconds (letterPause); function Update () if(Text = word) Text=; TypeText(); 有打字声音的字幕循环。var screnbutton:boolean;var icon : Texture;var allOptions = true;var extended1 = true;var extended2 = true;var letterPause = 0.2;/定义每个字出现的时间间隔 var so

14、und : AudioClip;/打字机的声音 private var word;/存储你打字机效果想要输出的字的变量 static var Text : String = This is zcx first project! I can do it.;var del : boolean = false;var particle : GameObject;function Start()DontDestroyOnLoad(this);screnbutton=true;word = Text;Text = ;yield WaitForSeconds (2); TypeText (); funct

15、ion Update () if(Input.GetKeyDown(KeyCode.Escape)screnbutton=true;if( Text = This is zcx first project! I can do it. )Text = ; TypeText (); if(Input.GetMouseButtonDown(0)Text = ;for (var c : char in Input.inputString) / Backspace - Remove the last characterif (c = b) if (guiText.text.Length != 0)gui

16、Text.text = guiText.text.Substring(0, guiText.text.Length - 1);/ End of entryelse if (c = n) print (User entered his name: + guiText.text);/ Normal text input - just append to the endelse guiText.text += c;function OnGUI()if(screnbutton=true)if(GUI.Button(Rect(Screen.width/2-30,Screen.height/2-30,60

17、,50),scene1)print(you click Scene 1);Application.LoadLevel (1);screnbutton=false;else if(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+30,60,50),scene2)print(you click Scene 2);Application.LoadLevel (2);screnbutton=false;else if(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+90,60,50),scene3)

18、print(you click Scene 3);Application.LoadLevel (3);screnbutton=false;else if(GUI.Button(Rect(Screen.width/2-30,Screen.height/2+150,60,50),Quit)print(you click Quit);Application.Quit();/GUI.Button (Rect (0, 0, 100, 20), Click Me);GUI.Button (Rect (0, 0, 100, 20), GUIContent (Click me,This is the tool

19、tip); GUI.Label (Rect (0, 20, 100, 40), GUI.tooltip);allOptions = GUI.Toggle (Rect (40,40,150,20), allOptions, Edit All Options); GUI.enabled = allOptions;extended1 = GUI.Toggle (Rect (60,60,130,20), extended1, Extended Option 1);extended2 = GUI.Toggle (Rect (60,80,130,20), extended2, Extended Optio

20、n 2);GUI.enabled = true;if (GUI.Button (Rect (40, 100, 150, 20), Ok)print (user clicked ok);GUI.BeginGroup (new Rect (Screen.width / 2 - 400, Screen.height/2-300,800,600);GUI.Box (new Rect (0,0,800,600), This box is now centered!-here you would put your main menu);GUI.EndGroup (); GUI.Label (Rect(25

21、0,75,250,25),Please introduce your objec!);GUI.Box (Rect(250,100,250,25),Text); function TypeText () for (var letter in word.ToCharArray() /做一个循环,定义一个变量letter逐次等于word中的每一个字符 Text += letter;/把这些字赋值给Text if (sound)/控制声音,出现一个字,发一个声音 audio.PlayOneShot (sound); yield WaitForSeconds (letterPause); 主板游戏系列程

22、序:CameraMoveRotate(第一视角界面场景自控程序)使用时点击鼠标右键可以自由转换视角。同时点击AWSD键可以控制前进后退,向左向右。C#程序。using UnityEngine;using System.Collections;/ MouseLook rotates the transform based on the mouse delta./ Minimum and Maximum values can be used to constrain the possible rotation/ To make an FPS style character:/ - Create a

23、 capsule./ - Add a rigid body to the capsule/ - Add the MouseLook script to the capsule./ - Set the mouse look to use LookX. (You want to only turn character but not tilt it)/ - Add FPSWalker script to the capsule/ - Create a camera. Make the camera a child of the capsule. Reset its transform./ - Ad

24、d a MouseLook script to the camera./ - Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)AddComponentMenu(Camera-Control/Mouse Look)public class CameraMoveRotate : MonoBehaviour public enum RotationAxes MouseXAndY = 0, MouseX = 1, Mou

25、seY = 2 public RotationAxes axes = RotationAxes.MouseXAndY;public float sensitivityX = 15F;public float sensitivityY = 15F;public float minimumX = -360F;public float maximumX = 360F;public float minimumY = -60F;public float maximumY = 60F;public float moveSpeed = 5.0F;float rotationX = 0F;float rota

26、tionY = 0F;Quaternion originalRotation;void Update ()/鼠标控制的转向if( !Input.GetMouseButton(1) )return;elseif (axes = RotationAxes.MouseXAndY)/ Read the mouse input axisrotationX += Input.GetAxis(Mouse X) * sensitivityX;rotationY += Input.GetAxis(Mouse Y) * sensitivityY;rotationX = ClampAngle (rotationX,

27、 minimumX, maximumX);rotationY = ClampAngle (rotationY, minimumY, maximumY);Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);transform.localRotation = originalRotation * xQuaternion * yQuaternion;else if (

28、axes = RotationAxes.MouseX)rotationX += Input.GetAxis(Mouse X) * sensitivityX;rotationX = ClampAngle (rotationX, minimumX, maximumX);Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);transform.localRotation = originalRotation * xQuaternion;elserotationY += Input.GetAxis(Mouse Y)

29、* sensitivityY;rotationY = ClampAngle (rotationY, minimumY, maximumY);Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);transform.localRotation = originalRotation * yQuaternion; /键盘控制的运动float x = Input.GetAxis(Horizontal) * Time.deltaTime * moveSpeed;float z = Input.GetAxis(V

30、ertical) * Time.deltaTime * moveSpeed;transform.Translate(x, 0, z);void Start ()/ Make the rigid body not change rotationif (rigidbody)rigidbody.freezeRotation = true;originalRotation = transform.localRotation;public static float ClampAngle (float angle, float min, float max)if (angle 360F)angle -=

31、360F;return Mathf.Clamp (angle, min, max);Unity3d闪烁灯光脚本也可以用unity3d做舞厅效果。把脚本扔给灯光就可以了。1. using UnityEngine;2. using System.Collections;3. RequireComponent( typeof( Light ) )4. public class flickeringLight : MonoBehaviour5. 6. / Flickering Styles7. public enum flickerinLightStyles CampFire = 0, Fluores

32、cent = 1 ;8. public flickerinLightStyles flickeringLightStyle = flickerinLightStyles.CampFire;9. / Campfire Methods10. public enum campfireMethods Intensity = 0, Range = 1, Both = 2 ;11. public campfireMethods campfireMethod = campfireMethods.Intensity;12. / Intensity Styles13. public enum campfireI

33、ntesityStyles Sine = 0, Random = 1 ;14. public campfireIntesityStyles campfireIntesityStyle = campfireIntesityStyles.Random;15. / Range Styles16. public enum campfireRangeStyles Sine = 0, Random = 1 ;17. public campfireRangeStyles campfireRangeStyle = campfireRangeStyles.Random;18. / Base Intensity

34、Value19. public float CampfireIntensityBaseValue = 0.5f;20. / Intensity Flickering Power21. public float CampfireIntensityFlickerValue = 0.1f;22. / Base Range Value23. public float CampfireRangeBaseValue = 10.0f;24. / Range Flickering Power25. public float CampfireRangeFlickerValue = 2.0f;26. 27. /

35、If Style is Sine28. private float CampfireSineCycleIntensity = 0.0f;29. private float CampfireSineCycleRange = 0.0f;30. / Glow Speeds31. public float CampfireSineCycleIntensitySpeed = 5.0f;32. public float CampfireSineCycleRangeSpeed = 5.0f;33. public float FluorescentFlickerMin = 0.4f;34. public fl

36、oat FluorescentFlickerMax = 0.5f;35. public float FluorescentFlicerPercent = 0.95f;36. / NOT IMPLEMENTED YET !37. public bool FluorescentFlickerPlaySound = false;38. public AudioClip FluorescentFlickerAudioClip;39. / -40.41.42. / Use this for initialization43. void Start () 44. 45. 46. 47. / Update

37、is called once per frame48. void Update () 49. switch( flickeringLightStyle )50. 51. / If Flickering Style is Campfire52. case flickerinLightStyles.CampFire:53. / If campfire method is Intesity OR Both54. if( campfireMethod = campfireMethods.Intensity | campfireMethod = campfireMethods.Both )55. 56.

38、 / If Intensity style is Sine57. if( campfireIntesityStyle = campfireIntesityStyles.Sine )58. 59. / Cycle the Campfire angle60. CampfireSineCycleIntensity += CampfireSineCycleIntensitySpeed;61. if( CampfireSineCycleIntensity 360.0f ) CampfireSineCycleIntensity = 0.0f;62. / Base + Values63. light.int

39、ensity = CampfireIntensityBaseValue + ( ( Mathf.Sin( CampfireSineCycleIntensity * Mathf.Deg2Rad ) * ( CampfireIntensityFlickerValue / 2.0f ) ) + ( CampfireIntensityFlickerValue / 2.0f ) );64. 65. else light.intensity = CampfireIntensityBaseValue + Random.Range( 0.0f, CampfireIntensityFlickerValue );

40、66. 67. / If campfire method is Range OR Both68. if( campfireMethod = campfireMethods.Range | campfireMethod = campfireMethods.Both )69. 70. / If Range style is Sine71. if( campfireRangeStyle = campfireRangeStyles.Sine )72. 73. / Cycle the Campfire angle74. CampfireSineCycleRange += CampfireSineCycl

41、eRangeSpeed;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. else light.range = CampfireRangeBaseValue + Random.Range( 0.0f, CampfireRangeFlickerValue );80. 81. break;82. / If Flickering Style is Fluorescent83.

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服