PointAndClickObjectPlacement.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. namespace O3DWB
  5. {
  6. [Serializable]
  7. public class PointAndClickObjectPlacement
  8. {
  9. #region Public Static Functions
  10. public static PointAndClickObjectPlacement Get()
  11. {
  12. return ObjectPlacement.Get().PointAndClickObjectPlacement;
  13. }
  14. #endregion
  15. #region Public Methods
  16. public void HandleMouseMoveEvent(Event e)
  17. {
  18. if (ObjectPlacementGuide.ExistsInSceneAndIsActive)
  19. {
  20. e.DisableInSceneView();
  21. AdjustGuidePositionAndRotation(e);
  22. }
  23. }
  24. public void HandleMouseButtonDownEvent(Event e)
  25. {
  26. if (e.InvolvesLeftMouseButton())
  27. {
  28. e.DisableInSceneView();
  29. if (ObjectPlacementGuide.ExistsInScene)
  30. {
  31. PlaceObject();
  32. ApplyRandomizationsForPlacementGuide();
  33. }
  34. }
  35. }
  36. #endregion
  37. #region Private Methods
  38. private void AdjustGuidePositionAndRotation(Event e)
  39. {
  40. ObjectPlacementGuide.Instance.Snap();
  41. AxisAlignment.AlignObjectAxis(ObjectPlacementGuide.SceneObject, PointAndClickObjectPlacementSettings.Get().PlacementGuideSurfaceAlignmentSettings, ObjectSnapping.Get().ObjectSnapSurfacePlane.normal);
  42. }
  43. private void ApplyRandomizationsForPlacementGuide()
  44. {
  45. PointAndClickObjectPlacementSettings.Get().PlacementGuideRotationRandomizationSettings.CustomAxisRandomizationSettings.Axis = ObjectSnapping.Get().ObjectSnapSurfacePlane.normal;
  46. ObjectRotationRandomization.Randomize(ObjectPlacementGuide.SceneObject, PointAndClickObjectPlacementSettings.Get().PlacementGuideRotationRandomizationSettings);
  47. ObjectScaleRandomization.Randomize(ObjectPlacementGuide.SceneObject, PointAndClickObjectPlacementSettings.Get().PlacementGuideScaleRandomizationSettings);
  48. }
  49. private void PlaceObject()
  50. {
  51. ObjectPlacementGuide placementGuide = ObjectPlacementGuide.Instance;
  52. GameObject placedHierarchyRoot = Octave3DScene.Get().InstantiateObjectHierarchyFromPrefab(placementGuide.SourcePrefab, placementGuide.WorldPosition, placementGuide.WorldRotation, placementGuide.WorldScale);
  53. ObjectHierarchyRootsWerePlacedInSceneMessage.SendToInterestedListeners(placedHierarchyRoot, ObjectHierarchyRootsWerePlacedInSceneMessage.PlacementType.ObjectPlacement);
  54. if (PointAndClickObjectPlacementSettings.Get().RandomizePrefabsInActiveCategory)
  55. {
  56. PrefabCategory activePrefabCategory = PrefabCategoryDatabase.Get().ActivePrefabCategory;
  57. if (activePrefabCategory != null) activePrefabCategory.RandomizeActivePrefab();
  58. }
  59. }
  60. #endregion
  61. }
  62. }
  63. #endif