MapGenerator.cs 2.8 KB

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