NGUI版虚拟摇杆

发表于2017-09-25
评论0 1.4k浏览

下面是我用nui实现的一个虚拟摇杆。

1,示图



2、代码如下,都有比较详细的注释,就不说明了。

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5.   
  6. [ExecuteInEditMode]  
  7. public class Joystick : MonoBehaviour  
  8. {      
  9.     #region Delegate & Event  
  10.     public delegate void JoystickEventHandler(Joystick joystick);  
  11.     /// <summary>  
  12.     /// 开如  
  13.     /// </summary>  
  14.     public static event JoystickEventHandler On_JoystickMoveStart;  
  15.     /// <summary>  
  16.     /// Occurs when the joystick move.  
  17.     /// </summary>  
  18.     public static event JoystickEventHandler On_JoystickMove;  
  19.     /// <summary>  
  20.     /// thumb偏离中心位置,并牌按住时,每帧的回调  
  21.     /// </summary>  
  22.     public static event JoystickEventHandler On_JoystickHolding;  
  23.     /// <summary>  
  24.     /// Occurs when the joystick stops move  
  25.     /// </summary>  
  26.     public static event JoystickEventHandler On_JoystickMoveEnd;  
  27.      
  28.     #endregion      
  29.  
  30.     #region   property  
  31.     [SerializeField] bool isRunInEditor = false;  
  32.     [SerializeField]private string joystickName = "NguiJoystick";  
  33.     public string JoystickName { get { return this.joystickName; } }  
  34.     [HideInInspector]private bool isLimitInCircle = true;  
  35.     public bool IsLimitInCircle { get { return this.isLimitInCircle; } }  
  36.     [SerializeField]private int radius = 100;  
  37.     public int Radius { getreturn this.radius; } }  
  38.   
  39.     [SerializeField]  
  40.     private float minAlpha = 0.3f;  
  41.     public float MinAlpha { get { return this.minAlpha; } }  
  42.   
  43.     private Vector2 joystickAxis = Vector2.zero;  
  44.     /// <summary>  
  45.     /// Gets the joystick axis value between -1 & 1...  
  46.     /// </summary>  
  47.     /// <value>  
  48.     /// The joystick axis.  
  49.     /// </value>  
  50.     public Vector2 JoystickAxis { get { return this.joystickAxis; } }  
  51.   
  52.     private Vector2 lastJoystickAxis = Vector2.zero;  
  53.     public Vector2 LastJoystickAxis { get { return this.lastJoystickAxis; } }  
  54.       
  55.     bool isForBid = false;  
  56.     /// <summary>  
  57.     /// 判断joystick是否被禁用  
  58.     /// </summary>  
  59.     public bool IsForBid { get { return this.isForBid; } }  
  60.     bool isHolding = false;  
  61.     public bool IsHolding { get { return this.isHolding; } }  
  62.     #endregion  
  63.   
  64.     UIWidget root;  
  65.     [SerializeField]UISprite bg;  
  66.     [SerializeField]UISprite thumb;  
  67.   
  68.     void Awake()  
  69.     {  
  70.         this.name = this.JoystickName;  
  71.         root = this.GetComponent<UIWidget>();  
  72.         Init();  
  73.     }  
  74.       
  75.       
  76.     // Update is called once per frame     
  77.     void Update ()  
  78.     {  
  79.         if (isRunInEditor && Application.isEditor && !Application.isPlaying)  
  80.         {  
  81.             SetJoystickSize(radius);  
  82.         }  
  83.   
  84.         if (!isForBid && isHolding)  
  85.         {  
  86.             Debug.Log("111111");  
  87.             if (On_JoystickHolding != null)  
  88.             {  
  89.                 On_JoystickHolding(this);  
  90.             }  
  91.         }  
  92.     }  
  93.   
  94.     void Init()  
  95.     {  
  96.         bg.transform.localPosition = Vector3.zero;  
  97.         thumb.transform.localPosition = Vector3.zero;  
  98.         SetJoystickSize(radius);  
  99.         Lighting(minAlpha);  
  100.     }  
  101.  
  102.     #region ngui event  
  103.     ///// <summary>  
  104.     ///// test  
  105.     ///// </summary>  
  106.     //void OnClick ()  
  107.     //{  
  108.     //    Debug.Log("mouse pos :"   Input.mousePosition   " -- touch pos :"   ScreenPos_to_NGUIPos(Input.mousePosition));  
  109.     //    thumb.transform.localPosition = ScreenPos_to_NGUIPos(Input.mousePosition);  
  110.     //}  
  111.     void OnPress (bool isPressed)  
  112.     {  
  113.         if (isForBid)  
  114.         {  
  115.             Debug.Log("joystick is forbid!");  
  116.             return;  
  117.         }  
  118.         Debug.Log("OnPress:"   isPressed.ToString());  
  119.         if(isPressed)  
  120.         {  
  121.             Lighting(1f);  
  122.             CalculateJoystickAxis();  
  123.             if (On_JoystickMoveStart != null)  
  124.             {  
  125.                 On_JoystickMoveStart(this);  
  126.             }  
  127.             isHolding = true;  
  128.         }  
  129.         else  
  130.         {  
  131.             CalculateJoystickAxis();  
  132.             if (On_JoystickMoveEnd != null)  
  133.             {  
  134.                 On_JoystickMoveEnd(this);  
  135.             }  
  136.             thumb.transform.localPosition = Vector3.zero;  
  137.             FadeOut(1f, minAlpha);  
  138.             isHolding = false;  
  139.         }  
  140.     }  
  141.   
  142.     //void OnDragStart ()  
  143.     //{  
  144.     //    if (isForBid)  
  145.     //    {  
  146.     //        Debug.Log("joystick is forbid!");  
  147.     //        return;  
  148.     //    }  
  149.   
  150.     //    Debug.Log("OnDragStart");  
  151.     //    Lighting(1f);  
  152.     //    CalculateJoystickAxis();  
  153.     //    if(On_JoystickMoveStart!=null)  
  154.     //    {  
  155.     //        On_JoystickMoveStart(this);  
  156.     //    }  
  157.     //    isHolding = true;  
  158.     //    Debug.Log(string.Format("time:{0} - axis:{1}", Time.time, joystickAxis));  
  159.     //}  
  160.   
  161.     void OnDrag(Vector2 delta)  
  162.     {  
  163.         if (isForBid)  
  164.         {  
  165.             return;  
  166.         }  
  167.   
  168.         //Debug.Log("OnDrag:" delta.ToString());          
  169.         CalculateJoystickAxis();  
  170.         if (On_JoystickMoveStart != null)  
  171.         {  
  172.             On_JoystickMoveStart(this);  
  173.         }  
  174.     }  
  175.   
  176.   
  177.     //void OnDragEnd ()  
  178.     //{  
  179.     //    if (isForBid)  
  180.     //    {  
  181.     //        return;  
  182.     //    }  
  183.   
  184.     //    Debug.Log("OnDragEnd");               
  185.     //    CalculateJoystickAxis();  
  186.     //    if (On_JoystickMoveEnd != null)  
  187.     //    {  
  188.     //        On_JoystickMoveEnd(this);  
  189.     //    }  
  190.     //    thumb.transform.localPosition = Vector3.zero;  
  191.     //    FadeOut(1f, minAlpha);  
  192.     //    isHolding = false;  
  193.     //}  
  194.     #endregion  
  195.  
  196.     #region utile  
  197.   
  198.     /// <summary>  
  199.     /// 计算JoystickAxis  
  200.     /// </summary>  
  201.     /// <returns></returns>  
  202.     void CalculateJoystickAxis()  
  203.     {  
  204.         Vector3 offset = ScreenPos_to_NGUIPos(UICamera.currentTouch.pos);  
  205.         offset -= transform.localPosition;  
  206.         if (isLimitInCircle)  
  207.         {  
  208.             if (offset.magnitude > radius)  
  209.             {  
  210.                 offset = offset.normalized * radius;  
  211.             }  
  212.         }  
  213.         thumb.transform.localPosition = offset;  
  214.   
  215.         lastJoystickAxis = joystickAxis;  
  216.         joystickAxis = new Vector2(offset.x / radius, offset.y / radius);  
  217.     }  
  218.   
  219.     /// <summary>  
  220.     /// Axis2s the angle.  
  221.     /// </summary>  
  222.     /// <returns>  
  223.     /// The angle.  
  224.     /// </returns>  
  225.     public float Axis2Angle(bool inDegree = true)  
  226.     {  
  227.         float angle = Mathf.Atan2(joystickAxis.x, joystickAxis.y);  
  228.   
  229.         if (inDegree)  
  230.         {  
  231.             return angle * Mathf.Rad2Deg;  
  232.         }  
  233.         else  
  234.         {  
  235.             return angle;  
  236.         }  
  237.     }  
  238.   
  239.     /// <summary>  
  240.     /// Axis2s the angle.  
  241.     /// </summary>  
  242.     /// <returns>  
  243.     /// The angle.  
  244.     /// </returns>  
  245.     public float Axis2Angle(Vector2 axis, bool inDegree = true)  
  246.     {  
  247.         float angle = Mathf.Atan2(axis.x, axis.y);  
  248.   
  249.         if (inDegree)  
  250.         {  
  251.             return angle * Mathf.Rad2Deg;  
  252.         }  
  253.         else  
  254.         {  
  255.             return angle;  
  256.         }  
  257.     }  
  258.   
  259.   
  260.   
  261.     /// <summary>  
  262.     /// 屏幕坐标-->ui坐标  
  263.     /// </summary>  
  264.     /// <param name="screenPos"></param>  
  265.     /// <returns></returns>  
  266.     Vector3 ScreenPos_to_NGUIPos(Vector3 screenPos)  
  267.     {  
  268.         Vector3 uiPos = UICamera.currentCamera.ScreenToWorldPoint(screenPos);  
  269.         uiPos = UICamera.currentCamera.transform.InverseTransformPoint(uiPos);  
  270.         return uiPos;  
  271.     }  
  272.   
  273.     /// <summary>  
  274.     /// 屏幕坐标-->ngui坐标  
  275.     /// </summary>  
  276.     /// <param name="screenPos"></param>  
  277.     /// <returns></returns>  
  278.     Vector3 ScreenPos_to_NGUIPos(Vector2 screenPos)  
  279.     {  
  280.         return ScreenPos_to_NGUIPos(new Vector3(screenPos.x, screenPos.y, 0f));  
  281.     }  
  282.   
  283.     /// <summary>  
  284.     /// 设置摇杆的大小  
  285.     /// </summary>  
  286.     /// <param name="radius"></param>  
  287.     void SetJoystickSize(int radius)  
  288.     {  
  289.         root.width = 2 * radius;  
  290.         root.height = 2 * radius;  
  291.         thumb.width = (int)(40f / 100f * root.width);  
  292.         thumb.height = (int)(40f / 100f * root.height);  
  293.     }  
  294.   
  295.     /// <summary>  
  296.     /// 点亮摇杆  
  297.     /// </summary>  
  298.     void Lighting(float alpha)  
  299.     {  
  300.         iTween.Stop(this.gameObject, "value");  
  301.         root.alpha = alpha;  
  302.     }  
  303.   
  304.     /// <summary>  
  305.     /// 渐变摇杆的透明度  
  306.     /// </summary>  
  307.     void FadeOut(float fromAlpha, float toAlpha)  
  308.     {  
  309.         Hashtable itweenArgs = new Hashtable();  
  310.         itweenArgs.Add("easetype", iTween.EaseType.linear);  
  311.         itweenArgs.Add("from", fromAlpha);  
  312.         itweenArgs.Add("to", toAlpha);         
  313.         itweenArgs.Add("time", 0.5f);  
  314.         itweenArgs.Add("onupdate""OnFadeOutTween");  
  315.         iTween.ValueTo(this.gameObject, itweenArgs);  
  316.     }  
  317.     void OnFadeOutTween(float value)  
  318.     {  
  319.         root.alpha = value;  
  320.     }  
  321.  
  322.     #endregion  
  323.  
  324.     #region 激活、禁用的控制  
  325.     List<string> keys = new List<string>();  
  326.       
  327.    /// <summary>  
  328.     /// 禁用  
  329.    /// </summary>  
  330.    /// <returns>返回值是,取消这个禁用要用到的key</returns>  
  331.     public string ForbidJosystick()  
  332.     {  
  333.         string key = System.Guid.NewGuid().ToString();  
  334.         keys.Add(key);  
  335.         isForBid = true;  
  336.         return key;  
  337.     }  
  338.   
  339.     /// <summary>  
  340.     /// 启用  
  341.     /// </summary>  
  342.     /// <param name="key"></param>  
  343.     public void ActivizeJosystick(string key)  
  344.     {  
  345.         if(keys.Contains(key))  
  346.         {  
  347.             keys.Remove(key);  
  348.         }  
  349.   
  350.         isForBid = true;  
  351.         if(keys.Count==0)  
  352.         {  
  353.             isForBid = false;  
  354.         }  
  355.     }  
  356.  
  357.     #endregion  
  358. }  
3、demo包,有兴趣的,也可以看看。

        下载:

http://blog.csdn.net/anyuanlzh/article/details/40107577

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引