using System.Collections; using System.Collections.Generic; using UnityEngine; public class MapView_ : MonoBehaviour { public GameManage manage; public TeshMap teshMap; public Transform player; public Transform map_player; public Transform mapPointA; public Transform mapPointB; public List list_Point; public GameObject meshRoute; public List list_route; public float disX = 0; public float disZ = 0; private void OnEnable() { if (API_SVR.GetHead()==null) { Debug.LogError("场景未初始化完成"); return; } SettingPoint(); SettingRoute(); SettingPlayer(); } /// /// 场景内水晶点在场景的比例 算出 在3DMap上应该出现的位置 /// 博物馆4个合一个 从第三个开始 /// public void SettingPoint() { for (int i = 0; i < list_Point.Count; i++) { // Vector3 pos = GetDisPos(manage.list_Crystal[i + 3].localPosition); Debug.Log(manage.list_Crystal[i + 3].localPosition + " " + teshMap.disX + " " + teshMap.disZ); Vector3 pos = manage.list_Crystal[i + 3].localPosition; pos = new Vector3(pos.z, 0, pos.x); pos = GetDisPos(pos); pos = new Vector3(pos.x, pos.y, -pos.z); list_Point[i].localPosition = pos + new Vector3(0, -0.3f, 0); } } public void SettingRoute() { #region old //for (int i = 0; i < manage.data.navLines.Count; i++) //{ // GameObject routes = new GameObject("Route" + i.ToString()); // routes.transform.parent = meshRoute.transform.parent; // for (int j = 0; j < manage.data.navLines[i].points.Count-1; j++) // { // GameObject route = GameObject.Instantiate(meshRoute, routes.transform); // Vector3 endPos = new Vector3((float)manage.data.navLines[i].points[j].X, 0, (float)manage.data.navLines[i].points[j].Y); // Vector3 exitPos = new Vector3((float)manage.data.navLines[i].points[j + 1].X, 0, (float)manage.data.navLines[i].points[j + 1].Y); // Debug.Log(endPos + " " + exitPos); // endPos = GetDisPos(endPos); // exitPos = GetDisPos(exitPos); // Debug.Log(endPos + " " + exitPos); // route.transform.localPosition = (endPos + exitPos) / 2.0f; // // route.GetComponent().SetMapRoute(endPos, exitPos); // route.SetActive(true); // } //} #endregion if (list_route == null) list_route = new List(); for (int i = 0; i < manage.list_route.Count; i++) { GameObject routes = new GameObject("Route" + i.ToString()); routes.transform.parent = meshRoute.transform.parent; routes.transform.localPosition = Vector3.zero; list_route.Add(routes.transform); for (int j = 0; j < manage.list_route[i].transform.childCount; j++) { Vector3 pos = GetDisPos(manage.list_route[i].transform.GetChild(j).localPosition); //Vector3 pos = manage.list_route[i].transform.GetChild(j).localPosition; //pos = new Vector3(pos.z, 0, pos.x); //pos = GetDisPos(pos); // Debug.Log(manage.list_route[i].transform.GetChild(j).localPosition +" "+ teshMap.disX +" "+ teshMap.disZ); GameObject route = GameObject.Instantiate(meshRoute, routes.transform); route.transform.localPosition = pos + new Vector3(0, 0f, 0); route.transform.localRotation = manage.list_route[i].transform.GetChild(j).localRotation; // Debug.Log(route.transform.localPosition); Vector3 endPos = manage.list_route[i].transform.GetChild(j).GetComponent().endPos; Vector3 exitPos = manage.list_route[i].transform.GetChild(j).GetComponent().exitPos; //endPos = GetDisPos(endPos); //exitPos = GetDisPos(exitPos); // route.GetComponent().SetMapRoute(); route.GetComponent().SetMapRoute(endPos , exitPos); route.transform.localScale += manage.list_route[i].transform.GetChild(j).localScale / 15.0f; route.SetActive(true); } } SwitchRoute(0); } public void SwitchRoute( int Index) { if (list_route == null || list_route.Count < Index) { Debug.LogError(" 未初始化路径 或 所选的路径不存在 "); return; } if (list_route.Count >Index) { for (int i = 0; i < list_route.Count; i++) { list_route[i].gameObject.SetActive(false); } list_route[Index].gameObject.SetActive(true); } } public void SettingPlayer() { Vector3 pos = GetDisPos(player.position); Debug.Log(player.position + " " + pos); map_player.localPosition = pos; } /// /// 按照当前场景比例 算出物体在场景内应该出现的位置 /// localPosition /// private Vector3 GetDisPos( Vector3 pos) { if (disX == 0) { disX = Mathf.Abs(mapPointB.localPosition.x - mapPointA.localPosition.x); disZ = Mathf.Abs(mapPointB.localPosition.z - mapPointA.localPosition.z); } float x =pos.x / teshMap.disZ; float z = pos.z / teshMap.disX; //Debug.Log(x +" "+ z); Vector3 disPos = new Vector3(x * disX, 0, z * disZ); return disPos; } }