GUIWidget.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. internal 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. internal 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. internal GUIPanel Panel
  41. {
  42. get { return nativeGUIWidget.Panel; }
  43. }
  44. private void OnReset()
  45. {
  46. if (nativeGUIWidget != null)
  47. nativeGUIWidget.Destroy();
  48. nativeGUIWidget = new NativeGUIWidget();
  49. // Restore saved values after reset
  50. nativeGUIWidget.Skin = serializableData.skin;
  51. nativeGUIWidget.Camera = serializableData.camera;
  52. }
  53. private void OnUpdate()
  54. {
  55. nativeGUIWidget.UpdateTransform(SceneObject);
  56. if (serializableData.camera == null)
  57. nativeGUIWidget.UpdateMainCamera(Scene.Camera);
  58. }
  59. private void OnDestroy()
  60. {
  61. nativeGUIWidget.Destroy();
  62. }
  63. /// <summary>
  64. /// Holds all data the GUIWidget component needs to persist through serialization.
  65. /// </summary>
  66. [SerializeObject]
  67. private class SerializableData
  68. {
  69. public GUISkin skin;
  70. public Camera camera;
  71. }
  72. }
  73. }