FlowPaintScreen.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using CW.Common;
  3. namespace FLOW
  4. {
  5. /// <summary>This component allows you paint the specified fluid prefab under the mouse/finger as it drags across the screen.</summary>
  6. [HelpURL(FlowCommon.HelpUrlPrefix + "FlowPaintScreen")]
  7. [AddComponentMenu(FlowCommon.ComponentMenuPrefix + "Paint Screen")]
  8. public class FlowPaintScreen : MonoBehaviour
  9. {
  10. /// <summary>The finger/mouse/key that must be pressed for the painting.</summary>
  11. public CwInputManager.Trigger PaintControls = new CwInputManager.Trigger(true, true, KeyCode.None);
  12. /// <summary>The modifier that will be applied to the fluid simulations.</summary>
  13. public FlowModifier Modifier { set { modifier = value; } get { return modifier; } } [SerializeField] private FlowModifier modifier;
  14. /// <summary>If you need to display some sort of visual while this component is drawing, you can set it here.</summary>
  15. public GameObject Visual { set { visual = value; } get { return visual; } } [SerializeField] private GameObject visual;
  16. /// <summary>The scene layers under the finger/mouse that this component will raycast. The spawned prefab will be placed there.</summary>
  17. public LayerMask Layers { set { layers = value; } get { return layers; } } [SerializeField] private LayerMask layers = Physics.DefaultRaycastLayers;
  18. /// <summary>The maximum distance of the raycast.</summary>
  19. public float MaxDistance { set { maxDistance = value; } get { return maxDistance; } } [SerializeField] private float maxDistance = 100.0f;
  20. protected virtual void OnEnable()
  21. {
  22. CwInputManager.EnsureThisComponentExists();
  23. }
  24. protected virtual void FixedUpdate()
  25. {
  26. var showVisual = false;
  27. if (modifier != null)
  28. {
  29. var camera = Camera.main;
  30. if (camera != null)
  31. {
  32. var fingers = CwInputManager.GetFingers(true, false);
  33. foreach (var finger in fingers)
  34. {
  35. if (PaintControls.IsDown(finger) == true)
  36. {
  37. var hit = default(RaycastHit);
  38. if (Physics.Raycast(camera.ScreenPointToRay(finger.ScreenPosition), out hit, float.PositiveInfinity, layers) == true)
  39. {
  40. DoSpawn(hit);
  41. showVisual = true;
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. if (visual != null)
  49. {
  50. visual.SetActive(showVisual);
  51. }
  52. }
  53. private void DoSpawn(RaycastHit hit)
  54. {
  55. modifier.transform.position = hit.point;
  56. modifier.transform.rotation = modifier.transform.rotation;
  57. if (visual != null)
  58. {
  59. visual.transform.position = modifier.transform.position;
  60. visual.transform.rotation = modifier.transform.rotation;
  61. }
  62. modifier.ApplyNow(Time.fixedDeltaTime);
  63. }
  64. }
  65. }
  66. #if UNITY_EDITOR
  67. namespace FLOW
  68. {
  69. using UnityEditor;
  70. using TARGET = FlowPaintScreen;
  71. [CanEditMultipleObjects]
  72. [CustomEditor(typeof(TARGET))]
  73. public class FlowPaintScreen_Editor : CwEditor
  74. {
  75. protected override void OnInspector()
  76. {
  77. TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
  78. Draw("PaintControls", "The finger/mouse/key that must be pressed for the painting.");
  79. Separator();
  80. BeginError(Any(tgts, t => t.Modifier == null));
  81. Draw("modifier", "The modifier that will be applied to the fluid simulations.");
  82. EndError();
  83. Draw("visual", "If you need to display some sort of visual while this component is drawing, you can set it here.");
  84. Draw("layers", "The scene layers under the finger/mouse that this component will raycast. The spawned prefab will be placed there.");
  85. Draw("maxDistance", "The maximum distance of the raycast.");
  86. }
  87. }
  88. }
  89. #endif