当前位置:   article > 正文

Unity + 雷达功能的实现_unity touchscripts雷达地面互动

unity touchscripts雷达地面互动

下载带有 TUIO 协议的 touchScript 插件,

这把博客只是为了让那些 要实现雷达交互的人入个门,给你们一个思路,毕竟现在感觉写这个雷达交互的博客好像压根没有,我也是大佬带着才能入门的,感谢我家老大,哈哈哈

OK,我们来用 Unity 实现雷达的交互

第一种方式:新建一个空物体,挂上下面两个脚本

ok,可以写代码了,简单吧,新建一个脚本继承 BBSimpleTouchableObject类,挂载到你需要实现交互的对象身上去,重写里面的 单指 双指的方法

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Tafdafa : BBSimpleTouchableObject {
  5. public bool allowDrag = true;
  6. public bool allowScale = true;
  7. public bool allowRotate = true;
  8. public float minimumScale = 0.0f;
  9. public float maximumScale = 30.0f;
  10. private Transform saveParent;
  11. private Vector3 movement;
  12. public GameObject pivot;
  13. public override void handleSingleTouch(iPhoneTouch touch)
  14. {
  15. print(touch.position);
  16. Ray ray = Camera.main.ScreenPointToRay(touch.position);
  17. RaycastHit hit;
  18. if (Physics.Raycast(ray,out hit))
  19. {
  20. if (hit.collider.gameObject.name == "Button")
  21. {
  22. print("草拟吗");
  23. }
  24. }
  25. }
  26. public override void handleDoubleTouch(ArrayList events)
  27. {
  28. if (!allowRotate && !allowScale) return;
  29. // double touch can be a scale or a rotate, or both
  30. //
  31. // let's do the rotate first
  32. // since this is a 2 touch gesture, we can only rotate in 2d, which in this case is in the camera plane
  33. // pivot on the lower touch index
  34. iPhoneTouch touch0 = (iPhoneTouch)events[0];
  35. iPhoneTouch touch1 = (iPhoneTouch)events[1];
  36. if (touch0.fingerId > touch1.fingerId)
  37. {
  38. // flip them, 0 should be the earlier index
  39. touch0 = (iPhoneTouch)events[1];
  40. touch1 = (iPhoneTouch)events[0];
  41. }
  42. this.startPivot(gameObject.transform.position);
  43. //
  44. // ROTATE
  45. //
  46. float zDistanceFromCamera = Vector3.Distance(renderingCamera.transform.position, gameObject.transform.position);
  47. Vector3 screenPosition0 = new Vector3(touch0.position.x, touch0.position.y, zDistanceFromCamera);
  48. Vector3 lastScreenPosition0 = new Vector3(touch0.position.x - touch0.deltaPosition.x, touch0.position.y - touch0.deltaPosition.y, zDistanceFromCamera);
  49. Vector3 screenPosition1 = new Vector3(touch1.position.x, touch1.position.y, zDistanceFromCamera);
  50. Vector3 lastScreenPosition1 = new Vector3(touch1.position.x - touch1.deltaPosition.x, touch1.position.y - touch1.deltaPosition.y, zDistanceFromCamera);
  51. //
  52. // /// SCALE
  53. //
  54. if (allowScale)
  55. {
  56. float distNow = (screenPosition0 - screenPosition1).magnitude;
  57. float distThen = (lastScreenPosition0 - lastScreenPosition1).magnitude;
  58. float scale = distNow / distThen;
  59. // presume for the time being that our scales are uniform
  60. if (transform.localScale.x * scale < minimumScale) scale = minimumScale / transform.localScale.x;
  61. if (transform.localScale.x * scale > maximumScale) scale = maximumScale / transform.localScale.x;
  62. Vector3 local = pivot.transform.localScale;
  63. local.x *= scale;
  64. local.y *= scale;
  65. local.z *= scale;
  66. pivot.transform.localScale = local;
  67. }
  68. this.endPivot();
  69. }
  70. virtual protected void startPivot(Vector3 pivotPosition)
  71. {
  72. if (pivot == null)
  73. {
  74. pivot = new GameObject();
  75. pivot.name = "BBBasicTouchManipulation Pivot";
  76. pivot.transform.position = pivotPosition;
  77. }
  78. saveParent = gameObject.transform.parent;
  79. gameObject.transform.parent = null;
  80. pivot.transform.parent = saveParent;
  81. gameObject.transform.parent = pivot.transform;
  82. }
  83. virtual protected void endPivot()
  84. {
  85. gameObject.transform.parent = saveParent;
  86. pivot.transform.parent = null;
  87. Destroy(pivot);
  88. }
  89. }

第二种方式 随便打开touchScript插件自带的场景,加上一个 TuioInput.cs的脚本,

Ok,剩下的就是用TouchScript插件 自带的单指,双指,多点等的方法去写代码吧,不需要管雷达,这个时候你无论是触摸还是雷达都已经联系上了,神奇吧至于 touchScript插件的使用,你们可以去看我以前写的博客,我这里就不赘述了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/114197?site
推荐阅读
相关标签
  

闽ICP备14008679号