Paint.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Paint : MonoBehaviour
  5. {
  6. public GameObject trailPrefab;
  7. private TrailRenderer thisTrail;
  8. private Vector3 startPos;
  9. private Plane objPlane;
  10. private Vector3 TeacherPos;
  11. [SerializeField]
  12. private bool isStart = false;
  13. void Start()
  14. {
  15. MessageCenterController.Instance.Register(GameEnum.MESSAGE_REFRESH_LINE_TARGET_POS_DATA, RefreshData);
  16. MessageCenterController.Instance.Register(GameEnum.MESSAGE_DRAW_START, DrawStart);
  17. MessageCenterController.Instance.Register(GameEnum.MESSAGE_DRAW_END, DrawEnd);
  18. isStart = false;
  19. }
  20. private void OnDestroy()
  21. {
  22. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_REFRESH_LINE_TARGET_POS_DATA, RefreshData);
  23. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_DRAW_START, DrawStart);
  24. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_DRAW_END, DrawEnd);
  25. }
  26. void DrawStart(System.Object o)
  27. {
  28. StartCoroutine(_DrawStart(o));
  29. }
  30. IEnumerator _DrawStart(System.Object o)
  31. {
  32. yield return new WaitForEndOfFrame();
  33. DrawData data = (DrawData)o;
  34. startPos = DrawingManager._instance.DrawPoint(); // 获取射线点
  35. GameObject g = Instantiate(trailPrefab, startPos, Quaternion.identity);
  36. // 加入绘制Stack
  37. DrawListManager._instance.AddBrush(g);
  38. thisTrail = g.GetComponent<TrailRenderer>();
  39. thisTrail.startColor = data.GetColor();
  40. thisTrail.endColor = data.GetColor();
  41. // thisTrail.widthMultiplier = data.size;
  42. isStart = true;
  43. }
  44. void DrawEnd(System.Object o)
  45. {
  46. isStart = false;
  47. }
  48. private void RefreshData(System.Object data)
  49. {
  50. if (GamePlayerData.Instance.room_data.IsReconnectOwner())
  51. {
  52. return;//如果是老师的话不执行
  53. }
  54. TeacherPos = (Vector3)data;
  55. }
  56. // Update is called once per frame
  57. void Update()
  58. {
  59. if (isStart)
  60. {
  61. Vector3 v;
  62. if (GamePlayerData.Instance.room_data.IsReconnectOwner())
  63. {
  64. v = DrawingManager.Instance.DrawPoint();
  65. }
  66. else
  67. {
  68. v = TeacherPos;
  69. }
  70. if (thisTrail)
  71. {
  72. thisTrail.transform.position = v;
  73. }
  74. }
  75. }
  76. }