MovableContent.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. namespace Immersal.Samples.ContentPlacement
  10. {
  11. public class MovableContent : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private float m_ClickHoldTime = 0.1f;
  15. private float m_timeHold = 0f;
  16. private bool m_EditingContent = false;
  17. private Transform m_CameraTransform;
  18. private float m_MovePlaneDistance;
  19. private void Start()
  20. {
  21. m_CameraTransform = Camera.main.transform;
  22. StoreContent();
  23. }
  24. private void Update()
  25. {
  26. if (m_EditingContent)
  27. {
  28. Vector3 projection = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, m_MovePlaneDistance));
  29. transform.position = projection;
  30. }
  31. }
  32. private void StoreContent()
  33. {
  34. if (!ContentStorageManager.Instance.contentList.Contains(this))
  35. {
  36. ContentStorageManager.Instance.contentList.Add(this);
  37. }
  38. ContentStorageManager.Instance.SaveContents();
  39. }
  40. public void RemoveContent()
  41. {
  42. if (ContentStorageManager.Instance.contentList.Contains(this))
  43. {
  44. ContentStorageManager.Instance.contentList.Remove(this);
  45. }
  46. ContentStorageManager.Instance.SaveContents();
  47. Destroy(gameObject);
  48. }
  49. private void OnMouseDrag()
  50. {
  51. m_timeHold += Time.deltaTime;
  52. if (m_timeHold >= m_ClickHoldTime && !m_EditingContent)
  53. {
  54. m_MovePlaneDistance = Vector3.Dot(transform.position - m_CameraTransform.position, m_CameraTransform.forward) / m_CameraTransform.forward.sqrMagnitude;
  55. m_EditingContent = true;
  56. }
  57. }
  58. private void OnMouseUp()
  59. {
  60. StoreContent();
  61. m_timeHold = 0f;
  62. m_EditingContent = false;
  63. }
  64. }
  65. }