PencilPointer.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // /******************************************************************************
  2. // * File: PencilPointer.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using UnityEngine;
  8. using UnityEngine.InputSystem;
  9. namespace QCHT.Samples.Drawing
  10. {
  11. public class PencilPointer : MonoBehaviour
  12. {
  13. public InputAction PenDown;
  14. [Space]
  15. [SerializeField] private float minSizeMultiplier = 1f;
  16. [SerializeField] private float maxSizeMultiplier = 2f;
  17. [Space]
  18. [SerializeField] private ParticleSystem pointerParticles;
  19. [SerializeField] private ParticleSystem lineParticles;
  20. private float _referenceScale = 1f;
  21. private void OnEnable() => PenDown.Enable();
  22. private void OnDisable() => PenDown.Disable();
  23. public void SetColor(Color color)
  24. {
  25. var main = pointerParticles.main;
  26. main.startColor = color;
  27. }
  28. public void SetScale(float scale)
  29. {
  30. _referenceScale = scale;
  31. }
  32. public void SetLineParticles(ParticleSystem particles)
  33. {
  34. lineParticles = particles;
  35. var particleSystemTransform = particles.transform;
  36. particleSystemTransform.SetParent(transform);
  37. particleSystemTransform.localPosition = Vector3.zero;
  38. }
  39. public void DestroyLineParticles()
  40. {
  41. if (!lineParticles)
  42. return;
  43. Destroy(lineParticles.gameObject);
  44. lineParticles = null;
  45. }
  46. public void StartLineParticles()
  47. {
  48. if (lineParticles)
  49. lineParticles.Play();
  50. }
  51. public void StopLineParticles()
  52. {
  53. if (lineParticles)
  54. lineParticles.Stop();
  55. }
  56. public void UpdateScale()
  57. {
  58. var main = pointerParticles.main;
  59. main.startSize = minSizeMultiplier * _referenceScale;
  60. }
  61. public void Show()
  62. {
  63. pointerParticles.Play();
  64. }
  65. public void Hide()
  66. {
  67. pointerParticles.Stop();
  68. }
  69. }
  70. }