手机3D游戏开发:自定义Joystick的相关设置和脚本源码

Joystick在手游开发中非常常见,也就是在手机屏幕上的虚拟操纵杆,但是Unity3D自带的Joystick贴图比较原始,所以经常有使用自定义贴图的需求。

     下面就来演示一下如何实现自定义JoyStick贴图。更多精彩请关注【狗刨学习网】

     首先导入贴图,注意要把默认的Texture改为GUI要不然尺寸会发生改变:
  
      

    在Inspector面板中点击Texture选项可以实现简单的贴图切换:

       

    选中后便会发现场景中的Joystick已经发生了改变:

       

    同理,可以对右边的Joystick做同样的修改:

       

    当然很多时候这样简单的修改很难满足我们的需求。

    下面来说说对Joystick的常见调整。

    首先是坐标的调整,一般把Postition归零而在GUITexture中调整Pixel Inset:

      

    但是这样依旧会出问题,全屏的时候因为采用了绝对坐标所以会出现这种情况:

      

    所以我们还需要在脚本中稍作调整。

    先来给Joystick加个背景图片。
   
    创建一个JS脚本JoystickBackgroundGUI:

  1. <font face="新宋体" size="2">
  2. @script RequireComponent(Joystick)
  3. @script ExecuteInEditMode ()
  4. var background = new SwitchGUI();
  5. var location = new Location();
  6. private var GUIalpha:float = 1;
  7. private var joystick : Joystick;
  8. joystick = GetComponent (Joystick);
  9. var noGuiStyle : GUIStyle;
  10. function Update() {
  11.     if (joystick.IsFingerDown()) {
  12.         background.up();
  13.     } else {
  14.         background.down();
  15.     }
  16.     if (background.texture != null){
  17.         location.updateLocation();
  18.     }
  19. }
  20. function OnGUI () {
  21.     GUI.color.a = GUIalpha;
  22.     GUI.Box(Rect(location.offset.x + background.offset.x - background.texture.width/2,location.offset.y + background.offset.y - background.texture.height/2,background.texture.width,background.texture.height),background.texture,noGuiStyle);
  23. }
  24. </font>
复制代码

      joystick是Unity自己封装好的对象,其中有IsFingerDown等函数有需要的同学可以查阅一下Unity官网的说明文档。

      脚本中用到了Location和SwitchGUI,这两个函数在另一个脚本   _GUIClasses   中定义:

  1. <font face="新宋体" size="2">[javascript] 


  2. import System.Collections.Generic;
  3. // TextureGUI Class: create a basic class for creating and placing GUI elements
  4. // texture = the texture to display
  5. // offset = pixel offset from top left corner, can be modified for easy positioning
  6. class TextureGUI {
  7.     var texture:Texture; //useful: texture.width, texture.height
  8.     var offset:Vector2; // .x and .y
  9.     private var originalOffset:Vector2; //store the original to correctly reset anchor point
  10.     enum Point { TopLeft, TopRight, BottomLeft, BottomRight, Center} //what part of texture to position around?
  11.     var anchorPoint = Point.TopLeft; // Unity default is from top left corner of texture
  12.     function setAnchor() { // meant to be run ONCE at Start.
  13.         originalOffset = offset;
  14.         if (texture) { // check for null texture
  15.             switch(anchorPoint) { //depending on where we want to center our offsets
  16.                 case anchorPoint.TopLeft: // Unity default, do nothing
  17.                     break;
  18.                 case anchorPoint.TopRight: // Take the offset and go to the top right corner
  19.                     offset.x = originalOffset.x - texture.width;
  20.                     break;
  21.                 case anchorPoint.BottomLeft: // bottom left corner of texture
  22.                     offset.y = originalOffset.y - texture.height;
  23.                     break;
  24.                 case anchorPoint.BottomRight: //bottom right corner of texture
  25.                     offset.x = originalOffset.x - texture.width;
  26.                     offset.y = originalOffset.y - texture.height;
  27.                     break;
  28.                 case anchorPoint.Center: //and the center of the texture (useful for screen center textures)
  29.                     offset.x = originalOffset.x - texture.width/2;
  30.                     offset.y = originalOffset.y - texture.height/2;
  31.                     break;
  32.             }
  33.         }
  34.     }
  35. }
  36. //Timer Class:
  37. class TimerGUI extends TextureGUI { // Extend functionality from TextureGUI for a depreciating timer graphic
  38.     var textureLEnd:Texture; // left side of full texture (non stretching part)
  39.     var offsetLEnd:Vector2; // left side of full texture (non stretching part) start position
  40.     var textureCenter:Texture; // center of timer (will be stretched across width)
  41.     var offsetCenter:Vector2;
  42.     var textureREnd:Texture;
  43.     var offsetREnd:Vector2;
  44.     var timerPerct:float = 1; // percentage (0 to 1) this stretches the center
  45.     var desiredWidth:float = 403; // max width of the timer in pixels
  46.     function setTime(newTime:float) {
  47.         timerPerct = newTime; // sets the percent based on value
  48.     }
  49. }
  50. // SwitchGUI Class: Extends the TextureGUI to be able to load in multiple textures and switch between them
  51. class SwitchGUI extends TextureGUI {
  52.     var switchableTextures = new List.();
  53.     var currentTexture:int = 0;
  54.     function Start() {
  55.         if (switchableTextures.Count > 0) {
  56.             texture = switchableTextures[currentTexture];
  57.         }
  58.     }
  59.     function changeTexture(switchTo:int) {
  60.         if (switchTo < switchableTextures.Count && switchTo >= 0) {
  61.             texture = switchableTextures[switchTo];
  62.             currentTexture = switchTo;
  63.         } else {
  64.             //Debug.Log( this + ": tried to call invalid part of switchTextures array!");
  65.         }
  66.     }
  67.     function up() {
  68.         if ((currentTexture+1) < switchableTextures.Count) {
  69.             ++currentTexture;
  70.             texture = switchableTextures[currentTexture];
  71.         } else {
  72.             //Debug.Log( this + ": at the top!");
  73.         }
  74.     }
  75.     function nextTexture() {
  76.         if ((currentTexture+1) < switchableTextures.Count) { // if we are at the end of the array
  77.             ++currentTexture;
  78.             texture = switchableTextures[currentTexture];
  79.         } else {// loop to the beginning
  80.             currentTexture = 0;
  81.             texture = switchableTextures[currentTexture];
  82.         }
  83.     }
  84.     function down() {
  85.         if ((currentTexture-1) >= 0) {
  86.             --currentTexture;
  87.             texture = switchableTextures[currentTexture];
  88.         } else {
  89.             //Debug.Log( this + ": at the bottom!");
  90.         }
  91.     }
  92. }
  93. // Location class:
  94. class Location {
  95.     enum Point { TopLeft, TopRight, BottomLeft, BottomRight, Center}
  96.     var pointLocation = Point.TopLeft;
  97.     var offset:Vector2;
  98.     function updateLocation() {
  99.         switch(pointLocation) {
  100.             case pointLocation.TopLeft:
  101.                 offset = Vector2(0,0);
  102.                 break;
  103.             case pointLocation.TopRight:
  104.                 offset = Vector2(Screen.width,0);
  105.                 break;
  106.             case pointLocation.BottomLeft:
  107.                 offset = Vector2(0,Screen.height);
  108.                 break;
  109.             case pointLocation.BottomRight:
  110.                 offset = Vector2(Screen.width,Screen.height);
  111.                 break;
  112.             case pointLocation.Center:
  113.                 offset = Vector2(Screen.width/2,Screen.height/2);
  114.                 break;
  115.         }
  116.     }
  117. }
  118. class TextureAnchor {
  119.     enum Point { TopLeft, TopRight, BottomLeft, BottomRight, Center}
  120.     var anchorPoint = Point.TopLeft;
  121.     var offset:Vector2;
  122.     function update() {
  123.         switch(anchorPoint) {
  124.             case anchorPoint.TopLeft:
  125.                 offset = Vector2(0,0);
  126.                 break;
  127.             case anchorPoint.TopRight:
  128.                 offset = Vector2(Screen.width,0);
  129.                 break;
  130.             case anchorPoint.BottomLeft:
  131.                 offset = Vector2(0,Screen.height);
  132.                 break;
  133.             case anchorPoint.BottomRight:
  134.                 offset = Vector2(Screen.width,Screen.height);
  135.                 break;
  136.             case anchorPoint.Center:
  137.                 offset = Vector2(Screen.width/2,Screen.height/2);
  138.                 break;
  139.         }
  140.     }
  141. }
  142. </font>
复制代码

     将脚本拖拽到Joystick上面并且部署好贴图,运行可见Joystick的背景贴图,当然坐标还有点问题:

       

    我们在脚本中将其设置为BottomLeft,并且设置好SwitchTexture:

      

    配置好了之后点击运行,会发现Joystick 的贴图出现在了左下角:

    

   通过脚本中的Pixel设置可以调整两个纹理贴图的坐标并使他们趋于一致:

    

   调整之后的结果如图:

    

    同时将Joystick的脚本换成下面的脚本,可以实现隐藏操纵杆而只在碰到摇杆区域才显示Joystick的效果:
  1. <font face="新宋体" size="2">[javascript] view plaincopy


  2. //
  3. // Joystick.js
  4. // Penelope iPhone Tutorial
  5. //
  6. // Joystick creates a movable joystick (via GUITexture) that
  7. // handles touch input, taps, and phases. Dead zones can control
  8. // where the joystick input gets picked up and can be normalized.
  9. //
  10. // Optionally, you can enable the touchPad property from the editor
  11. // to treat this Joystick as a TouchPad. A TouchPad allows the finger
  12. // to touch down at any point and it tracks the movement relatively
  13. // without moving the graphic
  14. //
  15. #pragma strict
  16. @script RequireComponent( GUITexture )
  17. // A simple class for bounding how far the GUITexture will move
  18. class Boundary
  19. {
  20.     var min : Vector2 = Vector2.zero;
  21.     var max : Vector2 = Vector2.zero;
  22. }
  23. static private var joysticks : Joystick[];                  // A static collection of all joysticks
  24. static private var enumeratedJoysticks : boolean = false;
  25. static private var tapTimeDelta : float = 0.3;              // Time allowed between taps
  26. var touchPad : boolean;                                     // Is this a TouchPad?
  27. var touchZone : Rect;
  28. var deadZone : Vector2 = Vector2.zero;                      // Control when position is output
  29. var normalize : boolean = false;                            // Normalize output after the dead-zone?
  30. var position : Vector2;                                     // [-1, 1] in x,y
  31. var tapCount : int;                                         // Current tap count
  32. private var lastFingerId = -1;                              // Finger last used for this joystick
  33. private var tapTimeWindow : float;                          // How much time there is left for a tap to occur
  34. private var fingerDownPos : Vector2;
  35. private var fingerDownTime : float;
  36. private var firstDeltaTime : float = 0.5;
  37. private var gui : GUITexture;                               // Joystick graphic
  38. private var defaultRect : Rect;                             // Default position / extents of the joystick graphic
  39. private var guiBoundary : Boundary = Boundary();            // Boundary for joystick graphic
  40. private var guiTouchOffset : Vector2;                       // Offset to apply to touch input
  41. private var guiCenter : Vector2;                            // Center of joystick
  42. private var alphaOff:float = 0.0;
  43. function Start()
  44. {
  45.     // Cache this component at startup instead of looking up every frame
  46.     gui = GetComponent( GUITexture );
  47.     // Store the default rect for the gui, so we can snap back to it
  48.     defaultRect = gui.pixelInset;
  49.     gui.color.a = alphaOff;
  50.     defaultRect.x += transform.position.x * Screen.width; // + gui.pixelInset.x; // -  Screen.width * 0.5;
  51.     defaultRect.y += transform.position.y * Screen.height; //+ gui.pixelInset.y; // - Screen.height * 0.5;
  52.     transform.position.x = 0.0;
  53.     transform.position.y = 0.0;
  54.     if ( touchPad )
  55.     {
  56.         // If a texture has been assigned, then use the rect ferom the gui as our touchZone
  57.         if ( gui.texture )
  58.             touchZone = defaultRect;
  59.     }
  60.     else
  61.     {
  62.         // This is an offset for touch input to match with the top left
  63.         // corner of the GUI
  64.         guiTouchOffset.x = defaultRect.width * 0.5;
  65.         guiTouchOffset.y = defaultRect.height * 0.5;
  66.         // Cache the center of the GUI, since it doesn't change
  67.         guiCenter.x = defaultRect.x + guiTouchOffset.x;
  68.         guiCenter.y = defaultRect.y + guiTouchOffset.y;
  69.         // Let's build the GUI boundary, so we can clamp joystick movement
  70.         guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
  71.         guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
  72.         guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
  73.         guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
  74.     }
  75. }
  76. function Disable()
  77. {
  78.     gameObject.active = false;
  79.     enumeratedJoysticks = false;
  80. }
  81. function ResetJoystick()
  82. {
  83.     // Release the finger control and set the joystick back to the default position
  84.     gui.pixelInset = defaultRect;
  85.     lastFingerId = -1;
  86.     position = Vector2.zero;
  87.     fingerDownPos = Vector2.zero;
  88.     gui.color.a = alphaOff;
  89. }
  90. function IsFingerDown() : boolean
  91. {
  92.     return (lastFingerId != -1);
  93. }
  94. function LatchedFinger( fingerId : int )
  95. {
  96.     // If another joystick has latched this finger, then we must release it
  97.     if ( lastFingerId == fingerId )
  98.         ResetJoystick();
  99. }
  100. function Update()
  101. {
  102.         if ( !enumeratedJoysticks )
  103.         {
  104.             // Collect all joysticks in the game, so we can relay finger latching messages
  105.             joysticks = FindObjectsOfType(Joystick) as Joystick[];
  106.             enumeratedJoysticks = true;
  107.         }
  108.         var count = Input.touchCount;
  109.         // Adjust the tap time window while it still available
  110.         if ( tapTimeWindow > 0 )
  111.             tapTimeWindow -= Time.deltaTime;
  112.         else
  113.             tapCount = 0;
  114.         if ( count == 0 )
  115.             ResetJoystick();
  116.         else
  117.         {
  118.             for(var i : int = 0;i < count; i++)
  119.             {
  120.                 var touch : Touch = Input.GetTouch(i);
  121.                 var guiTouchPos : Vector2 = touch.position - guiTouchOffset;
  122.                 var shouldLatchFinger = false;
  123.                 if ( touchPad )
  124.                 {
  125.                     if ( touchZone.Contains( touch.position ) )
  126.                         shouldLatchFinger = true;
  127.                 }
  128.                 else if ( gui.HitTest( touch.position ) )
  129.                 {
  130.                     shouldLatchFinger = true;
  131.                     gui.color.a = .5;
  132.                 }
  133.                 // Latch the finger if this is a new touch
  134.                 if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
  135.                 {
  136.                     if ( touchPad )
  137.                     {
  138.                         //gui.color.a = 0.15;
  139.                         lastFingerId = touch.fingerId;
  140.                         fingerDownPos = touch.position;
  141.                         fingerDownTime = Time.time;
  142.                     }
  143.                     lastFingerId = touch.fingerId;
  144.                     // Accumulate taps if it is within the time window
  145.                     if ( tapTimeWindow > 0 )
  146.                         tapCount++;
  147.                     else
  148.                     {
  149.                         tapCount = 1;
  150.                         tapTimeWindow = tapTimeDelta;
  151.                     }
  152.                     // Tell other joysticks we've latched this finger
  153.                     for ( var j : Joystick in joysticks )
  154.                     {
  155.                         if ( j != this )
  156.                             j.LatchedFinger( touch.fingerId );
  157.                     }
  158.                 }
  159.                 if ( lastFingerId == touch.fingerId )
  160.                 {
  161.                     // Override the tap count with what the iPhone SDK reports if it is greater
  162.                     // This is a workaround, since the iPhone SDK does not currently track taps
  163.                     // for multiple touches
  164.                     if ( touch.tapCount > tapCount )
  165.                         tapCount = touch.tapCount;
  166.                     if ( touchPad )
  167.                     {
  168.                         // For a touchpad, let's just set the position directly based on distance from initial touchdown
  169.                         position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
  170.                         position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
  171.                     }
  172.                     else
  173.                     {
  174.                         // Change the location of the joystick graphic to match where the touch is
  175.                         gui.pixelInset.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
  176.                         gui.pixelInset.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );
  177.                     }
  178.                     if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled ) {
  179.                         ResetJoystick();
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.         if ( !touchPad )
  185.         {
  186.             // Get a value between -1 and 1 based on the joystick graphic location
  187.             position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
  188.             position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
  189.         }
  190.         // Adjust for dead zone
  191.         var absoluteX = Mathf.Abs( position.x );
  192.         var absoluteY = Mathf.Abs( position.y );
  193.         if ( absoluteX < deadZone.x )
  194.         {
  195.             // Report the joystick as being at the center if it is within the dead zone
  196.             position.x = 0;
  197.         }
  198.         else if ( normalize )
  199.         {
  200.             // Rescale the output after taking the dead zone into account
  201.             position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
  202.         }
  203.         if ( absoluteY < deadZone.y )
  204.         {
  205.             // Report the joystick as being at the center if it is within the dead zone
  206.             position.y = 0;
  207.         }
  208.         else if ( normalize )
  209.         {
  210.             // Rescale the output after taking the dead zone into account
  211.             position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
  212.         }
  213. }</font>
复制代码

      运行以下项目可以发现Joystick不见了:

       

     但是点击屏幕就会出现了:

       

1359015220_8065.jpg (45.21 KB, 下载次数: 0)

下载附件  保存到相册

9 小时前 上传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值