国产精品电影_久久视频免费_欧美日韩国产激情_成年人视频免费在线播放_日本久久亚洲电影_久久都是精品_66av99_九色精品美女在线_蜜臀a∨国产成人精品_冲田杏梨av在线_欧美精品在线一区二区三区_麻豆mv在线看

Unity3D 游戲引擎之iOS自定義游戲搖桿與飛機(jī)平滑移動(dòng)

移動(dòng)開發(fā) iOS 游戲開發(fā)
移動(dòng)開發(fā)游戲中使用到的觸摸游戲搖桿在iPhone上是非常普遍的,畢竟是全觸摸屏手機(jī),今天MOMO 通過(guò)一個(gè)小例子和大家討論Unity3D 中如何自定義一個(gè)漂亮的全觸摸游戲搖桿。

值得高興的是,Unity3D 游戲引擎的標(biāo)準(zhǔn)資源中已經(jīng)幫助我們封裝了一個(gè)游戲搖桿腳本,所以實(shí)現(xiàn)部分的代碼可以完全借助它的,具體調(diào)用需要我們自己來(lái)。

 Joystick.js是官方提供的腳本,具體代碼如下,有興趣的朋友可以仔細(xì)研究研究,MOMO就不多說(shuō)啦。哇咔咔~

[代碼]js代碼:

001 ////////////////////////////////////////////////////////////// 
002 // Joystick.js 
003 // Penelope iPhone Tutorial 
004 // 
005 // Joystick creates a movable joystick (via GUITexture) that  
006 // handles touch input, taps, and phases. Dead zones can control 
007 // where the joystick input gets picked up and can be normalized. 
008 // 
009 // Optionally, you can enable the touchPad property from the editor 
010 // to treat this Joystick as a TouchPad. A TouchPad allows the finger 
011 // to touch down at any point and it tracks the movement relatively  
012 // without moving the graphic 
013 ////////////////////////////////////////////////////////////// 
014    
015 @script RequireComponent( GUITexture ) 
016    
017 // A simple class for bounding how far the GUITexture will move 
018 class Boundary  
019
020     var min : Vector2 = Vector2.zero; 
021     var max : Vector2 = Vector2.zero; 
022
023    
024 static private var joysticks : Joystick[];                  // A static collection of all joysticks 
025 static private var enumeratedJoysticks : boolean = false; 
026 static private var tapTimeDelta : float = 0.3;              // Time allowed between taps 
027    
028 var touchPad : boolean;                                     // Is this a TouchPad? 
029 var touchZone : Rect; 
030 var deadZone : Vector2 = Vector2.zero;                      // Control when position is output 
031 var normalize : boolean = false;                            // Normalize output after the dead-zone? 
032 var position : Vector2;                                     // [-1, 1] in x,y 
033 var tapCount : int;                                         // Current tap count 
034    
035 private var lastFingerId = -1;                              // Finger last used for this joystick 
036 private var tapTimeWindow : float;                          // How much time there is left for a tap to occur 
037 private var fingerDownPos : Vector2; 
038 private var fingerDownTime : float; 
039 private var firstDeltaTime : float = 0.5; 
040    
041 private var gui : GUITexture;                               // Joystick graphic 
042 private var defaultRect : Rect;                             // Default position / extents of the joystick graphic 
043 private var guiBoundary : Boundary = Boundary();            // Boundary for joystick graphic 
044 private var guiTouchOffset : Vector2;                       // Offset to apply to touch input 
045 private var guiCenter : Vector2;                            // Center of joystick 
046    
047 function Start() 
048
049     // Cache this component at startup instead of looking up every frame     
050     gui = GetComponent( GUITexture ); 
051        
052     // Store the default rect for the gui, so we can snap back to it 
053     defaultRect = gui.pixelInset;    
054        
055     defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5; 
056     defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5; 
057        
058     transform.position.x = 0.0; 
059     transform.position.y = 0.0; 
060            
061     if ( touchPad ) 
062     { 
063         // If a texture has been assigned, then use the rect ferom the gui as our touchZone 
064         if ( gui.texture ) 
065             touchZone = defaultRect; 
066     } 
067     else 
068     {                
069         // This is an offset for touch input to match with the top left 
070         // corner of the GUI 
071         guiTouchOffset.x = defaultRect.width * 0.5; 
072         guiTouchOffset.y = defaultRect.height * 0.5; 
073            
074         // Cache the center of the GUI, since it doesn't change 
075         guiCenter.x = defaultRect.x + guiTouchOffset.x; 
076         guiCenter.y = defaultRect.y + guiTouchOffset.y; 
077            
078         // Let's build the GUI boundary, so we can clamp joystick movement 
079         guiBoundary.min.x = defaultRect.x - guiTouchOffset.x; 
080         guiBoundary.max.x = defaultRect.x + guiTouchOffset.x; 
081         guiBoundary.min.y = defaultRect.y - guiTouchOffset.y; 
082         guiBoundary.max.y = defaultRect.y + guiTouchOffset.y; 
083     } 
084
085    
086 function Disable() 
087
088     gameObject.active = false; 
089     enumeratedJoysticks = false; 
090
091    
092 function ResetJoystick() 
093
094     // Release the finger control and set the joystick back to the default position 
095     gui.pixelInset = defaultRect; 
096     lastFingerId = -1; 
097     position = Vector2.zero; 
098     fingerDownPosition = Vector2.zero; 
099        
100     if ( touchPad ) 
101         gui.color.a = 0.025;     
102
103    
104 function IsFingerDown() : boolean 
105
106     return (lastFingerId != -1); 
107
108        
109 function LatchedFinger( fingerId : int ) 
110
111     // If another joystick has latched this finger, then we must release it 
112     if ( lastFingerId == fingerId ) 
113         ResetJoystick(); 
114
115    
116 function Update() 
117 {    
118     if ( !enumeratedJoysticks ) 
119     { 
120         // Collect all joysticks in the game, so we can relay finger latching messages 
121         joysticks = FindObjectsOfType( Joystick ); 
122         enumeratedJoysticks = true; 
123     }    
124            
125     var count = Input.touchCount; 
126        
127     // Adjust the tap time window while it still available 
128     if ( tapTimeWindow > 0 ) 
129         tapTimeWindow -= Time.deltaTime; 
130     else 
131         tapCount = 0; 
132        
133     if ( count == 0 ) 
134         ResetJoystick(); 
135     else 
136     { 
137         for(var i : int = 0;i < count; i++) 
138         { 
139             var touch : Touch = Input.GetTouch(i);           
140             var guiTouchPos : Vector2 = touch.position - guiTouchOffset; 
141        
142             var shouldLatchFinger = false; 
143             if ( touchPad ) 
144             {                
145                 if ( touchZone.Contains( touch.position ) ) 
146                     shouldLatchFinger = true; 
147             } 
148             else if ( gui.HitTest( touch.position ) ) 
149             { 
150                 shouldLatchFinger = true; 
151             }        
152        
153             // Latch the finger if this is a new touch 
154             if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) ) 
155             { 
156                    
157                 if ( touchPad ) 
158                 { 
159                     gui.color.a = 0.15; 
160                        
161                     lastFingerId = touch.fingerId; 
162                     fingerDownPos = touch.position; 
163                     fingerDownTime = Time.time; 
164                 } 
165                    
166                 lastFingerId = touch.fingerId; 
167                    
168                 // Accumulate taps if it is within the time window 
169                 if ( tapTimeWindow > 0 ) 
170                     tapCount++; 
171                 else 
172                 { 
173                     tapCount = 1; 
174                     tapTimeWindow = tapTimeDelta; 
175                 } 
176                                                
177                 // Tell other joysticks we've latched this finger 
178                 for ( var j : Joystick in joysticks ) 
179                 { 
180                     if ( j != this ) 
181                         j.LatchedFinger( touch.fingerId ); 
182                 }                        
183             }                
184        
185             if ( lastFingerId == touch.fingerId ) 
186             {    
187                 // Override the tap count with what the iPhone SDK reports if it is greater 
188                 // This is a workaround, since the iPhone SDK does not currently track taps 
189                 // for multiple touches 
190                 if ( touch.tapCount > tapCount ) 
191                     tapCount = touch.tapCount; 
192                    
193                 if ( touchPad ) 
194                 {    
195                     // For a touchpad, let's just set the position directly based on distance from initial touchdown 
196                     position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 ); 
197                     position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 ); 
198                 } 
199                 else 
200                 {                    
201                     // Change the location of the joystick graphic to match where the touch is 
202                     gui.pixelInset.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x ); 
203                     gui.pixelInset.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );      
204                 } 
205                    
206                 if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled ) 
207                     ResetJoystick();                     
208             }            
209         } 
210     } 
211        
212     if ( !touchPad ) 
213     { 
214         // Get a value between -1 and 1 based on the joystick graphic location 
215         position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x; 
216         position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y; 
217     } 
218        
219     // Adjust for dead zone  
220     var absoluteX = Mathf.Abs( position.x ); 
221     var absoluteY = Mathf.Abs( position.y ); 
222        
223     if ( absoluteX < deadZone.x ) 
224     { 
225         // Report the joystick as being at the center if it is within the dead zone 
226         position.x = 0; 
227     } 
228     else if ( normalize ) 
229     { 
230         // Rescale the output after taking the dead zone into account 
231         position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x ); 
232     } 
233            
234     if ( absoluteY < deadZone.y ) 
235     { 
236         // Report the joystick as being at the center if it is within the dead zone 
237         position.y = 0; 
238     } 
239     else if ( normalize ) 
240     { 
241         // Rescale the output after taking the dead zone into account 
242         position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y ); 
243     } 
244 }

單擊Create 創(chuàng)建一個(gè)GUI Texture,命名為Joy ,它用來(lái)顯示游戲搖桿,如下圖所示將搖桿的圖片資源,與搖桿的腳本連線賦值給Joy.  Pixel Inset 中可以設(shè)置搖桿的顯示位置與顯示寬高。

到這一步 build and run 就可以在iPhone上看到這個(gè)游戲搖桿,并且可以通過(guò)觸摸它,360度平滑過(guò)度。

在屏幕中繪制一個(gè)飛機(jī),通過(guò)游戲搖桿去控制飛機(jī)的移動(dòng)。

創(chuàng)建一個(gè)腳本,命名為Main.js 如下圖所示  將 Main.js 、joy、plan 分別 綁定在Main Camera 上。

moveJoystick.position.x;

moveJoystick.position.y;

這兩個(gè)值是非常重要的兩個(gè)信息,它們的取值范圍是 -1 到 +1 ,表示 用戶觸摸搖桿的位置, 上 下 左 右 的信息。

[代碼]js代碼:

01 //游戲搖桿對(duì)象 
02 var moveJoystick : Joystick; 
03    
04 //飛機(jī)的貼圖 
05 var plan : Texture; 
06    
07 //飛機(jī)在屏幕中的坐標(biāo) 
08 var x = 0; 
09 var y = 0; 
10    
11 //避免飛機(jī)飛出屏幕,分別是X、Y最大坐標(biāo),最小坐標(biāo)是0、0 
12 var cross_x = 0; 
13 var cross_y = 0; 
14    
15 //飛機(jī)移動(dòng)的速度 
16 var planSpeed = 20; 
17    
18 function Start() { 
19     //初始化賦值 
20     x = 100; 
21     y = 100; 
22     cross_x = Screen.width -  plan.width; 
23     cross_y = Screen.height -  plan.height; 
24        
25
26    
27 function Update () { 
28     //得到游戲搖桿的反饋信息,得到的值是 -1 到 +1 之間 
29        
30     var touchKey_x =  moveJoystick.position.x; 
31     var touchKey_y =  moveJoystick.position.y; 
32        
33     //搖桿向左 
34     if(touchKey_x == -1){ 
35         x -= planSpeed; 
36            
37     } 
38     //搖桿向右 
39     else if(touchKey_x == 1){ 
40         x += planSpeed; 
41            
42     } 
43     //搖桿向上 
44     if(touchKey_y == -1){ 
45         y += planSpeed; 
46            
47     } 
48     //搖桿向下 
49     else if(touchKey_y == 1){ 
50         y -= planSpeed; 
51            
52     } 
53        
54     //防止飛機(jī)飛出屏幕,出界檢測(cè) 
55     if(x < 0){ 
56         x = 0; 
57     }else if(x > cross_x){ 
58         x = cross_x; 
59     } 
60        
61     if(y < 0){ 
62         y = 0; 
63     }else if(y > cross_y){ 
64         y = cross_y; 
65     } 
66
67    
68    
69    
70    
71 function OnGUI () { 
72    
73    
74   //將飛機(jī)繪制在屏幕中 
75   GUI.DrawTexture(Rect(x,y,128,128),plan);   
76    
77 }

導(dǎo)出 build and run  看看在iPhone 上的效果,通過(guò)觸摸游戲搖桿可以控制飛機(jī)的移動(dòng)啦,不錯(cuò)吧,哇咔咔~~

 

 

 

責(zé)任編輯:冰凝兒
相關(guān)推薦

2012-12-24 08:40:12

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 08:57:35

iOSUnity3D

2012-12-24 08:50:21

iOSUnity3D

2012-12-24 09:04:04

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 08:56:15

iOSUnity3D

2012-12-24 09:07:09

iOSUnity3D

2012-12-24 08:51:23

iOSUnity3D

2012-12-24 09:01:41

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 09:06:14

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 09:02:48

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:00:31

iOSUnity3D

2012-12-24 09:17:12

iOSUnity3D

2012-12-24 09:15:57

iOSUnity3D

2013-05-20 17:48:20

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

鲁大师成人一区二区三区| 国产999精品久久| 三级黄色的网站| 国产成人精品综合在线观看| 57pao精品| 欧美成人影院| 欧美一二三四在线| 头脑特工队2在线播放| 久久久另类综合| 9色porny| 国产真实精品久久二三区| 免费精品视频一区| 激情综合网址| 国产精品日韩欧美综合| 盗摄系列偷拍视频精品tp| 亚洲天堂一区二区三区| 欧美人体视频xxxxx| 欧美日韩亚洲综合一区二区三区| 成视人a免费观看视频| 国产精品欧美一区喷水| 中文字幕日本最新乱码视频| 丁香六月综合激情| 久久手机在线视频| 成人性视频免费网站| 老太脱裤让老头玩ⅹxxxx| 99精品视频一区| 国产免费毛卡片| 国产亚洲精品免费| 免费看污污网站| 日韩理论片在线| 在线免费精品视频| 香港伦理在线| 精品国产电影一区二区| 激情图片在线观看高清国产| 日韩av在线精品| 欧美大片免费观看网址| 久久精品91久久香蕉加勒比| 97se亚洲| 亚洲中国色老太| 天堂资源在线中文精品| 特级西西444| 久久精品一区二区三区av| 激情亚洲色图| 欧美午夜精品久久久久久人妖| av在线第一页| 亚洲精品97久久| 久久精品国产福利| 韩国三级日本三级少妇99| 成人一区而且| 欧美一卡2卡3卡4卡无卡免费观看水多多 | 综合亚洲色图| 国产日韩欧美另类| 三级影片在线观看欧美日韩一区二区| 在线免费观看成人| 中文av一区特黄| av大片在线观看| 亚洲性生活视频在线观看| 天堂在线精品| 国内外成人免费视频| 不卡在线观看av| 亚洲美女主播视频免费观看| 欧美大片一区二区| gogo久久日韩裸体艺术| 国产精品.com| 99久久精品费精品国产一区二区| 少妇高潮露脸国语对白| 日韩一区二区免费电影| 国产成人澳门| 91精品久久久久久蜜桃| 国产精品一区专区| 爽爽免费视频| 亚洲精品福利免费在线观看| 国产伦精品一区二区三区在线播放| 91成人免费观看| av不卡在线观看| 3d成人动漫在线| 欧美激情a在线| 亚洲区国产区| 啊啊啊好爽视频| 日韩精品久久久久 | 久久视频在线视频| 亚洲精品一二| 成人网18免费网站在线| 精品精品欲导航| 加勒比久久综合| 色哺乳xxxxhd奶水米仓惠香| 亚洲成人福利片| 欧美另类激情| 欧美高清性xxxxhd| 亚洲男人天堂av| 天堂网在线最新版www中文网| 国产在线观看精品| 久久久久久久久岛国免费| 欧美hdxxxxx| 成人做爽爽免费视频| 久久久www成人免费毛片麻豆 | 成人午夜电影在线播放| 久久亚洲春色中文字幕久久久| 成人免费在线| 国产伦精品免费视频| 成人动漫一区二区| 宅男网站在线免费观看| 成人免费视频网址| 国产精品你懂的在线欣赏| 国产精品久久久久av电视剧| 国产精品久久久久久久小唯西川| 亚洲欧洲日韩av| 久久久加勒比| 亚洲一卡二卡三卡| 3atv一区二区三区| 999久久久91| 好男人社区在线视频| 草民午夜欧美限制a级福利片| 精品一区二区三区在线播放| 毛片在线不卡| 国产精品传媒毛片三区| 亚洲成av人片一区二区三区| 精品国产午夜肉伦伦影院| 妞干网在线观看视频| 日韩精品亚洲元码| 欧美激情综合色综合啪啪| 在线观看污污视频| 青青久久aⅴ北条麻妃| 久久久久久99久久久精品网站| 波多野结衣成人在线| 国产精品久久久久四虎| 欧美区一区二区| 国产精品人人爽人人爽| 欧美成人免费网| 国产色综合一区| 高潮久久久久久久久久久久久久| 日本三级免费网站| 久久香蕉国产线看观看av| 国产91精品露脸国语对白| 鲁鲁在线中文| 欧美一区二区三区综合| 亚洲欧美精品一区二区| 国产成人aaaa| 日韩一区网站| 蜜桃传媒av| 国产一区二区在线免费视频| 偷拍一区二区三区| 欧美日韩精品| av免费看在线| 欧美a级黄色大片| 久久视频在线播放| 亚洲女人的天堂| 欧美一区二区三区久久精品茉莉花| 波多结衣在线观看| 国产精品久久久久一区二区| 一本一道波多野结衣一区二区| 99av国产精品欲麻豆| 欧美xxxx少妇| 国产精品沙发午睡系列| 日本精品va在线观看| 欧美性生活大片免费观看网址| 亚洲国产高清视频| 日韩av一卡| 窝窝九色成人影院| 国产不卡一区二区三区在线观看| 精品日韩一区二区| 成人高清视频在线| 精品国产日韩欧美| 69久久夜色| 久久国产精品网| 欧美尤物巨大精品爽| 欧美日韩一区二区在线视频| 国产一区不卡精品| 欧美日韩一区二区三区在线电影| 免费在线视频你懂得| 异国色恋浪漫潭| 欧美亚洲成人网| 6080国产精品一区二区| jvid福利写真一区二区三区| 成人高清电影网站| 电影k8一区二区三区久久| 国产日产欧美视频| 国产精品久久久久免费a∨大胸| 5858s免费视频成人| 91免费观看视频| 国产精品成人a在线观看| 欧美xxxbbb| 国产乱子视频| 日本一区二区视频| 久久久久久久久久久人体| 欧美日韩一区高清| 久久久久久久综合日本| 激情婷婷亚洲| av不卡一区| 亚洲婷婷噜噜| 欧美精品123区| 国产精品视频yy9299一区| 国产一级一区二区| 成人精品动漫一区二区三区| 国产美女福利在线| www.大网伊人| 成人性做爰片免费视频| 国产日韩精品在线播放| 久久久精品影院| 91精品国产91久久久久久最新毛片 |