MapView_.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MapView_ : MonoBehaviour
  5. {
  6. public GameManage manage;
  7. public TeshMap teshMap;
  8. public Transform player;
  9. public Transform map_player;
  10. public Transform mapPointA;
  11. public Transform mapPointB;
  12. public List<Transform> list_Point;
  13. public GameObject meshRoute;
  14. public List<Transform> list_route;
  15. public float disX = 0;
  16. public float disZ = 0;
  17. private void OnEnable()
  18. {
  19. if (API_SVR.GetHead()==null)
  20. {
  21. Debug.LogError("场景未初始化完成");
  22. return;
  23. }
  24. SettingPoint();
  25. SettingRoute();
  26. SettingPlayer();
  27. }
  28. /// <summary>
  29. /// 场景内水晶点在场景的比例 算出 在3DMap上应该出现的位置
  30. /// 博物馆4个合一个 从第三个开始
  31. /// </summary>
  32. public void SettingPoint()
  33. {
  34. for (int i = 0; i < list_Point.Count; i++)
  35. {
  36. // Vector3 pos = GetDisPos(manage.list_Crystal[i + 3].localPosition);
  37. Debug.Log(manage.list_Crystal[i + 3].localPosition + " " + teshMap.disX + " " + teshMap.disZ);
  38. Vector3 pos = manage.list_Crystal[i + 3].localPosition;
  39. pos = new Vector3(pos.z, 0, pos.x);
  40. pos = GetDisPos(pos);
  41. pos = new Vector3(pos.x, pos.y, -pos.z);
  42. list_Point[i].localPosition = pos + new Vector3(0, -0.3f, 0);
  43. }
  44. }
  45. public void SettingRoute()
  46. {
  47. #region old
  48. //for (int i = 0; i < manage.data.navLines.Count; i++)
  49. //{
  50. // GameObject routes = new GameObject("Route" + i.ToString());
  51. // routes.transform.parent = meshRoute.transform.parent;
  52. // for (int j = 0; j < manage.data.navLines[i].points.Count-1; j++)
  53. // {
  54. // GameObject route = GameObject.Instantiate(meshRoute, routes.transform);
  55. // Vector3 endPos = new Vector3((float)manage.data.navLines[i].points[j].X, 0, (float)manage.data.navLines[i].points[j].Y);
  56. // Vector3 exitPos = new Vector3((float)manage.data.navLines[i].points[j + 1].X, 0, (float)manage.data.navLines[i].points[j + 1].Y);
  57. // Debug.Log(endPos + " " + exitPos);
  58. // endPos = GetDisPos(endPos);
  59. // exitPos = GetDisPos(exitPos);
  60. // Debug.Log(endPos + " " + exitPos);
  61. // route.transform.localPosition = (endPos + exitPos) / 2.0f;
  62. // // route.GetComponent<TestRoute>().SetMapRoute(endPos, exitPos);
  63. // route.SetActive(true);
  64. // }
  65. //}
  66. #endregion
  67. if (list_route == null)
  68. list_route = new List<Transform>();
  69. for (int i = 0; i < manage.list_route.Count; i++)
  70. {
  71. GameObject routes = new GameObject("Route" + i.ToString());
  72. routes.transform.parent = meshRoute.transform.parent;
  73. routes.transform.localPosition = Vector3.zero;
  74. list_route.Add(routes.transform);
  75. for (int j = 0; j < manage.list_route[i].transform.childCount; j++)
  76. {
  77. Vector3 pos = GetDisPos(manage.list_route[i].transform.GetChild(j).localPosition);
  78. //Vector3 pos = manage.list_route[i].transform.GetChild(j).localPosition;
  79. //pos = new Vector3(pos.z, 0, pos.x);
  80. //pos = GetDisPos(pos);
  81. // Debug.Log(manage.list_route[i].transform.GetChild(j).localPosition +" "+ teshMap.disX +" "+ teshMap.disZ);
  82. GameObject route = GameObject.Instantiate(meshRoute, routes.transform);
  83. route.transform.localPosition = pos + new Vector3(0, 0f, 0);
  84. route.transform.localRotation = manage.list_route[i].transform.GetChild(j).localRotation;
  85. // Debug.Log(route.transform.localPosition);
  86. Vector3 endPos = manage.list_route[i].transform.GetChild(j).GetComponent<TestRoute>().endPos;
  87. Vector3 exitPos = manage.list_route[i].transform.GetChild(j).GetComponent<TestRoute>().exitPos;
  88. //endPos = GetDisPos(endPos);
  89. //exitPos = GetDisPos(exitPos);
  90. // route.GetComponent<TestRoute>().SetMapRoute();
  91. route.GetComponent<TestRoute>().SetMapRoute(endPos , exitPos);
  92. route.transform.localScale += manage.list_route[i].transform.GetChild(j).localScale / 15.0f;
  93. route.SetActive(true);
  94. }
  95. }
  96. SwitchRoute(0);
  97. }
  98. public void SwitchRoute( int Index)
  99. {
  100. if (list_route == null || list_route.Count < Index)
  101. {
  102. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  103. return;
  104. }
  105. if (list_route.Count >Index)
  106. {
  107. for (int i = 0; i < list_route.Count; i++)
  108. {
  109. list_route[i].gameObject.SetActive(false);
  110. }
  111. list_route[Index].gameObject.SetActive(true);
  112. }
  113. }
  114. public void SettingPlayer()
  115. {
  116. Vector3 pos = GetDisPos(player.position);
  117. Debug.Log(player.position + " " + pos);
  118. map_player.localPosition = pos;
  119. }
  120. /// <summary>
  121. /// 按照当前场景比例 算出物体在场景内应该出现的位置
  122. /// localPosition
  123. /// </summary>
  124. private Vector3 GetDisPos( Vector3 pos)
  125. {
  126. if (disX == 0)
  127. {
  128. disX = Mathf.Abs(mapPointB.localPosition.x - mapPointA.localPosition.x);
  129. disZ = Mathf.Abs(mapPointB.localPosition.z - mapPointA.localPosition.z);
  130. }
  131. float x =pos.x / teshMap.disZ;
  132. float z = pos.z / teshMap.disX;
  133. //Debug.Log(x +" "+ z);
  134. Vector3 disPos = new Vector3(x * disX, 0, z * disZ);
  135. return disPos;
  136. }
  137. }