Renderable.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. namespace BansheeEngine
  3. {
  4. public class Renderable : Component
  5. {
  6. private RenderableHandler handler;
  7. [SerializeField]
  8. private SerializableData serializableData = new SerializableData();
  9. internal RenderableHandler Handler
  10. {
  11. get { return handler; }
  12. }
  13. public Mesh Mesh
  14. {
  15. get { return handler.Mesh; }
  16. set
  17. {
  18. handler.Mesh = value;
  19. serializableData.mesh = value;
  20. int subMeshCount = 0;
  21. if (value != null)
  22. subMeshCount = value.SubMeshCount;
  23. Material[] newMaterials = new Material[subMeshCount];
  24. int numToCopy = MathEx.Min(newMaterials.Length, serializableData.materials.Length);
  25. Array.Copy(serializableData.materials, newMaterials, numToCopy);
  26. serializableData.materials = newMaterials;
  27. }
  28. }
  29. public Material Material
  30. {
  31. get { return handler.GetMaterial(0); }
  32. set
  33. { handler.SetMaterial(value); serializableData.materials[0] = value; }
  34. }
  35. public Material GetMaterial(int index = 0)
  36. {
  37. return handler.GetMaterial(index);
  38. }
  39. public void SetMaterial(Material material, int index = 0)
  40. {
  41. handler.SetMaterial(material, index);
  42. serializableData.materials[index] = material;
  43. }
  44. public UInt64 Layers
  45. {
  46. get { return handler.Layers; }
  47. set { handler.Layers = value; serializableData.layers = value; }
  48. }
  49. public Bounds Bounds
  50. {
  51. get { return handler.GetBounds(SceneObject); }
  52. }
  53. private void OnInitialize()
  54. {
  55. serializableData.materials = new Material[0];
  56. serializableData.layers = 1;
  57. }
  58. private void OnReset()
  59. {
  60. if (handler != null)
  61. handler.OnDestroy();
  62. handler = new RenderableHandler(SceneObject);
  63. // Restore saved values after reset
  64. handler.Mesh = serializableData.mesh;
  65. if (serializableData.materials != null)
  66. {
  67. for (int i = 0; i < serializableData.materials.Length; i++)
  68. handler.SetMaterial(serializableData.materials[i], i);
  69. }
  70. handler.Layers = serializableData.layers;
  71. }
  72. private void Update()
  73. {
  74. handler.UpdateTransform(SceneObject);
  75. }
  76. private void OnDestroy()
  77. {
  78. handler.OnDestroy();
  79. }
  80. [SerializeObject]
  81. private struct SerializableData
  82. {
  83. public Mesh mesh;
  84. public Material[] materials;
  85. public UInt64 layers;
  86. }
  87. }
  88. }