InspectableGameObjectRef.cs 2.5 KB

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