MapGenerator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public enum MapTextureType
  6. {
  7. png,
  8. jpg
  9. }
  10. public class MapGenerator : MonoBehaviour
  11. {
  12. public string nameHeard = "L";
  13. public int x_Count = 0;
  14. public int y_Count = 0;
  15. public string ab_Name;
  16. private List<GameObject> quadList = new List<GameObject>();
  17. private bool isInit = false;
  18. private bool isShow = false;
  19. public MapTextureType TextureType;
  20. public bool initDone = false;
  21. public void InitMap()
  22. {
  23. if (!isInit)
  24. {
  25. StartCoroutine(CreatMap(false));
  26. }
  27. }
  28. public void ShowMap()
  29. {
  30. if (!isShow)
  31. {
  32. isShow = true;
  33. if (!isInit)
  34. {
  35. StartCoroutine(CreatMap(true));
  36. }
  37. else
  38. {
  39. foreach (var mapQuad in quadList)
  40. {
  41. mapQuad.SetActive(true);
  42. }
  43. }
  44. }
  45. }
  46. public void Hide()
  47. {
  48. if (isShow)
  49. {
  50. isShow = false;
  51. foreach (var mapQuad in quadList)
  52. {
  53. mapQuad.SetActive(false);
  54. }
  55. }
  56. }
  57. private IEnumerator CreatMap(bool showQuad=false)
  58. {
  59. isInit = true;
  60. WaitForSeconds wait = new WaitForSeconds(0.01f);
  61. for (int i = 0; i < x_Count; i++)
  62. {
  63. for (int j = 0; j < y_Count; j++)
  64. {
  65. GeneratorQuad(i, j, new Vector2(1, 1),showQuad);
  66. }
  67. yield return wait;
  68. }
  69. initDone = true;
  70. }
  71. private void GeneratorQuad(int _x, int _y, Vector2 _size,bool showQuad)
  72. {
  73. string texName = $"{nameHeard}{_x}_{_y}";
  74. GameObject tempQuad = new GameObject(texName);
  75. if (!isShow||showQuad == false)
  76. {
  77. tempQuad.SetActive(false);
  78. }
  79. MeshFilter meshFilter = tempQuad.AddComponent<MeshFilter>();
  80. MeshRenderer meshRenderer = tempQuad.AddComponent<MeshRenderer>();
  81. Material tempMat = new Material(Shader.Find("Unlit/Texture"));
  82. meshRenderer.material = tempMat;
  83. TextureLoadHelp._Instance.LoadTexFromUrl_AB(ab_Name + "_" + _x, texName + "."+TextureType.ToString(), tempMat);
  84. Mesh mesh = new Mesh();
  85. mesh.vertices = new Vector3[]
  86. {
  87. new Vector3(-_size.x / 2, -_size.y / 2, 0), // BL
  88. new Vector3(-_size.x / 2, _size.y / 2, 0), // TL
  89. new Vector3(_size.x / 2, _size.y / 2, 0), // TR
  90. new Vector3(_size.x / 2, -_size.y / 2, 0), // BR
  91. };
  92. mesh.uv = new Vector2[]
  93. {
  94. new Vector2(0, 0), // BL
  95. new Vector2(0, 1), // TL
  96. new Vector2(1, 1), // TR
  97. new Vector2(1, 0), // BR
  98. };
  99. mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
  100. meshFilter.mesh = mesh;
  101. tempQuad.transform.SetParent(this.transform);
  102. tempQuad.transform.localScale = Vector3.one;
  103. var x = (-x_Count * 0.5f) + (_y * _size.x) + 0.5f;
  104. var y = (y_Count * 0.5f) - (_x * _size.y) - 0.5f;
  105. tempQuad.transform.localPosition = new Vector3(x, y, 0);
  106. quadList.Add(tempQuad);
  107. }
  108. }