MapCircularSignManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class MapCircularSignManager : MonoBehaviour {
  7. // Start is called before the first frame update
  8. public GameObject[] TypeOfWorkModel;
  9. public GameObject messagePanel;
  10. public GameObject initRot;
  11. public Text nameText;
  12. public Text posText;
  13. public Text posTextX, posTextZ;
  14. public Text disText;
  15. public TextMesh objHeadUpPostext;
  16. public GameObject dangerousPanl;
  17. private float angleSpeed = 2000f;
  18. public string targetPos;
  19. private bool isRotate = true;
  20. private Vector3 typeOfWorkPosV3;
  21. private Vector3 typeOfWorkRotV3;
  22. // private GameObject PlayerTypeLogoPoint;//产生的人物标识点(小圆球)
  23. public string nameStr, endPosV3, endRotV3;//界面最终显示的值
  24. public float endDis;//距离
  25. private string typeOfWorks;
  26. private string changeUTF8TypeOfWorks;
  27. public Vector3 GpsEnemyPos;//敌人真实的Gps坐标
  28. public float a;
  29. public float b;
  30. public float c;
  31. public Text duifang;
  32. public Text ziji;
  33. public Text zuizhong;
  34. public enum TypeOfWork {
  35. typeNull,
  36. FriendlyForcesTypeOfWork1,
  37. FriendlyForcesTypeOfWork2,
  38. FriendlyForcesTank,
  39. EnemyTypeOfWork1,
  40. EnemyTypeOfWork2,
  41. EnemyTanke,
  42. EneutralTypeOfWork1,
  43. EneutralTypeOfWork2,
  44. EneutralTanke,
  45. AIAircraft,
  46. }
  47. public TypeOfWork typeOfWork;
  48. private float playerY;
  49. private void Start() {
  50. UserExit();
  51. playerY = SvrManager.Instance.head.position.y;
  52. // AddPlayerTypeOfWorkLogoPoint();
  53. //duifang.text = a.ToString();
  54. //ziji.text = b.ToString();
  55. //zuizhong.text = c.ToString();
  56. }
  57. //public void AddPlayerTypeOfWorkLogoPoint()
  58. //{
  59. //GameObject PlayerPoint = Resources.Load("sphere") as GameObject;
  60. // PlayerTypeLogoPoint = Instantiate(PlayerPoint) as GameObject;
  61. // Debug.Log(PlayerTypeLogoPoint.name + "加载物体");
  62. // PlayerTypeLogoPoint.transform.parent = GameObject.Find("GameScenes/Scenes5/huan").transform;
  63. // PlayerTypeLogoPoint.transform.localPosition = new Vector3(0, 0, 0);
  64. // PlayerTypeLogoPoint.transform.rotation = Quaternion.Euler(0, 0, 0);
  65. // PlayerTypeLogoPoint.GetComponent<LookAtPlayer>().player = this.gameObject.transform.GetChild(0).gameObject;
  66. //}
  67. public void SetTypeOfWork(string str) {
  68. typeOfWorks = str;
  69. switch(str) {
  70. case "AIAircraft":
  71. typeOfWork = TypeOfWork.AIAircraft;
  72. objHeadUpPostext.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  73. break;
  74. //友方单位
  75. case "FriendlyForcesTypeOfWork1":
  76. typeOfWork = TypeOfWork.FriendlyForcesTypeOfWork1;
  77. objHeadUpPostext.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  78. break;
  79. case "FriendlyForcesTypeOfWork2":
  80. typeOfWork = TypeOfWork.FriendlyForcesTypeOfWork2;
  81. objHeadUpPostext.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  82. break;
  83. case "FriendlyForcesTank":
  84. typeOfWork = TypeOfWork.FriendlyForcesTank;
  85. objHeadUpPostext.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  86. break;
  87. //敌方单位
  88. case "EnemyTypeOfWork1":
  89. typeOfWork = TypeOfWork.EnemyTypeOfWork1;
  90. objHeadUpPostext.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  91. break;
  92. case "EnemyTypeOfWork2":
  93. typeOfWork = TypeOfWork.EnemyTypeOfWork2;
  94. objHeadUpPostext.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  95. break;
  96. case "EnemyTanke":
  97. typeOfWork = TypeOfWork.EnemyTanke;
  98. objHeadUpPostext.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  99. break;
  100. //中方单位
  101. case "EneutralTypeOfWork1":
  102. typeOfWork = TypeOfWork.EneutralTypeOfWork1;
  103. break;
  104. case "EneutralTypeOfWork2":
  105. typeOfWork = TypeOfWork.EneutralTypeOfWork2;
  106. break;
  107. case "EneutralTanke":
  108. typeOfWork = TypeOfWork.EneutralTanke;
  109. break;
  110. }
  111. TyepEnum();
  112. }
  113. private void TyepEnum() {
  114. switch(typeOfWork) {
  115. case TypeOfWork.typeNull:
  116. ShowGameObj(-1);
  117. break;
  118. case TypeOfWork.FriendlyForcesTypeOfWork1:
  119. ShowGameObj(0);
  120. break;
  121. case TypeOfWork.FriendlyForcesTypeOfWork2:
  122. ShowGameObj(1);
  123. break;
  124. case TypeOfWork.FriendlyForcesTank:
  125. ShowGameObj(2);
  126. break;
  127. case TypeOfWork.EnemyTypeOfWork1:
  128. ShowGameObj(3);
  129. break;
  130. case TypeOfWork.EnemyTypeOfWork2:
  131. ShowGameObj(4);
  132. break;
  133. case TypeOfWork.EnemyTanke:
  134. ShowGameObj(5);
  135. break;
  136. case TypeOfWork.EneutralTypeOfWork1:
  137. ShowGameObj(6);
  138. break;
  139. case TypeOfWork.EneutralTypeOfWork2:
  140. ShowGameObj(7);
  141. break;
  142. case TypeOfWork.EneutralTanke:
  143. ShowGameObj(8);
  144. break;
  145. case TypeOfWork.AIAircraft:
  146. ShowGameObj(9);
  147. break;
  148. }
  149. }
  150. /// <summary>
  151. /// 跟随 设备使用者 并看向指定目标
  152. /// </summary>
  153. private void LateUpdate() {
  154. //if(typeOfWorks == "FriendlyForcesTypeOfWork1") {
  155. // var velocity = Vector3.zero;
  156. // Vector3 handPos = new Vector3(SvrManager.Instance.head.transform.position.x, SvrManager.Instance.head.transform.position.y - 1.7f, SvrManager.Instance.head.transform.position.z);
  157. // // Vector3 handPos = new Vector3(SvrManager.Instance.head.transform.position.x, -1.72f, SvrManager.Instance.head.transform.position.z);
  158. // transform.position = Vector3.SmoothDamp(this.transform.position, handPos, ref velocity, 0.001f, 1000f);
  159. // if(typeOfWorks == "EnemyTypeOfWork1" || typeOfWorks == "EnemyTypeOfWork2" || typeOfWorks == "EnemyTanke")//造的敌人单位
  160. // {
  161. // //ThisInitRotLookAt(GpsEnemyPos.x + "_" + GpsEnemyPos.y + "_" + GpsEnemyPos.z);
  162. // ThisInitRotLookAt(targetPos);
  163. // } else if(typeOfWorks == "FriendlyForcesTypeOfWork1" || typeOfWorks == "FriendlyForcesTypeOfWork2" || typeOfWorks == "FriendlyForcesTank") {
  164. // ThisInitRotLookAt(targetPos);
  165. // } else if(typeOfWorks == "AIAircraft") {
  166. // ThisInitRotLookAt(targetPos);
  167. // }
  168. //} else if(typeOfWorks == "EneutralTypeOfWork1" || typeOfWorks == "EneutralTypeOfWork2" || typeOfWorks == "EneutralTanke") {
  169. var velocity = Vector3.zero;
  170. Vector3 handPos = new Vector3(SvrManager.Instance.head.transform.position.x, playerY - 1.72f, SvrManager.Instance.head.transform.position.z);
  171. // Vector3 handPos = new Vector3(SvrManager.Instance.head.transform.position.x, -1.72f, SvrManager.Instance.head.transform.position.z);
  172. transform.position = Vector3.SmoothDamp(this.transform.position, handPos, ref velocity, 0.001f, 1000f);
  173. if(typeOfWorks == "EnemyTypeOfWork1" || typeOfWorks == "EnemyTypeOfWork2" || typeOfWorks == "EnemyTanke")//造的敌人单位
  174. {
  175. //ThisInitRotLookAt(GpsEnemyPos.x + "_" + GpsEnemyPos.y + "_" + GpsEnemyPos.z);
  176. ThisInitRotLookAt(targetPos);
  177. } else if(typeOfWorks == "FriendlyForcesTypeOfWork1" || typeOfWorks == "FriendlyForcesTypeOfWork2" || typeOfWorks == "FriendlyForcesTank") {
  178. ThisInitRotLookAt(targetPos);
  179. } else if(typeOfWorks == "AIAircraft") {
  180. ThisInitRotLookAt(targetPos);
  181. }
  182. // }
  183. SetTextvalue(nameStr, endPosV3, DisTance());
  184. }
  185. /// <summary>
  186. /// 根据 枚举 显示对应游戏物体
  187. /// </summary>
  188. /// <param name="index">0:友军1 ,1:友军2 ,2:友军坦克 ,3:敌军1 ,4:敌军2 ,5:敌军坦克</param>
  189. private void ShowGameObj(int index) {
  190. for(int i = 0; i < TypeOfWorkModel.Length; i++) {
  191. if(i == index) {
  192. TypeOfWorkModel[i].SetActive(true);
  193. } else {
  194. TypeOfWorkModel[i].SetActive(false);
  195. }
  196. }
  197. }
  198. public void SetTextvalue(string name, string pos, float dis) {
  199. typeOfWorkPosV3 = StrSubstringToV3(endPosV3);
  200. // typeOfWorkRotV3 = StrSubstringToV3(endRotV3);
  201. string v3Posx = typeOfWorkPosV3.x.ToString("f2");
  202. string v3Posy = typeOfWorkPosV3.y.ToString("f2");
  203. string v3Posz = typeOfWorkPosV3.z.ToString("f2");
  204. string v3Rotx = typeOfWorkPosV3.x.ToString("f2");
  205. string v3Roty = typeOfWorkPosV3.y.ToString("f2");
  206. string v3Rotz = typeOfWorkPosV3.z.ToString("f2");
  207. endPosV3 = v3Posx + "," + 0 + "," + v3Posz;
  208. endRotV3 = v3Rotx + "," + 0 + "," + v3Rotz;
  209. nameText.text = name;
  210. Debug.Log(name);
  211. if(name != "" || name != null) {
  212. string[] names = name.Split(',');
  213. UTF8ChangeName(typeOfWorks, names);
  214. }
  215. Vector3 v3Pos = StrSubstringToV3(pos);
  216. //去掉小数点
  217. double dispos = Math.Round(dis);
  218. disText.text = dispos.ToString() + "米";//距离
  219. if(typeOfWorks == "EnemyTypeOfWork1" || typeOfWorks == "EnemyTypeOfWork2" || typeOfWorks == "EnemyTanke")//造的敌人单位
  220. {
  221. posTextX.text = (GpsEnemyPos.x).ToString();
  222. posTextZ.text = (GpsEnemyPos.z).ToString();
  223. // objHeadUpPostext.text = GpsEnemyPos.ToString(); 显示gps
  224. objHeadUpPostext.text = dispos.ToString() + "米";//距离
  225. dangerousPanl.gameObject.SetActive(true);
  226. } else if(typeOfWorks == "FriendlyForcesTypeOfWork1" || typeOfWorks == "FriendlyForcesTypeOfWork2" || typeOfWorks == "FriendlyForcesTank")//造玩家单位
  227. {
  228. posTextX.text = (v3Pos.x).ToString();
  229. posTextZ.text = (v3Pos.z).ToString();
  230. //objHeadUpPostext.text = v3Pos.ToString();显示gps
  231. objHeadUpPostext.text = dispos.ToString() + "米";//距离
  232. dangerousPanl.gameObject.SetActive(false);
  233. }
  234. //10.30改
  235. else if(typeOfWorks == "AIAircraft") {
  236. float AIPosX = v3Pos.x + GameManager.instacne.gpsPosInitx;
  237. float AIPosZ = v3Pos.x + GameManager.instacne.gpsPosInitx;
  238. posTextX.text = AIPosX.ToString();
  239. posTextZ.text = AIPosZ.ToString();
  240. // objHeadUpPostext.text = AIPosX + "," + "0" + "," + AIPosZ;
  241. objHeadUpPostext.text = dispos.ToString() + "米";//距离
  242. dangerousPanl.gameObject.SetActive(false);
  243. }
  244. //10.30改
  245. }
  246. public void UTF8ChangeName(string str, string[] names) {
  247. if(str == "EnemyTypeOfWork1") {
  248. changeUTF8TypeOfWorks = "敌方陆军";
  249. nameText.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  250. } else if(str == "EnemyTypeOfWork2") {
  251. changeUTF8TypeOfWorks = "敌方飞机";
  252. nameText.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  253. } else if(str == "EnemyTanke") {
  254. changeUTF8TypeOfWorks = "敌方坦克";
  255. nameText.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  256. } else if(str == "FriendlyForcesTypeOfWork1") {
  257. changeUTF8TypeOfWorks = "我方陆军";
  258. nameText.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  259. } else if(str == "FriendlyForcesTypeOfWork2") {
  260. changeUTF8TypeOfWorks = "我方飞机";
  261. nameText.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  262. } else if(str == "FriendlyForcesTank") {
  263. changeUTF8TypeOfWorks = "我方坦克";
  264. nameText.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  265. } else if(str == "AIAircraft") {
  266. changeUTF8TypeOfWorks = "我方无人机";
  267. nameText.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  268. }
  269. nameText.text = changeUTF8TypeOfWorks + names[1].ToString();
  270. }
  271. private bool isOpen;
  272. private float openShowTimer = 3;
  273. private float openShowInitTimer = 3;
  274. public void UserEnter() {
  275. isOpen = true;
  276. }
  277. public void UserExit() {
  278. // isOpen = false;
  279. }
  280. private void Update() {
  281. if(isOpen) {
  282. messagePanel.SetActive(true);
  283. if(openShowTimer > 0) {
  284. openShowTimer -= Time.deltaTime;
  285. } else {
  286. messagePanel.SetActive(false);
  287. openShowTimer = openShowInitTimer;
  288. isOpen = false;
  289. }
  290. }
  291. }
  292. private float DisTance() {
  293. // float diss = Vector3.Distance(StrSubstringToV3(endPosV3), SvrManager.Instance.head.transform.position);
  294. //Vector3 playerGpsPos = PosCommunication.shadowPos;
  295. float diss = 0;
  296. if(typeOfWorks == "EnemyTypeOfWork1" || typeOfWorks == "EnemyTypeOfWork2" || typeOfWorks == "EnemyTanke")//造的敌人单位
  297. {
  298. Debug.Log(GpsEnemyPos + "敌人的位置" + PosCommunication.shadowPosStatic + "自己的位置");
  299. diss = Vector3.Distance(GpsEnemyPos, PosCommunication.shadowPosStatic);
  300. } else if(typeOfWorks == "FriendlyForcesTypeOfWork1" || typeOfWorks == "FriendlyForcesTypeOfWork2" || typeOfWorks == "FriendlyForcesTank") {
  301. // Debug.Log(StrSubstringToV3(targetPos) + "友军的位置" + PosCommunication.shadowPos + "我的位置");
  302. diss = Vector3.Distance(StrSubstringToV3(targetPos), PosCommunication.shadowPosStatic);
  303. }
  304. //10.30修改,飞机暂时使用Slam值
  305. else if(typeOfWorks == "AIAircraft") {
  306. diss = Vector3.Distance(StrSubstringToV3(targetPos), SvrManager.Instance.head.transform.position);
  307. }
  308. //10.30修改
  309. return diss;
  310. }
  311. private float gpsX, gpsY, gpsZ;//用来做位置换算
  312. Vector3 v3Pos;
  313. public void ThisInitRotLookAt(string pos) {
  314. //posText.text = pos;//显示协同场景模型坐标位置
  315. //10.29修改 x和z是相反位置
  316. if(typeOfWorks == "EnemyTypeOfWork1" || typeOfWorks == "EnemyTypeOfWork2" || typeOfWorks == "EnemyTanke")//造的敌人单位
  317. {
  318. v3Pos = StrSubstringToV3(pos);
  319. } else if(typeOfWorks == "FriendlyForcesTypeOfWork1" || typeOfWorks == "FriendlyForcesTypeOfWork2" || typeOfWorks == "FriendlyForcesTank") {
  320. // Vector3 targetV3 = StrSubstringToV3(pos);
  321. //gpsX = targetV3.x - PosCommunication.shadowPos.x;
  322. //gpsY = targetV3.y - PosCommunication.shadowPos.y;
  323. //gpsZ = targetV3.z - PosCommunication.shadowPos.z;
  324. // v3Pos = new Vector3(gpsX, gpsY, gpsZ);
  325. v3Pos = StrSubstringToV3(pos);
  326. } else if(typeOfWorks == "AIAircraft") {
  327. v3Pos = StrSubstringToV3(pos);
  328. }
  329. // x和z是相反位置
  330. //10.29修改
  331. if(targetPos != null) {
  332. //Vector3 vec = (target.position - initRot.transform.position);
  333. Vector3 vecs = new Vector3(v3Pos.x - initRot.transform.position.x, 0, v3Pos.z - initRot.transform.position.z);
  334. Quaternion rotate = Quaternion.LookRotation(vecs);
  335. initRot.transform.localRotation = Quaternion.Slerp(initRot.transform.localRotation, rotate, angleSpeed);
  336. }
  337. }
  338. public static Vector3 StrSubstringToV3(string str) {
  339. if(str == null) {
  340. return new Vector3();
  341. }
  342. string[] s = str.Split('_');
  343. return new Vector3(float.Parse(s[0]), float.Parse(s[1]), float.Parse(s[2]));
  344. }
  345. }