zoukankan      html  css  js  c++  java
  • 【转】UGUI实现unity摇杆

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

    好久没有写文章了,最近在做项目是用的unity最新的ui系统UGUI,项目需要做一个摇杆,网上大部分都是用的插件和NGUI做的摇杆,unity自带的摇杆也不怎么好用,而最新的unity4.6.x来了,加入了最新的UI系统“UGUI”,那我们怎么用UGUI来制作摇杆呢~大神勿喷,本人是技术渣渣。

    比较出色的摇杆插件《Easy Touch》(很强大)

    还是主要讲讲我们自己怎么做。

    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作为“伪”地面,太黑了再打个灯….

      下面是测试效果:

    如果有任何问题直接留言~尽自己所能帮忙解决~

     
     
  • 相关阅读:
    修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
    C# 正则表达式类 Match类和Group类
    JS流程控制语句 退出循环break 在while、for、do...while、while循环中使用break语句退出当前循环,直接执行后面的代码。
    JS流程控制语句 重复重复(for循环)语句结构: for(初始化变量;循环条件;循环迭代) { 循环语句 }
    JS流程控制语句 反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足。
    JS流程控制语句 多重判断满足你各种需求 要在多组语句中选择一组来执行,使用if..else嵌套语句。
    JS流程控制语句 多种选择(Switch语句) 当有很多种选项的时候,switch比if else使用更方便。
    JS流程控制语句 做判断(if语句)if语句是基于条件成立才执行相应代码时使用的语句。语法:if(条件) { 条件成立时执行代码}
    JS流程控制语句 二选一 (if...else语句) 语法: if(条件) { 条件成立时执行的代码} else {条件不成立时执行的代码}
    JS数组 编程练习 使用Javascript语言,把以下数组 在页面显示如下图所示的图案
  • 原文地址:https://www.cnblogs.com/mimime/p/6533288.html
Copyright © 2011-2022 走看看