Component.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Base class for all components. Components represent primary logic elements in the scene. They are attached to
  7. /// scene objects.
  8. ///
  9. /// Implementations of <see cref="Component"/> can implement a set of callbacks that will be called by the runtime
  10. /// at specified occassions:
  11. /// void OnInitialize() - Called once when the component is instantiated. Only called when the game is playing.
  12. /// void OnUpdate() - Called every frame while the game is running and the component is enabled.
  13. /// void OnEnable() - Called whenever a component is enabled, or instantiated as enabled in which case it is called
  14. /// after OnInitialize. Only called when the game is playing or paused.
  15. /// void OnDisable() - Called whenever a component is disabled. This includes destruction where it is called before
  16. /// OnDestroy. Only called when the game is playing or paused.
  17. /// void OnDestroy() - Called before the component is destroyed. Destruction is usually delayed until the end of the
  18. /// current frame unless specified otherwise in a call to Destroy.
  19. /// void OnReset() - Called when script assemblies have been refreshed or when the component is initialized. During
  20. /// initialization it is called after OnInitialize but before OnEnable. Only relevant in editor.
  21. ///
  22. /// You can also make these callbacks trigger when the game is stopped/paused by using the <see cref="RunInEditor"/>
  23. /// attribute on the component.
  24. /// </summary>
  25. public class Component : GameObject
  26. {
  27. // Internal use only
  28. protected Component()
  29. { }
  30. /// <summary>
  31. /// Returns the scene object this component is attached to.
  32. /// </summary>
  33. public SceneObject SceneObject
  34. {
  35. get { return Internal_GetSceneObject(mCachedPtr); }
  36. }
  37. /// <summary>
  38. /// Calculates bounds of the visible content for this component.
  39. /// </summary>
  40. /// <param name="box">Bounds in world space represented as an axis aligned bounding box.</param>
  41. /// <param name="sphere">Bounds in world space represented as a sphere.</param>
  42. /// <returns>True if the bounds have non-zero volume, false otherwise.</returns>
  43. internal protected virtual bool CalculateBounds(out AABox box, out Sphere sphere)
  44. {
  45. Vector3 pos = SceneObject.Position;
  46. box = new AABox(pos, pos);
  47. sphere = new Sphere(pos, 0.0f);
  48. return false;
  49. }
  50. [MethodImpl(MethodImplOptions.InternalCall)]
  51. internal static extern Component Internal_AddComponent(SceneObject parent, Type type);
  52. [MethodImpl(MethodImplOptions.InternalCall)]
  53. internal static extern Component Internal_GetComponent(SceneObject parent, Type type);
  54. [MethodImpl(MethodImplOptions.InternalCall)]
  55. internal static extern Component[] Internal_GetComponents(SceneObject parent);
  56. [MethodImpl(MethodImplOptions.InternalCall)]
  57. internal static extern Component Internal_RemoveComponent(SceneObject parent, Type type);
  58. [MethodImpl(MethodImplOptions.InternalCall)]
  59. internal static extern SceneObject Internal_GetSceneObject(IntPtr nativeInstance);
  60. }
  61. }