IKDragPoint.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using UnityEngine;
  3. public class IKDragPoint : MonoBehaviour
  4. {
  5. public static bool IsDrag { get; private set; }
  6. private void Update()
  7. {
  8. if (this.axis_obj_ == null || this.target_trans_ == null)
  9. {
  10. return;
  11. }
  12. if ((this.axis_obj_.is_drag || this.axis_obj_.is_grip) && !this.is_drag_)
  13. {
  14. this.OnDragStart();
  15. IKDragPoint.IsDrag = true;
  16. }
  17. else if (this.is_drag_ && !this.axis_obj_.is_drag && !this.axis_obj_.is_grip)
  18. {
  19. this.OnDragEnd();
  20. IKDragPoint.IsDrag = false;
  21. if (this.PositonCorrectionEnabled)
  22. {
  23. this.target_trans_.localPosition = this.backup_local_pos_;
  24. }
  25. }
  26. this.is_drag_ = (this.axis_obj_.is_drag || this.axis_obj_.is_grip);
  27. if (this.is_drag_)
  28. {
  29. this.OnDrag();
  30. if (this.PositonCorrectionEnabled)
  31. {
  32. this.target_trans_.localPosition = this.backup_local_pos_;
  33. }
  34. return;
  35. }
  36. base.transform.position = this.target_trans_.position;
  37. }
  38. public void OnDestroy()
  39. {
  40. if (this.axis_obj_ != null)
  41. {
  42. UnityEngine.Object.Destroy(this.axis_obj_.gameObject);
  43. }
  44. this.axis_obj_ = null;
  45. }
  46. public void SetTargetIKPoint(GameObject obj)
  47. {
  48. this.target_trans_ = obj.transform;
  49. this.backup_local_pos_ = this.target_trans_.localPosition;
  50. this.Update();
  51. }
  52. public void SetWorldTransformAxis(WorldTransformAxis axis_obj)
  53. {
  54. this.axis_obj_ = axis_obj;
  55. this.Update();
  56. }
  57. public Transform target_ik_point_trans
  58. {
  59. get
  60. {
  61. return this.target_trans_;
  62. }
  63. }
  64. public WorldTransformAxis axis_obj
  65. {
  66. get
  67. {
  68. return this.axis_obj_;
  69. }
  70. }
  71. public bool visible
  72. {
  73. get
  74. {
  75. return this.axis_obj_.Visible;
  76. }
  77. set
  78. {
  79. this.axis_obj_.Visible = value;
  80. }
  81. }
  82. private void OnDragStart()
  83. {
  84. if (this.drag_start_event != null)
  85. {
  86. this.drag_start_event();
  87. }
  88. }
  89. private void OnDrag()
  90. {
  91. if (this.cur_drag_event != null)
  92. {
  93. this.cur_drag_event();
  94. }
  95. }
  96. private void OnDragEnd()
  97. {
  98. if (this.drag_end_event != null)
  99. {
  100. this.drag_end_event();
  101. }
  102. }
  103. public bool PositonCorrectionEnabled = true;
  104. public Action cur_drag_event;
  105. public Action drag_start_event;
  106. public Action drag_end_event;
  107. private Transform target_trans_;
  108. private Vector3 backup_local_pos_;
  109. private WorldTransformAxis axis_obj_;
  110. private bool is_drag_;
  111. }