SceneMonoConfig.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using Fort23.UTool;
  3. using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
  4. using UnityEngine;
  5. using Utility;
  6. namespace GameLogic.Combat.CombatTool.SceneTool
  7. {
  8. public class SceneMonoConfig : SceneMonoConfigBasic
  9. {
  10. public GameObject[] allObject;
  11. public float radius;
  12. public string dec;
  13. // private Transform root;
  14. private SceneMonoConfig sceneMonoConfig;
  15. private Map<int, GameObjectPool> _allGameObjectPools = new Map<int, GameObjectPool>();
  16. protected override void ProInit()
  17. {
  18. this.sceneMonoConfig = this;
  19. }
  20. protected override void ProUpdate()
  21. {
  22. Vector3 pos = moveRoot.position;
  23. List<int> removeKey = new List<int>();
  24. for (_allGameObjectPools.Begin(); _allGameObjectPools.Next();)
  25. {
  26. Vector3 objetcPos = _allGameObjectPools.Value.own.transform.position;
  27. if (Vector3.Distance(pos, objetcPos) > 300)
  28. {
  29. removeKey.Add(_allGameObjectPools.Key);
  30. GObjectPool.Instance.Recycle(_allGameObjectPools.Value);
  31. // _allGameObjectPools.Value.SetActive(false);
  32. }
  33. }
  34. for (int i = 0; i < removeKey.Count; i++)
  35. {
  36. _allGameObjectPools.Remove(removeKey[i]);
  37. }
  38. float radius = sceneMonoConfig.radius;
  39. int x = (int)(pos.x / radius);
  40. int z = (int)(pos.z / radius);
  41. float xStartInit = x;
  42. float zStartInit = z;
  43. for (int i = -3; i <= 3; i++)
  44. {
  45. for (int j = -3; j <= 4; j++)
  46. {
  47. // if (i == 0 && j == 0)
  48. // {
  49. // continue;
  50. // }
  51. float currX = (xStartInit + i) * radius;
  52. float currZ = (zStartInit + j) * radius;
  53. int key = (int)(currX * 1000 + currZ);
  54. if (_allGameObjectPools.ContainsKey(key))
  55. {
  56. continue;
  57. }
  58. GameObjectPool gameObjectPool = GetObejct();
  59. float y = Random.Range(-2, 0);
  60. gameObjectPool.own.transform.position = new Vector3(currX, y, currZ);
  61. _allGameObjectPools.Add(key, gameObjectPool);
  62. }
  63. }
  64. }
  65. private GameObjectPool GetObejct()
  66. {
  67. int index = Random.Range(0, sceneMonoConfig.allObject.Length);
  68. GameObject gameObject = sceneMonoConfig.allObject[index];
  69. // GameObject go = GameObject.Instantiate(gameObject);
  70. GameObjectPool gameObjectPool =
  71. GObjectPool.Instance.FetchAsyncForGameObject<GameObjectPool>(gameObject, gameObject.name);
  72. return gameObjectPool;
  73. }
  74. }
  75. }