InspectableGameObjectRef.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a <see cref="GameObject"/> reference.
  11. /// </summary>
  12. public class InspectableGameObjectRef : InspectableField
  13. {
  14. private GameObject propertyValue;
  15. private GUIGameObjectField guiField;
  16. /// <summary>
  17. /// Creates a new inspectable game object reference GUI for the specified property.
  18. /// </summary>
  19. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  20. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  21. /// contain other fields, in which case you should increase this value by one.</param>
  22. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  23. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  24. public InspectableGameObjectRef(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  25. : base(title, depth, layout, property)
  26. {
  27. }
  28. /// <inheritoc/>
  29. protected override void BuildGUI(int layoutIndex)
  30. {
  31. if (property.Type == SerializableProperty.FieldType.GameObjectRef)
  32. {
  33. guiField = new GUIGameObjectField(property.InternalType, new GUIContent(title));
  34. guiField.OnChanged += OnFieldValueChanged;
  35. layout.AddElement(layoutIndex, guiField);
  36. }
  37. }
  38. /// <inheritdoc/>
  39. protected override bool IsModified(out bool rebuildGUI)
  40. {
  41. GameObject newPropertyValue = property.GetValue<GameObject>();
  42. if (propertyValue != newPropertyValue)
  43. {
  44. rebuildGUI = false;
  45. return true;
  46. }
  47. return base.IsModified(out rebuildGUI);
  48. }
  49. /// <inheritdoc/>
  50. protected override void Update(int layoutIndex, bool rebuildGUI)
  51. {
  52. base.Update(layoutIndex, rebuildGUI);
  53. propertyValue = property.GetValue<GameObject>();
  54. if (guiField != null)
  55. guiField.Value = propertyValue;
  56. }
  57. /// <summary>
  58. /// Triggered when the user drops a new game object onto the field, or clears the current value.
  59. /// </summary>
  60. /// <param name="newValue">New game object to reference.</param>
  61. private void OnFieldValueChanged(GameObject newValue)
  62. {
  63. property.SetValue(newValue);
  64. }
  65. }
  66. }