using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public enum MapTextureType { png, jpg } public class MapGenerator : MonoBehaviour { public string nameHeard = "L"; public int x_Count = 0; public int y_Count = 0; public string ab_Name; private List quadList = new List(); private bool isInit = false; private bool isShow = false; public MapTextureType TextureType; public bool initDone = false; public void InitMap() { if (!isInit) { StartCoroutine(CreatMap(false)); } } public void ShowMap() { if (!isShow) { isShow = true; if (!isInit) { StartCoroutine(CreatMap(true)); } else { foreach (var mapQuad in quadList) { mapQuad.SetActive(true); } } } } public void Hide() { if (isShow) { isShow = false; foreach (var mapQuad in quadList) { mapQuad.SetActive(false); } } } private IEnumerator CreatMap(bool showQuad=false) { isInit = true; WaitForSeconds wait = new WaitForSeconds(0.01f); for (int i = 0; i < x_Count; i++) { for (int j = 0; j < y_Count; j++) { GeneratorQuad(i, j, new Vector2(1, 1),showQuad); } yield return wait; } initDone = true; } private void GeneratorQuad(int _x, int _y, Vector2 _size,bool showQuad) { string texName = $"{nameHeard}{_x}_{_y}"; GameObject tempQuad = new GameObject(texName); if (!isShow||showQuad == false) { tempQuad.SetActive(false); } MeshFilter meshFilter = tempQuad.AddComponent(); MeshRenderer meshRenderer = tempQuad.AddComponent(); Material tempMat = new Material(Shader.Find("Unlit/Texture")); meshRenderer.material = tempMat; TextureLoadHelp._Instance.LoadTexFromUrl_AB(ab_Name + "_" + _x, texName + "."+TextureType.ToString(), tempMat); Mesh mesh = new Mesh(); mesh.vertices = new Vector3[] { new Vector3(-_size.x / 2, -_size.y / 2, 0), // BL new Vector3(-_size.x / 2, _size.y / 2, 0), // TL new Vector3(_size.x / 2, _size.y / 2, 0), // TR new Vector3(_size.x / 2, -_size.y / 2, 0), // BR }; mesh.uv = new Vector2[] { new Vector2(0, 0), // BL new Vector2(0, 1), // TL new Vector2(1, 1), // TR new Vector2(1, 0), // BR }; mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 }; meshFilter.mesh = mesh; tempQuad.transform.SetParent(this.transform); tempQuad.transform.localScale = Vector3.one; var x = (-x_Count * 0.5f) + (_y * _size.x) + 0.5f; var y = (y_Count * 0.5f) - (_x * _size.y) - 0.5f; tempQuad.transform.localPosition = new Vector3(x, y, 0); quadList.Add(tempQuad); } }