Painter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. public class Painter : MonoBehaviour
  3. {
  4. /// <summary>
  5. /// 画笔的颜色
  6. /// </summary>
  7. public Color32 penColor;
  8. public Transform rayOrigin;
  9. private RaycastHit hitInfo;
  10. //这个画笔是不是正在被手柄抓着
  11. private bool IsGrabbing;
  12. private static PaintBoard board;//设置成类型的成员,而不是类型实例的成员,因为所有画笔都是用的同一个board
  13. private void Start()
  14. {
  15. //将画笔部件设置为画笔的颜色,用于识别这个画笔的颜色
  16. foreach (var renderer in GetComponentsInChildren<MeshRenderer>())
  17. {
  18. if (renderer.transform == transform)
  19. {
  20. continue;
  21. }
  22. renderer.material.color = penColor;
  23. }
  24. if (!board)
  25. {
  26. board = FindObjectOfType<PaintBoard>();
  27. }
  28. }
  29. private void Update()
  30. {
  31. Ray r = new Ray(rayOrigin.position, rayOrigin.forward);
  32. Debug.DrawLine(rayOrigin.position, rayOrigin.position + rayOrigin.forward * 10, Color.red);
  33. if (Physics.Raycast(r, out hitInfo, 0.1f))
  34. {
  35. if (hitInfo.collider.tag == "Board")
  36. {
  37. //设置画笔所在位置对应画板图片的UV坐标
  38. board.SetPainterPositon(hitInfo.textureCoord.x, hitInfo.textureCoord.y);
  39. //当前笔的颜色
  40. board.SetPainterColor(penColor);
  41. board.IsDrawing = true;
  42. IsGrabbing = true;
  43. }
  44. }
  45. else if (IsGrabbing)
  46. {
  47. board.IsDrawing = false;
  48. IsGrabbing = false;
  49. }
  50. }
  51. }