InspectableGameObjectRef.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Displays GUI for a serializable property containing a <see cref="GameObject"/> reference.
  8. /// </summary>
  9. public class InspectableGameObjectRef : InspectableField
  10. {
  11. private GUIGameObjectField guiField;
  12. private InspectableState state;
  13. /// <summary>
  14. /// Creates a new inspectable game object reference GUI for the specified property.
  15. /// </summary>
  16. /// <param name="parent">Parent Inspector this field belongs to.</param>
  17. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  18. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  19. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  20. /// contain other fields, in which case you should increase this value by one.</param>
  21. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  22. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  23. public InspectableGameObjectRef(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  24. SerializableProperty property)
  25. : base(parent, title, path, SerializableProperty.FieldType.GameObjectRef, depth, layout, property)
  26. {
  27. }
  28. /// <inheritoc/>
  29. protected internal override void Initialize(int layoutIndex)
  30. {
  31. if (property != null)
  32. {
  33. guiField = new GUIGameObjectField(property.InternalType, new GUIContent(title));
  34. guiField.OnChanged += OnFieldValueChanged;
  35. layout.AddElement(layoutIndex, guiField);
  36. }
  37. }
  38. /// <inheritdoc/>
  39. public override InspectableState Refresh(int layoutIndex)
  40. {
  41. if (guiField != null)
  42. guiField.Value = property.GetValue<GameObject>();
  43. InspectableState oldState = state;
  44. if (state.HasFlag(InspectableState.Modified))
  45. state = InspectableState.NotModified;
  46. return oldState;
  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. state = InspectableState.Modified;
  56. }
  57. }
  58. }