BattleDecoration.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using System.Collections;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonAIClient.Unity.Utils;
  5. using CommonAI.ZoneClient;
  6. using System.Collections.Generic;
  7. using CommonLang;
  8. namespace CommonAIClient.Unity.Battle
  9. {
  10. public class BattleDecoration : BattleFlag
  11. {
  12. private GameObject mDecrationRoot;
  13. public ZoneEditorDecoration ZDecoration
  14. {
  15. get { return base.ZFlag as ZoneEditorDecoration; }
  16. }
  17. public BattleDecoration(BattleScene battleScene, ZoneEditorDecoration zf)
  18. : base(battleScene, zf)
  19. {
  20. this.ObjectRoot.ZonePos2NavPos(BattleScene.Terrain.TotalHeight
  21. , zf.X, zf.Y, 0f);
  22. this.ObjectRoot.ZoneRot2UnityRot(zf.Data.StripDirection);
  23. mDecrationRoot = new GameObject("DecrationRoot");
  24. using (var pts = ListObjectPool<CommonLang.Geometry.Vector2>.AllocAutoRelease())
  25. {
  26. ZDecoration.Data.GetResourcePoints(pts);
  27. foreach(var pos in pts)
  28. {
  29. GameObject cell = new GameObject("cell");
  30. cell.ParentRoot(mDecrationRoot);
  31. cell.Position(new Vector3(pos.X, 0, pos.Y));
  32. cell.transform.localScale *= ZDecoration.Data.Scale;
  33. }
  34. }
  35. mDecrationRoot.ParentRoot(ObjectRoot);
  36. }
  37. protected override void OnDispose()
  38. {
  39. AssetObjectExt[] aoes = mDecrationRoot.GetComponentsInChildren<AssetObjectExt>();
  40. foreach (var aoe in aoes)
  41. {
  42. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  43. }
  44. base.OnDispose();
  45. }
  46. internal void OnChanged()
  47. {
  48. if (this.ZDecoration.Enable)
  49. {
  50. AssetObjectExt[] aoes = mDecrationRoot.GetComponentsInChildren<AssetObjectExt>();
  51. foreach (var aoe in aoes)
  52. {
  53. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  54. }
  55. for (int i = 0; i < mDecrationRoot.transform.childCount; i++)
  56. {
  57. LoadCellModel(mDecrationRoot.transform.GetChild(i).gameObject
  58. , ZDecoration.Data.ResourceID);
  59. }
  60. }
  61. else
  62. {
  63. AssetObjectExt[] aoes = mDecrationRoot.GetComponentsInChildren<AssetObjectExt>();
  64. foreach (var aoe in aoes)
  65. {
  66. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  67. }
  68. for (int i = 0; i < mDecrationRoot.transform.childCount; i++)
  69. {
  70. LoadCellModel(mDecrationRoot.transform.GetChild(i).gameObject
  71. , ZDecoration.Data.ResourceID_Disabled);
  72. }
  73. }
  74. }
  75. private void LoadCellModel(GameObject root, string name)
  76. {
  77. //加载模型
  78. if (!string.IsNullOrEmpty(name))
  79. {
  80. BattleFactroy.Instance.GameObjectAdapter.Load(
  81. name,
  82. System.IO.Path.GetFileNameWithoutExtension(name),
  83. (succ, o) =>
  84. {
  85. if (succ)
  86. {
  87. if (this.IsDisposed)
  88. {
  89. BattleFactroy.Instance.GameObjectAdapter.Unload(o);
  90. }
  91. else
  92. {
  93. o.gameObject.ParentRoot(root);
  94. }
  95. }
  96. });
  97. }
  98. }
  99. }
  100. }