歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Unity3D游戲開發之切割方塊(附源碼)

Android Unity3D游戲開發之切割方塊(附源碼)

日期:2017/3/1 11:13:13   编辑:Linux編程

開發環境

Window7

Unity3D 3.4.1

MB525defy

Android 2.2.1

雖然標題是游戲開發,其實也是羽化測試鼠標和彈幕的時候做的一個小游戲,於是一點點有模有樣了,至於為什麼又是方塊,囧!所以就分享出來,內容很簡陋,代碼很簡單,先送上效果截圖。。。Android中PlanA是二刀流版- -大家湊合看吧-0-



本次學習:

1. 彈幕追蹤簡單AI

2. Unity鼠標特效

1. 彈幕追蹤簡單AI

群裡面有人共享的一個網頁彈幕代碼,通過XML控制,做得真的很不錯,這裡的彈幕AI很簡單,可以用到很多飛行游戲中,如果想做出花哨的子彈軌跡效果,這個要花很多時間在上面鑽研了,這裡的代碼只能實現前期的軌跡與方位追蹤。希望能給大家參考~ ~

Boss.js

  1. var target : Transform;
  2. var speed : float = 4.0;
  3. var LookAt : boolean = true;
  4. var go : boolean = false;
  5. private var mx : float;
  6. private var my : float;
  7. private var mz : float;
  8. private var dis : float;
  9. private var save : Vector3;
  10. private var time : int;
  11. function Start()
  12. {
  13. save = transform.position;
  14. dis = Random.value * 8- 4;
  15. mx = Random.value * 8 - 4;
  16. my = Random.value * 8 - 4;
  17. mz = Random.value * 8 - 4;
  18. time = 30 - Random.value*30;
  19. yield WaitForSeconds (time);
  20. go = true;
  21. }
  22. function Update ()
  23. {
  24. if(go)
  25. {
  26. if(Vector3.Distance(target.position, transform.position) > 5 && LookAt)
  27. {
  28. transform.LookAt(target.position);
  29. }
  30. else if(Vector3.Distance(target.position, transform.position) < 5)
  31. {
  32. LookAt = false;
  33. Destroy(gameObject,2);
  34. }
  35. transform.Translate(Vector3.forward * Time.deltaTime*speed);
  36. }
  37. else
  38. {
  39. transform.RotateAround(Vector3(mx,my,mz),save + Vector3(mx,my,0),Time.deltaTime * speed * dis);
  40. }
  41. }

2. Unity鼠標特效

上篇提過鼠標特效的事情,於是羽化就做了測試,由於現在公司游戲主攻Web端,手機端在此之上刪減,所以操作性質就發生了改變,但羽化還是寫了手機端的代碼,還是二刀流- -,圖片從晚上找的,這裡提供了兩種鼠標特效,當然在真正做的時候羽化估計會用第二種加強版,所以看大家的平台和需求來。MOUSE.js
  1. var target1 : Transform;
  2. var target1C : Transform;
  3. var target2 : Transform;
  4. var target2C : Transform;
  5. var mousePos1 : Vector3;
  6. var mousePos2 : Vector3;
  7. var cursorImage : Texture;
  8. var Mouse : GUISkin;
  9. private var MouseImg : boolean = false;
  10. function Update()
  11. {
  12. if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  13. {
  14. if(Input.touchCount == 1)
  15. {
  16. mousePos1 = Input.touches[0].position;
  17. }
  18. else if(Input.touchCount == 2)
  19. {
  20. mousePos1 = Input.touches[0].position;
  21. mousePos2 = Input.touches[1].position;
  22. }
  23. }
  24. else
  25. {
  26. mousePos1 = Input.mousePosition;
  27. }
  28. target1.position = camera.ScreenToWorldPoint (Vector3(mousePos1.x,mousePos1.y,1));
  29. target2.position = camera.ScreenToWorldPoint (Vector3(mousePos2.x,mousePos2.y,1));
  30. }
  31. function LateUpdate()
  32. {
  33. if(Input.GetKey(KeyCode.Escape))
  34. {
  35. Application.Quit();
  36. }
  37. }
  38. function OnGUI()
  39. {
  40. if(MouseImg)
  41. {
  42. GUI.skin = Mouse;
  43. var windowRect : Rect = Rect (mousePos1.x - cursorImage.width/2, Screen.height - mousePos1.y - cursorImage.height/2, cursorImage.width, cursorImage.height);
  44. windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
  45. }
  46. if(GUILayout.Button("PlanA"))
  47. {
  48. Screen.showCursor = !Screen.showCursor;
  49. target1.gameObject.active = !target1.gameObject.active;
  50. target1C.gameObject.active = target1.gameObject.active;
  51. if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  52. {
  53. target2.gameObject.active = !target2.gameObject.active;
  54. target2C.gameObject.active = target2.gameObject.active;
  55. }
  56. }
  57. else if(GUILayout.Button("PlanB"))
  58. {
  59. Screen.showCursor = !Screen.showCursor;
  60. MouseImg = !MouseImg;
  61. }
  62. else if(GUILayout.Button("Restart"))
  63. {
  64. Application.LoadLevel(0);
  65. }
  66. if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to YUHUA!"))
  67. {
  68. Application.OpenURL("http://blog.csdn.net/libeifs");
  69. }
  70. GUI.color = Color.white;
  71. GUILayout.Label("fps:" + FPS.fps.ToString("f0") + " " + FPS.afps.ToString("f0"));
  72. }
  73. function DoMyWindow (windowID : int)
  74. {
  75. }

這裡第二種替換圖片,羽化用的是GUI中的Window,因為Window可以改變層級,大家研究下就知道,事件可以按需求自己判斷。若大家有什麼更好的點子,希望與羽化交流~ ~

Android Unity3D游戲開發之切割方塊源碼下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/10月/Android Unity3D游戲開發之切割方塊源碼/

Copyright © Linux教程網 All Rights Reserved