using System;
using UnityEngine;
using DefiMetaverse.Api;
using UnityEngine.Networking;
using Newtonsoft.Json;
namespace DefiMetaverse
{
public class MapEditor : MonoBehaviour
{
[SerializeField] private float cellRatio = 10.563f;
[SerializeField] private Vector3 size = new Vector3(50, 0, 50);
[SerializeField] private int cityId = 1;
[SerializeField] private LandTypeRankGameObject prefabs;
private string primaryParent = "__PrimaryObjects";
public void LoadCityMap()
{
string url = $"https://dev-meta-api.defiwarrior.io/api/v1/lands/init/{cityId}";
Debug.Log($"LoadCityMap: {url}");
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
webRequest.SetRequestHeader("Content-Type", "application/json");
webRequest.SetRequestHeader("X-Secret-Key", "06ff0cd4-0d90-11ed-861d-0242ac120002");
// Request and wait for the desired page.
webRequest.SendWebRequest();
while (webRequest.result == UnityWebRequest.Result.InProgress)
{
//do something, or nothing while blocking
}
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError("Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError("HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log("Received: " + webRequest.downloadHandler.text);
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
CityLandResponse rs = JsonConvert.DeserializeObject<CityLandResponse>(webRequest.downloadHandler.text, settings);
Debug.Log(JsonConvert.SerializeObject(rs));
GameObject parent = GameObject.Find(primaryParent);
if (parent != null)
{
Debug.Log($"Found {primaryParent}");
GameObject.DestroyImmediate(parent);
}
parent = new GameObject(primaryParent);
InitTerrain(parent.transform);
InitCityMap(rs.data, parent.transform);
break;
}
}
}
private void InitTerrain(Transform parent)
{
//create a new terrain data
TerrainData _terrainData = new TerrainData();
//set terrain width, height, length
_terrainData.size = size * cellRatio;
//this is where I'm stuck, how do I change the terrain colour?
GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
_terrain.transform.position = _terrainData.size / -2;
_terrain.transform.parent = parent;
}
private void InitCityMap(CityLandDataList cityLandDatas, Transform parent)
{
if (cityLandDatas == null)
return;
foreach (CityLandData land in cityLandDatas)
{
Debug.Log($"land {land.id}: {land.cell.landType}");
if (prefabs.TryGetValue(land.cell.landTypeRank, out GameObject prefab))
{
GameObject building = GameObject.Instantiate(prefab);
building.transform.position = new Vector3(land.cell.p.x + land.cell.s.x / 2, 0.0f, -land.cell.p.y - land.cell.s.y / 2) * cellRatio;
building.transform.parent = parent.transform;
}
}
}
public void GenarateMeshCollider()
{
var objs = FindObjectsOfType<MeshRenderer>();
foreach (var item in objs)
{
if (!item.sortingLayerName.Equals("NPC"))
{
Debug.Log($"GenarateMeshCollider {item.gameObject.sceneCullingMask}");
item.gameObject.GetOrAddComponent<MeshCollider>();
}
}
}
}
}
No comments:
Post a Comment