UGUI摇杆

  1. 首先在unity场景里面新建一个空物体和两个Image,把空物体放在创建Image自动生成的Canvas里面,再把两个Image放在空物体里

    Image建立方式:GameObject->UI->Image   或者直接在“Hierarchy”右键然后UI->Image,看喜好。

    空物体和两个Image的命名看个人喜好。

    威恩的节点是这样的:

    节点中的joystack是刚刚建立的空节点。

    Backgound是摇杆的背景。

    JoystackControl是真实的可以拖动的摇杆。

  2. 把Backgound和JoystackControl的SourceImage替换成自己喜欢的图片,并且把JoystackControl的图片缩小点,这里我就用系统自带的图片了,威恩这两个节点的inspector如下(我修改过得地方用红框标注了,其他都没改):

    最终样子如下:

  3. 那么样子有了就需要让他动起来,需要三个类“EventTriggerListener”、“JoystackCc”、“PlayerMoveControl”。


    EventTriggerListener:在NGUI开发的时候处理事件都会用到UIEventListener,我们已经用的习惯的不得了,而UGUI则不是这种机制,

    并且我觉得这种是最合理的方式,所以自己写一套类似的。

    只是一个帮助类,不需要挂在任何的游戏对象上。

    JoystackCc:这是主要来控制摇杆的。

    挂在JoystackControl节点上

    PlayerMoveControl:这是主要来通过摇杆来控制角色的。

    挂在你想控制的物体上


    直接上代码,写了注释,就不哔哔了。

    EventTriggerListener.cs

  4. [csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1.   
    2.       
    3.       
    4.   
    5. using UnityEngine;  
    6.   
    7. using System.Collections;  
    8.   
    9. using UnityEngine.EventSystems;  
    10.   
    11. using System.Collections.Generic;  
    12.   
    13.   
    14. /// <summary>  
    15.   
    16. /// UGUI事件监听类  
    17.   
    18. /// </summary>  
    19.   
    20. public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{  
    21.       
    22.     publicdelegate void VoidDelegate (GameObject go);  
    23.       
    24.     publicdelegate void VectorDelegate(GameObject go, Vector2 delta);  
    25.       
    26.     publicVoidDelegate onClick;  
    27.       
    28.     publicVoidDelegate onDown;  
    29.       
    30.     publicVoidDelegate onEnter;  
    31.       
    32.     publicVoidDelegate onExit;  
    33.       
    34.     publicVoidDelegate onUp;  
    35.       
    36.     publicVoidDelegate onSelect;  
    37.       
    38.     publicVoidDelegate onUpdateSelect;  
    39.       
    40.       
    41.       
    42.     publicVectorDelegate onDrag;  
    43.       
    44.     publicVoidDelegate onDragOut;  
    45.       
    46.       
    47.       
    48.       
    49.       
    50.     staticpublic EventTriggerListener Get (GameObject go)  
    51.           
    52.     {  
    53.           
    54.         if(go==null){  
    55.               
    56.             Debug.LogError("EventTriggerListener_go_is_NULL");  
    57.               
    58.             return null;  
    59.               
    60.         }  
    61.           
    62.         else{  
    63.               
    64.             EventTriggerListener listener = go.GetComponent<EventTriggerListener>();  
    65.               
    66.             if (listener == null) listener = go.AddComponent<EventTriggerListener>();  
    67.               
    68.             return listener;  
    69.               
    70.         }  
    71.           
    72.     }  
    73.       
    74.       
    75.       
    76.     publicoverride void OnDrag(PointerEventData eventData)  
    77.           
    78.     {  
    79.           
    80.         if(onDrag != null) onDrag(gameObject, eventData.delta);  
    81.           
    82.     }  
    83.       
    84.       
    85.       
    86.     publicoverride void OnEndDrag(PointerEventData eventData)  
    87.           
    88.     {  
    89.           
    90.         if(onDragOut != null) onDragOut(gameObject);  
    91.           
    92.     }  
    93.       
    94.       
    95.       
    96.     publicoverride void OnPointerClick(PointerEventData eventData)  
    97.           
    98.     {  
    99.           
    100.         if(onClick !=null)  onClick(gameObject);  
    101.           
    102.     }  
    103.       
    104.     publicoverride void OnPointerDown (PointerEventData eventData){  
    105.           
    106.         if(onDown !=null) onDown(gameObject);  
    107.           
    108.     }  
    109.       
    110.     publicoverride void OnPointerEnter (PointerEventData eventData){  
    111.           
    112.         if(onEnter !=null) onEnter(gameObject);  
    113.           
    114.     }  
    115.       
    116.     publicoverride void OnPointerExit (PointerEventData eventData){  
    117.           
    118.         if(onExit !=null) onExit(gameObject);  
    119.           
    120.     }  
    121.       
    122.     publicoverride void OnPointerUp (PointerEventData eventData){  
    123.           
    124.         if(onUp !=null) onUp(gameObject);  
    125.           
    126.     }  
    127.       
    128.     publicoverride void OnSelect (BaseEventData eventData){  
    129.           
    130.         if(onSelect !=null) onSelect(gameObject);  
    131.           
    132.     }  
    133.       
    134.     publicoverride void OnUpdateSelected (BaseEventData eventData){  
    135.           
    136.         if(onUpdateSelect !=null) onUpdateSelect(gameObject);  
    137.           
    138.     }  
    139.       
    140. }   


     

     
     

     

  5. JoystackCc.cs

  6. [csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. using UnityEngine;  
    2.   
    3. using System.Collections;  
    4.   
    5.   
    6. public class JoystackCc : MonoBehaviour {  
    7.       
    8.     privateVector3 Origin;  
    9.       
    10.       
    11.     Transform mTrans;  
    12.       
    13.       
    14.     privateVector3 _deltaPos;  
    15.       
    16.     privatebool _drag = false;  
    17.       
    18.       
    19.     privateVector3 deltaPosition;  
    20.       
    21.       
    22.     floatdis;  
    23.       
    24.     [SerializeField]  
    25.       
    26.     privatefloat MoveMaxDistance = 80;            //最大拖动距离  
    27.       
    28.       
    29.     [HideInInspector]  
    30.       
    31.     publicVector3 FiexdMovePosiNorm; //固定8个角度移动的距离  
    32.       
    33.       
    34.     [HideInInspector]  
    35.       
    36.     publicVector3 MovePosiNorm;  //标准化移动的距离  
    37.       
    38.     [SerializeField]  
    39.       
    40.     privatefloat ActiveMoveDistance = 1;              //激活移动的最低距离  
    41.       
    42.     voidAwake()  
    43.           
    44.     {  
    45.           
    46.         EventTriggerListener.Get(gameObject).onDrag = OnDrag;  
    47.           
    48.         EventTriggerListener.Get(gameObject).onDragOut = OnDragOut;  
    49.           
    50.           
    51.         EventTriggerListener.Get(gameObject).onDown = OnMoveStart;  
    52.           
    53.     }  
    54.       
    55.       
    56.       
    57.     // Use this for initialization  
    58.       
    59.     voidStart () {  
    60.           
    61.         Origin = transform.localPosition;//设置原点  
    62.           
    63.         mTrans = transform;  
    64.           
    65.     }  
    66.       
    67.       
    68.       
    69.     // Update is called once per frame  
    70.       
    71.     voidUpdate()  
    72.           
    73.     {  
    74.           
    75.         dis = Vector3.Distance(transform.localPosition, Origin);//拖动距离,这不是最大的拖动距离,是根据触摸位置算出来的  
    76.           
    77.         if(dis >= MoveMaxDistance)       //如果大于可拖动的最大距离  
    78.               
    79.         {  
    80.               
    81.             Vector3 vec = Origin + (transform.localPosition - Origin) * MoveMaxDistance / dis;  //求圆上的一点:(目标点-原点) * 半径/原点到目标点的距离  
    82.               
    83.             transform.localPosition = vec;  
    84.               
    85.         }  
    86.           
    87.         if(Vector3.Distance(transform.localPosition, Origin) > ActiveMoveDistance) //距离大于激活移动的距离  
    88.               
    89.         {  
    90.               
    91.             MovePosiNorm = (transform.localPosition - Origin).normalized;  
    92.               
    93.             MovePosiNorm = new Vector3(MovePosiNorm.x, 0, MovePosiNorm.y);  
    94.               
    95.         }  
    96.           
    97.         else  
    98.               
    99.             MovePosiNorm = Vector3.zero;  
    100.           
    101.     }  
    102.       
    103.     voidMiouseDown()  
    104.           
    105.     {  
    106.           
    107.         if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved))  
    108.               
    109.         {  
    110.               
    111.         }  
    112.           
    113.         else  
    114.               
    115.             mTrans.localPosition = Origin;  
    116.           
    117.     }  
    118.       
    119.     Vector3 result;  
    120.       
    121.     privateVector3 _checkPosition(Vector3 movePos, Vector3 _offsetPos)  
    122.           
    123.     {  
    124.           
    125.         result = movePos + _offsetPos;  
    126.           
    127.         returnresult;  
    128.           
    129.     }  
    130.       
    131.       
    132.     voidOnDrag(GameObject go, Vector2 delta)  
    133.           
    134.     {  
    135.           
    136.         if(!_drag)  
    137.               
    138.         {  
    139.               
    140.             _drag = true;  
    141.               
    142.         }  
    143.           
    144.         _deltaPos = delta;  
    145.           
    146.           
    147.         mTrans.localPosition +=new Vector3(_deltaPos.x, _deltaPos.y, 0);  
    148.           
    149.     }  
    150.       
    151.       
    152.     voidOnDragOut(GameObject go)  
    153.           
    154.     {  
    155.           
    156.         _drag =false;  
    157.           
    158.         mTrans.localPosition = Origin;  
    159.           
    160.         if(PlayerMoveControl.moveEnd != null) PlayerMoveControl.moveEnd();  
    161.           
    162.     }  
    163.       
    164.       
    165.     voidOnMoveStart(GameObject go)  
    166.           
    167.     {  
    168.           
    169.         if(PlayerMoveControl.moveStart != null) PlayerMoveControl.moveStart();  
    170.           
    171.     }  
    172.       
    173. }  


     

  7.  
     

    PlayerMoveControl.cs

  8. [csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. using UnityEngine;  
    2.   
    3. using System.Collections;  
    4.   
    5.   
    6. public class PlayerMoveControl : MonoBehaviour {  
    7.       
    8.     privateTransform _mTransform;  
    9.       
    10.     publicJoystackCc _mJoystackCc;  
    11.       
    12.       
    13.     publicfloat moveSpeed = 50;  
    14.       
    15.       
    16.     publicdelegate void MoveDelegate();  
    17.       
    18.     publicstatic MoveDelegate moveEnd;  
    19.       
    20.     publicstatic MoveDelegate moveStart;  
    21.       
    22.     publicstatic PlayerMoveControl Instance;  
    23.       
    24.     // Use this for initialization  
    25.       
    26.     voidAwake()  
    27.           
    28.     {  
    29.           
    30.         Instance =this;  
    31.           
    32.         _mTransform = transform;  
    33.           
    34.           
    35.         moveEnd = OnMoveEnd;  
    36.           
    37.         moveStart = OnMoveStart;  
    38.           
    39.     }  
    40.       
    41.     voidStart () {  
    42.           
    43.           
    44.     }  
    45.       
    46.     voidOnMoveEnd()  
    47.           
    48.     {  
    49.           
    50.         _turnBase =false;  
    51.           
    52.     }  
    53.       
    54.       
    55.     voidOnMoveStart()  
    56.           
    57.     {  
    58.           
    59.         _turnBase =true;  
    60.           
    61.     }  
    62.       
    63.       
    64.       
    65.     // Update is called once per frame  
    66.       
    67.     privatefloat angle;  
    68.       
    69.     privatebool _turnBase = false;  
    70.       
    71.     voidUpdate()  
    72.           
    73.     {  
    74.           
    75.         if(_turnBase)   
    76.               
    77.         {  
    78.               
    79.             Vector3 vecMove = _mJoystackCc.MovePosiNorm*Time.deltaTime*moveSpeed/10;  
    80.               
    81.             _mTransform.localPosition+=vecMove;  
    82.               
    83.             angle = Mathf.Atan2 (_mJoystackCc.MovePosiNorm.x, _mJoystackCc.MovePosiNorm.z) * Mathf.Rad2Deg - 10;  
    84.               
    85.             _mTransform.localRotation = Quaternion.Euler(Vector3.up*angle);  
    86.               
    87.         }  
    88.           
    89.     }  
    90.       
    91. }  


     

     
     

    建好这三个类之后,把他们绑定到相应的节点上。都挂在哪,代码上面有写

  9. 测试一下,威恩新建了一个cube来作为测试对象,加了个plane作为“伪”地面,太黑了再打个灯….

    下面是测试效果:



    原文: http://blog.csdn.net/onafioo/article/details/46403801

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值