ObjectOnSurfaceProjectSettings.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. namespace O3DWB
  6. {
  7. [Serializable]
  8. public class ObjectOnSurfaceProjectSettings : ScriptableObject
  9. {
  10. public enum ProjectionDirection
  11. {
  12. Down = 0,
  13. Up
  14. }
  15. [SerializeField]
  16. private bool _isVisible = true;
  17. private bool _projectOnGrid = false;
  18. [SerializeField]
  19. private GameObject _projectionSurface;
  20. [SerializeField]
  21. private ProjectionDirection _projectionDirection = ProjectionDirection.Down;
  22. public bool ProjectOnGrid { get { return _projectOnGrid; } set { _projectOnGrid = value; } }
  23. public GameObject ProjectionSurface
  24. {
  25. get { return _projectionSurface; }
  26. set
  27. {
  28. if (CanBeUsedAsProjectionSurface(value)) _projectionSurface = value;
  29. else Debug.LogWarning("Invalid projection surface. The projection surface object must be a scene object (not prefab) " +
  30. "and it must be either a terrain object with a terrain collider or mesh object. If it is a mesh object, it must be a child of Octave3D.");
  31. }
  32. }
  33. public ProjectionDirection ProjectionDir { get { return _projectionDirection; } set { _projectionDirection = value; } }
  34. public bool CanBeUsedAsProjectionSurface(GameObject gameObject)
  35. {
  36. if (gameObject.HasTerrain()) return gameObject.GetComponent<TerrainCollider>() != null;
  37. else
  38. if (gameObject.HasMesh()) return Octave3DWorldBuilder.ActiveInstance.IsWorkingObject(gameObject);
  39. return false;
  40. }
  41. public Vector3 GetProjectionDirectionVector()
  42. {
  43. return _projectionDirection == ProjectionDirection.Down ? -Vector3.up : Vector3.up;
  44. }
  45. public void RenderView()
  46. {
  47. bool newBool;
  48. newBool = EditorGUILayout.Foldout(_isVisible, "Selection Projection Settings");
  49. if(newBool != _isVisible)
  50. {
  51. UndoEx.RecordForToolAction(this);
  52. _isVisible = newBool;
  53. }
  54. GUIContent content = new GUIContent();
  55. if(_isVisible)
  56. {
  57. EditorGUILayoutEx.BeginVerticalBox();
  58. content.text = "Project on grid";
  59. content.tooltip = "If this is checked, the projection will be done on the scene grid.";
  60. newBool = EditorGUILayout.ToggleLeft(content, ProjectOnGrid);
  61. if(newBool != ProjectOnGrid)
  62. {
  63. UndoEx.RecordForToolAction(this);
  64. ProjectOnGrid = newBool;
  65. }
  66. if(!ProjectOnGrid)
  67. {
  68. Octave3DWorldBuilder.ActiveInstance.ShowGUIHint("The surface projection object must be a terrain object (with a terrain collider) or mesh (no colliders required). " +
  69. "If it is a mesh, it must also be a child of Octave3D.");
  70. content.text = "Surface object";
  71. content.tooltip = "The object which acts as a projection surface. Must be a terrain (with a mesh collider attached) or mesh object. If it is a mesh object, it must be a child of Octave3D.";
  72. GameObject newSurface = EditorGUILayout.ObjectField(content, ProjectionSurface, typeof(GameObject), true) as GameObject;
  73. if (newSurface != _projectionSurface)
  74. {
  75. UndoEx.RecordForToolAction(this);
  76. ProjectionSurface = newSurface;
  77. }
  78. }
  79. content.text = "Projection direction";
  80. content.tooltip = "The direction in which the objects will be projected on the projection surface.";
  81. ProjectionDirection newProjectionDir = (ProjectionDirection)EditorGUILayout.EnumPopup(content, ProjectionDir);
  82. if(newProjectionDir != ProjectionDir)
  83. {
  84. UndoEx.RecordForToolAction(this);
  85. ProjectionDir = newProjectionDir;
  86. }
  87. content.text = "Project selection";
  88. content.tooltip = "Projects the selected objects onto the projection surface (if one was specified).";
  89. if(GUILayout.Button(content, GUILayout.Width(130.0f)))
  90. {
  91. if (ProjectionSurface == null && !ProjectOnGrid) Debug.LogWarning("Projection not possible. No projection surface was specified!");
  92. else ObjectSelection.Get().ProjectSelectionOnProjectionSurface();
  93. }
  94. EditorGUILayoutEx.EndVerticalBox();
  95. }
  96. }
  97. }
  98. }
  99. #endif