Android Unity3D游戏开发之切割方块(附源码)


开发环境

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

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.    
  11. function Update()   
  12. {   
  13.     if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)   
  14.     {   
  15.         if(Input.touchCount == 1)   
  16.         {   
  17.             mousePos1 = Input.touches[0].position;   
  18.         }   
  19.         else if(Input.touchCount == 2)   
  20.         {   
  21.             mousePos1 = Input.touches[0].position;   
  22.             mousePos2 = Input.touches[1].position;   
  23.         }   
  24.     }   
  25.     else   
  26.     {   
  27.         mousePos1 = Input.mousePosition;   
  28.     }   
  29.     target1.position = camera.ScreenToWorldPoint (Vector3(mousePos1.x,mousePos1.y,1));   
  30.     target2.position = camera.ScreenToWorldPoint (Vector3(mousePos2.x,mousePos2.y,1));   
  31. }   
  32.    
  33. function LateUpdate()   
  34. {   
  35.     if(Input.GetKey(KeyCode.Escape))   
  36.     {   
  37.         Application.Quit();   
  38.     }   
  39. }   
  40.    
  41. function OnGUI()    
  42. {      
  43.     if(MouseImg)   
  44.     {   
  45.         GUI.skin = Mouse;   
  46.         var windowRect : Rect = Rect (mousePos1.x - cursorImage.width/2, Screen.height - mousePos1.y - cursorImage.height/2, cursorImage.width, cursorImage.height);   
  47.         windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");   
  48.     }   
  49.        
  50.     if(GUILayout.Button("PlanA"))   
  51.     {   
  52.         Screen.showCursor = !Screen.showCursor;   
  53.         target1.gameObject.active = !target1.gameObject.active;   
  54.         target1C.gameObject.active = target1.gameObject.active;   
  55.         if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)   
  56.         {   
  57.             target2.gameObject.active = !target2.gameObject.active;   
  58.             target2C.gameObject.active = target2.gameObject.active;   
  59.         }   
  60.     }   
  61.     else if(GUILayout.Button("PlanB"))   
  62.     {   
  63.         Screen.showCursor = !Screen.showCursor;   
  64.         MouseImg = !MouseImg;   
  65.     }   
  66.     else if(GUILayout.Button("Restart"))   
  67.     {   
  68.         Application.LoadLevel(0);   
  69.     }   
  70.        
  71.      if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to YUHUA!"))       
  72.      {      
  73.         Application.OpenURL("http://blog.csdn.net/libeifs");      
  74.      }      
  75.        
  76.     GUI.color = Color.white;   
  77.     GUILayout.Label("fps:" + FPS.fps.ToString("f0") + "      " + FPS.afps.ToString("f0"));   
  78. }   
  79.    
  80. function DoMyWindow (windowID : int)   
  81. {      
  82. }  

这里第二种替换图片,羽化用的是GUI中的Window,因为Window可以改变层级,大家研究下就知道,事件可以按需求自己判断。若大家有什么更好的点子,希望与羽化交流~ ~ 

Android Unity3D游戏开发之切割方块源码下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/Android源码集锦/2011年/10月/Android Unity3D游戏开发之切割方块源码/

相关内容