MapGenerator.cs 3.1 KB

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