原始的:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NGUIJoystick : MonoBehaviour
  5. {
  6. public float radius = 2.0f;
  7. public Vector3 scale = Vector3.one;
  8. private Plane mPlane;
  9. private Vector3 mLastPos;
  10. private UIPanel mPanel;
  11. private Vector3 center;
  12. [HideInInspector]
  13. public Vector2 position;
  14.  
  15. private void Start ()
  16. {
  17. center = transform.localPosition;
  18. }
  19.  
  20. /// <summary>
  21. /// Create a plane on which we will be performing the dragging.
  22. /// </summary>
  23.  
  24. private void OnPress (bool pressed)
  25. {
  26. if (enabled && gameObject.activeInHierarchy) {
  27. if (pressed) {
  28. mLastPos = UICamera.lastHit.point;
  29. mPlane = new Plane (Vector3.back, mLastPos);
  30. } else {
  31. transform.localPosition = center;
  32. position=Vector2.zero;
  33. }
  34. }
  35. }
  36.  
  37. /// <summary>
  38. /// Drag the object along the plane.
  39. /// </summary>
  40.  
  41. void OnDrag (Vector2 delta)
  42. {
  43. if (enabled && gameObject.activeInHierarchy) {
  44. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  45.  
  46. Ray ray = UICamera.currentCamera.ScreenPointToRay (UICamera.currentTouch.pos);
  47. float dist = 0f;
  48.  
  49. if (mPlane.Raycast (ray, out dist)) {
  50. Vector3 currentPos = ray.GetPoint (dist);
  51. Vector3 offset = currentPos - mLastPos;
  52. mLastPos = currentPos;
  53.  
  54. if (offset.x != 0f || offset.y != 0f) {
  55. offset = transform.InverseTransformDirection (offset);
  56. offset.Scale (scale);
  57. offset = transform.TransformDirection (offset);
  58. }
  59.  
  60. offset.z = ;
  61. transform.position += offset;
  62.  
  63. float length = transform.localPosition.magnitude;
  64.  
  65. if (length > radius) {
  66. transform.localPosition = Vector3.ClampMagnitude (transform.localPosition, radius);
  67. }
  68.  
  69. position = new Vector2((transform.localPosition.x-center.x)/radius,(transform.localPosition.y-center.y)/radius);
  70. }
  71. }
  72. }
  73.  
  74. }

超凡特工的:

  1. using System;
  2. using UnityEngine;
  3.  
  4. public class NGUIJoystick : MonoBehaviour
  5. {
  6. public bool activeFlag;
  7. public Vector3 center;
  8. private float deadRadius;
  9. public float deadRadiusRatio = 0.3f;
  10. public bool lastPressed;
  11. protected UIPanel mPanel;
  12. protected Plane mPlane;
  13. public Vector2 normalizedPosition;
  14. public float normalizedRadius = 2f;
  15. public Vector2 position;
  16. public float radius = 2f;
  17. public Vector3 worldCenter;
  18.  
  19. internal void Move(Vector3 offset)
  20. {
  21. offset.z = 0f;
  22. base.transform.position = this.worldCenter + offset;
  23. float magnitude = base.transform.localPosition.magnitude;
  24. if (magnitude < this.deadRadius)
  25. {
  26. this.normalizedPosition = Vector2.zero;
  27. }
  28. else
  29. {
  30. if (magnitude > this.radius)
  31. {
  32. base.transform.localPosition = Vector3.ClampMagnitude(base.transform.localPosition, this.radius);
  33. }
  34. float num2 = base.transform.localPosition.x - this.center.x;
  35. float num3 = base.transform.localPosition.y - this.center.y;
  36. this.position.x = num2 / this.radius;
  37. this.position.y = num3 / this.radius;
  38. this.normalizedPosition.x = num2 / this.normalizedRadius;
  39. this.normalizedPosition.y = num3 / this.normalizedRadius;
  40. this.normalizedPosition.x = Mathf.Clamp(this.normalizedPosition.x, -1f, 1f);
  41. this.normalizedPosition.y = Mathf.Clamp(this.normalizedPosition.y, -1f, 1f);
  42. }
  43. }
  44.  
  45. internal virtual void OnDrag(Vector2 delta)
  46. {
  47. if (base.enabled && base.gameObject.activeInHierarchy)
  48. {
  49. Ray ray = UICamera.currentCamera.ScreenPointToRay((Vector3) UICamera.currentTouch.pos);
  50. float enter = 0f;
  51. if (this.mPlane.Raycast(ray, out enter))
  52. {
  53. Vector3 offset = ray.GetPoint(enter) - this.worldCenter;
  54. this.Move(offset);
  55. }
  56. }
  57. }
  58.  
  59. internal virtual void OnPress(bool pressed)
  60. {
  61. if (base.enabled && base.gameObject.activeInHierarchy)
  62. {
  63. if (pressed)
  64. {
  65. this.mPlane = new Plane(Vector3.back, UICamera.lastHit.point);
  66. }
  67. else
  68. {
  69. base.transform.localPosition = this.center;
  70. this.position = Vector2.zero;
  71. this.activeFlag = true;
  72. }
  73. this.lastPressed = pressed;
  74. }
  75. }
  76.  
  77. public void SetRadius(float r, float nr)
  78. {
  79. this.radius = r;
  80. this.normalizedRadius = nr;
  81. this.deadRadius = this.radius * this.deadRadiusRatio;
  82. }
  83.  
  84. internal virtual void Start()
  85. {
  86. this.center = base.transform.localPosition;
  87. this.worldCenter = base.transform.position;
  88. }
  89. }
  1. namespace Kola
  2. {
  3. using LuaInterface;
  4. using System;
  5. using UnityEngine;
  6.  
  7. public class KJoystick : NGUIJoystick
  8. {
  9. public UIRect bgObject;
  10. public bool FollowPressPosition;
  11. private Vector3 originPos;
  12. private Transform parent;
  13. public LuaTable playerControl;
  14. public LuaFunction playerControlSetInputFunc;
  15. public KMoveBehavior playerMove;
  16. public float pressAlpha = 0.5f;
  17.  
  18. private void _OnDrag(GameObject go, Vector2 delta)
  19. {
  20. this.OnDrag(delta);
  21. }
  22.  
  23. private void _OnPress(GameObject go, bool pressed)
  24. {
  25. this.OnPress(pressed);
  26. if (pressed)
  27. {
  28. Vector3 point = UICamera.lastHit.point;
  29. if (this.FollowPressPosition)
  30. {
  31. this.parent.position = point;
  32. }
  33. else
  34. {
  35. Vector3 position = this.parent.position;
  36. Vector3 offset = point - position;
  37. base.Move(offset);
  38. this.OnDrag(Vector2.zero);
  39. }
  40. if (this.bgObject != null)
  41. {
  42. this.bgObject.alpha = this.pressAlpha;
  43. }
  44. }
  45. else
  46. {
  47. if (this.playerMove != null)
  48. {
  49. this.playerMove.Stand();
  50. }
  51. if (this.bgObject != null)
  52. {
  53. this.bgObject.alpha = 1f;
  54. }
  55. if (this.playerControlSetInputFunc != null)
  56. {
  57. object[] args = new object[] { this.playerControl, 0f, 0f, true };
  58. KLuaFunc.Call(this.playerControlSetInputFunc, args);
  59. }
  60. if (this.FollowPressPosition)
  61. {
  62. this.parent.position = this.originPos;
  63. }
  64. }
  65. }
  66.  
  67. public void Attach(GameObject draggedObj, GameObject bgObj)
  68. {
  69. UIEventListener listener1 = UIEventListener.Get(draggedObj);
  70. listener1.onPress = (UIEventListener.BoolDelegate) Delegate.Combine(listener1.onPress, new UIEventListener.BoolDelegate(this._OnPress));
  71. UIEventListener listener2 = UIEventListener.Get(draggedObj);
  72. listener2.onDrag = (UIEventListener.VectorDelegate) Delegate.Combine(listener2.onDrag, new UIEventListener.VectorDelegate(this._OnDrag));
  73. this.bgObject = (bgObj != null) ? bgObj.GetComponent<UIRect>() : null;
  74. }
  75.  
  76. private void OnDisable()
  77. {
  78. this._OnPress(base.gameObject, false);
  79. }
  80.  
  81. internal override void OnDrag(Vector2 delta)
  82. {
  83. base.OnDrag(delta);
  84. if ((this.playerMove != null) && (base.enabled && base.gameObject.activeInHierarchy))
  85. {
  86. if (this.playerControlSetInputFunc != null)
  87. {
  88. object[] args = new object[] { this.playerControl, this.normalizedPosition.x, this.normalizedPosition.y, false };
  89. KLuaFunc.Call(this.playerControlSetInputFunc, args);
  90. }
  91. else
  92. {
  93. this.playerMove.Move(this.normalizedPosition.x, this.normalizedPosition.y);
  94. }
  95. }
  96. }
  97.  
  98. public void OnDrawGizmos()
  99. {
  100. Gizmos.color = Color.green;
  101. Vector3 to = base.transform.position + ((Vector3) (new Vector3(this.position.x, this.position.y, 0f) * 100f));
  102. Gizmos.DrawLine(base.transform.position, to);
  103. }
  104.  
  105. public void SetPlayer(GameObject obj)
  106. {
  107. this.playerMove = obj.GetComponent<KMoveBehavior>();
  108. }
  109.  
  110. public void SetPlayerControl(LuaTable control)
  111. {
  112. this.playerControl = control;
  113. this.playerControlSetInputFunc = KLuaBehavior.GetFunction(control, "SetJoyStickInput");
  114. }
  115.  
  116. internal override void Start()
  117. {
  118. base.Start();
  119. this.parent = base.transform.parent;
  120. this.originPos = this.parent.position;
  121. }
  122. }
  123. }

NGUIJoysticK的更多相关文章

  1. 【转】NGUI版虚拟摇杆

    http://blog.csdn.net/anyuanlzh/article/details/40107577 下面是我用nui实现的一个虚拟摇杆. 1,示图 2.代码如下,都有比较详细的注释,就不说 ...

  2. NGUI版虚拟摇杆

    以下是我用nui实现的一个虚拟摇杆. 1,示图 2.代码例如以下,都有比較具体的凝视.就不说明了. using UnityEngine; using System.Collections; using ...

  3. Unity基于NGUI的简单并可直接使用的虚拟摇杆实现(一)

    可能大家都听说过大名鼎鼎的easytouch,然而easytouch是基于UGUI的,两种不同的UI混用,可能会造成项目管理的混乱,并且可能会出现各种幺蛾子,比如事件传递互相扰乱的问题. 于是就想找一 ...

随机推荐

  1. 记录我开始学习 Git的路程

    工作半年多了,总觉得没学到什么东西,于是乎找了个Git学习一下,感觉还蛮厉害的样子.为此记录下我的路程 2015,11,26 更新 前面的路都挺艰难的,在官网下载msysgit网速几乎为0(心情千万只 ...

  2. Java问题:Quartz,Hibernate,Spring,Tomcat中定时任务无故停止,没有错误

    最近在做一个java项目的时候遇到一个十分奇怪的问题,想到大家可能也会遇到这样的问题,所以在此发出来,希望大家遇到的时候能够快速解决! 直入主题 问题:使用quartz进行定时任务自动执行的时候,用到 ...

  3. Linq之Expression进阶

    目录 写在前面 系列文章 表达式树解析 表达式树特性 编译表达树 总结 写在前面 让我们首先简单回顾一下上篇文章介绍的内容,上篇文章介绍了表达式树的基本概念(表达式树又称为“表达式目录树”,以数据形式 ...

  4. 软工实践练习一(个人)----将Androidstudio的项目共享到github

    在Androidstudio上使用git插件 将项目共享至github 将 显示共享成功但是出了点问题 项目文件并没有上传至github库中,而是只创建了一个新的库 问题在于我的gitforwindo ...

  5. Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]

    Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...

  6. oracle 的安装脚本

    ==[root@oracle ~]# cat 1.preusers.sh ==#!/bin/bash#Purpose:Create 3 groups named 'oinstall','dba','o ...

  7. CXF 自定义拦截器

    此例子来自apache cxf sample. /**  * Licensed to the Apache Software Foundation (ASF) under one  * or more ...

  8. Shell重定向&>file、2>&1、1>&2的区别

    shell上: 0表示标准输入 1表示标准输出 2表示标准错误输出 > 默认为标准输出重定向,与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. & ...

  9. 【前端】Sublime text3 插件HTML/CSS/JS prettify 格式化代码

    1.首先安装插件 菜单的preference->packages control,然后输入install .. 回车,再输入HTML/CSS/JS prettify 再回车,重启后就可以了. 2 ...

  10. JAVA的整型与字符串相互转换

    1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([S ...