CoursewareItem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CoursewareItem : MonoBehaviour {
  5. [SerializeField]
  6. private CoursewareDir ShowDir;
  7. [SerializeField]
  8. private float ShowScaleRate = 2;//放大倍数
  9. private Transform startPar;//初始的根节点
  10. private Vector3 startPos;//初始的位置
  11. private Vector3 startScale;//初始大小
  12. private bool isShow = false;//是否正在被焦点展示
  13. void Start () {
  14. startPar = this.transform.parent;
  15. startPos = this.transform.position;
  16. startScale = this.transform.localScale;
  17. }
  18. // Update is called once per frame
  19. private void LateUpdate() {
  20. if(Input.GetKeyDown(KeyCode.P))
  21. {
  22. Show();
  23. }
  24. if (Input.GetKeyUp(KeyCode.P))
  25. {
  26. //Hide();
  27. }
  28. RefreshTween();
  29. }
  30. private float showDis = 0.25f;
  31. private Vector3 GetShowPos()
  32. {
  33. var pos = Vector3.zero;
  34. switch(ShowDir)
  35. {
  36. case CoursewareDir.Up:
  37. pos = Vector3.up;
  38. break;
  39. case CoursewareDir.Down:
  40. pos = Vector3.down;
  41. break;
  42. case CoursewareDir.Left:
  43. pos = Vector3.left;
  44. break;
  45. case CoursewareDir.Right:
  46. pos = Vector3.right;
  47. break;
  48. case CoursewareDir.LeftUp:
  49. pos = Vector3.up + Vector3.left;
  50. break;
  51. case CoursewareDir.LeftDown:
  52. pos = Vector3.down + Vector3.left;
  53. break;
  54. case CoursewareDir.RightUp:
  55. pos = Vector3.up + Vector3.right;
  56. break;
  57. case CoursewareDir.RightDown:
  58. pos = Vector3.down + Vector3.right;
  59. break;
  60. default:
  61. pos = Vector3.zero;
  62. break;
  63. }
  64. return pos * showDis + startPos;
  65. }
  66. private float suofangNeedTime = 0.5f;//缩放需要的时间
  67. private void RefreshTween()
  68. {
  69. if(isTweening)
  70. {
  71. useTime += Time.deltaTime/ suofangNeedTime;
  72. this.transform.position = Vector3.Lerp(this.transform.position, targetPos, useTime);
  73. this.transform.localScale = Vector3.Lerp(this.transform.localScale, targetScale, useTime);
  74. if (useTime >= 1)
  75. {
  76. Arrived();
  77. }
  78. }
  79. }
  80. public bool IsShow
  81. {
  82. get { return isShow; }
  83. }
  84. public bool IsTweening
  85. {
  86. get { return isTweening; }
  87. }
  88. public void Show()
  89. {
  90. isShow = true;
  91. this.transform.parent = null;//先放出去
  92. AddTween(GetShowPos(), startScale * ShowScaleRate);
  93. }
  94. private bool isTweening = false;
  95. private Vector3 targetPos;
  96. private Vector3 targetScale;
  97. private Vector3 curPos;
  98. private Vector3 curScale;
  99. private float useTime = 0;
  100. private void AddTween(Vector3 pos, Vector3 scale)
  101. {
  102. CDebug.Log("AddTween");
  103. useTime = 0;
  104. curPos = this.transform.position;
  105. curScale = this.transform.localScale;
  106. isTweening = true;
  107. targetPos = pos;
  108. targetScale = scale;
  109. }
  110. //动画结束的位置
  111. private void Arrived()
  112. {
  113. CDebug.Log("欢动完成");
  114. isTweening = false;
  115. //关闭了
  116. if (!isShow)
  117. {
  118. this.transform.parent = startPar;//归位
  119. }
  120. }
  121. public void Hide()
  122. {
  123. isShow = false;
  124. AddTween(startPos, startScale);
  125. }
  126. public void QiangzhiHide()
  127. {
  128. isShow = false;
  129. this.transform.parent = startPar;//归位
  130. this.transform.position = startPos;
  131. this.transform.localScale = startScale;
  132. }
  133. }