ObjectPlacementGuidePrefabUpdate.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. namespace O3DWB
  4. {
  5. public static class ObjectPlacementGuidePrefabUpdate
  6. {
  7. #region Public Static Functions
  8. public static void EnsureGuideUsesCorrectPrefab()
  9. {
  10. if(ObjectPlacement.Get().IsObjectVertexSnapSessionActive)
  11. {
  12. ObjectPlacement.Get().DestroyPlacementGuide();
  13. return;
  14. }
  15. if(ObjectPlacement.Get().ObjectPlacementMode == ObjectPlacementMode.VolumeTiles)
  16. {
  17. ObjectPlacement.Get().DestroyPlacementGuide();
  18. return;
  19. }
  20. if (ObjectPlacement.Get().UserWantsToPlaceTileConnections) EnsureGuideUsesBeginTileConnectionPrefab();
  21. else
  22. if (ObjectPlacement.Get().UsingBrushDecorPaintMode) ObjectPlacement.Get().DestroyPlacementGuide();
  23. else EnsureGuideUsesActivePrefab();
  24. // If the placement guide exists in the scene but its source prefab is not available, destroy the guide
  25. if (ObjectPlacementGuide.ExistsInScene)
  26. {
  27. if (!ObjectPlacementGuide.Instance.IsSourcePrefabAvailable) ObjectPlacement.Get().DestroyPlacementGuide();
  28. }
  29. }
  30. #endregion
  31. #region Private Static Functions
  32. private static void EnsureGuideUsesBeginTileConnectionPrefab()
  33. {
  34. ObjectPlacementPathTileConnectionSettings tileConnectionSettings = ObjectPlacement.Get().PathObjectPlacement.PathSettings.TileConnectionSettings;
  35. ObjectPlacementPathTileConnectionTypeSettings beginTileConnectionSettings = tileConnectionSettings.GetSettingsForTileConnectionType(ObjectPlacementPathTileConnectionType.Begin);
  36. if (CanRefreshGuideToUseBeginTileConnectionPrefab(beginTileConnectionSettings))
  37. {
  38. PrefabCategory categoryWhichContainsBeginPrefab = PrefabCategoryDatabase.Get().GetPrefabCategoryWhichContainsPrefab(beginTileConnectionSettings.Prefab);
  39. if (categoryWhichContainsBeginPrefab == null) return;
  40. PrefabCategoryDatabase.Get().SetActivePrefabCategory(categoryWhichContainsBeginPrefab);
  41. categoryWhichContainsBeginPrefab.SetActivePrefab(beginTileConnectionSettings.Prefab);
  42. ObjectPlacement.Get().DestroyPlacementGuide();
  43. ObjectPlacementGuide.CreateFromActivePrefabIfNotExists();
  44. }
  45. // Note: When using tile connections, we will always use the original prefab scale
  46. if (ObjectPlacementGuide.ExistsInScene) ObjectPlacementGuide.Instance.WorldScale = beginTileConnectionSettings.Prefab.InitialWorldScale;
  47. }
  48. private static bool CanRefreshGuideToUseBeginTileConnectionPrefab(ObjectPlacementPathTileConnectionTypeSettings beginTileConnectionSettings)
  49. {
  50. return ((ObjectPlacementGuide.ExistsInScene && beginTileConnectionSettings.Prefab != ObjectPlacementGuide.Instance.SourcePrefab) ||
  51. !ObjectPlacementGuide.ExistsInScene);
  52. }
  53. private static void EnsureGuideUsesActivePrefab()
  54. {
  55. PrefabCategory activePrefabCategory = PrefabCategoryDatabase.Get().ActivePrefabCategory;
  56. if(activePrefabCategory != null)
  57. {
  58. Prefab activePrefab = activePrefabCategory.ActivePrefab;
  59. if(activePrefab != null)
  60. {
  61. if (!ObjectPlacementGuide.ExistsInScene) ObjectPlacementGuide.CreateFromActivePrefabIfNotExists();
  62. else if(ObjectPlacementGuide.Instance.SourcePrefab != activePrefab)
  63. {
  64. ObjectPlacement.Get().DestroyPlacementGuide();
  65. ObjectPlacementGuide.CreateFromActivePrefabIfNotExists();
  66. }
  67. }
  68. }
  69. }
  70. #endregion
  71. }
  72. }
  73. #endif