GUIWidget.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. namespace BansheeEngine
  2. {
  3. /// <summary>
  4. /// Container of GUI elements that can be positioned in the scene, and can output the rendered GUI to a user defined
  5. /// camera.
  6. /// </summary>
  7. public class GUIWidget : Component
  8. {
  9. private NativeGUIWidget nativeGUIWidget;
  10. [SerializeField]
  11. private SerializableData serializableData = new SerializableData();
  12. /// <summary>
  13. /// Skin used for rendering all the GUI elements belonging to this widget.
  14. /// </summary>
  15. public GUISkin Skin
  16. {
  17. get { return serializableData.skin; }
  18. set
  19. {
  20. serializableData.skin = value;
  21. nativeGUIWidget.Skin = value;
  22. }
  23. }
  24. /// <summary>
  25. /// Determines to which camera are the GUI elements belonong to this widget rendered. If null then they will be
  26. /// rendered on the main camera.
  27. /// </summary>
  28. public Camera Camera
  29. {
  30. get { return serializableData.camera; }
  31. set
  32. {
  33. serializableData.camera = value;
  34. nativeGUIWidget.Camera = value;
  35. }
  36. }
  37. /// <summary>
  38. /// Container into which all GUI elements belonging to this widget should be placed.
  39. /// </summary>
  40. public GUIPanel Panel
  41. {
  42. get { return nativeGUIWidget.Panel; }
  43. }
  44. /// <summary>
  45. /// Depth that determines in which order are GUI widgets rendered. Widgets with lower depth are rendered in front
  46. /// of widgets with higher depth.
  47. /// </summary>
  48. public short Depth
  49. {
  50. set { nativeGUIWidget.Depth = value; }
  51. get { return nativeGUIWidget.Depth; }
  52. }
  53. private void OnReset()
  54. {
  55. if (nativeGUIWidget != null)
  56. nativeGUIWidget.Destroy();
  57. nativeGUIWidget = new NativeGUIWidget();
  58. // Restore saved values after reset
  59. nativeGUIWidget.Skin = serializableData.skin;
  60. nativeGUIWidget.Camera = serializableData.camera;
  61. }
  62. private void OnUpdate()
  63. {
  64. nativeGUIWidget.UpdateTransform(SceneObject);
  65. if (serializableData.camera == null)
  66. nativeGUIWidget.UpdateMainCamera(Scene.Camera);
  67. }
  68. private void OnDestroy()
  69. {
  70. nativeGUIWidget.Destroy();
  71. }
  72. /// <summary>
  73. /// Holds all data the GUIWidget component needs to persist through serialization.
  74. /// </summary>
  75. [SerializeObject]
  76. private class SerializableData
  77. {
  78. public GUISkin skin;
  79. public Camera camera;
  80. }
  81. }
  82. }