ModelMinMapArrow.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class ModelMinMapArrow : MonoBehaviour {
  6. // Start is called before the first frame update
  7. public TextMesh textMesh;
  8. private int mapScaleX = 3000;
  9. private int mapScaleZ = 3000;
  10. public string typeOfWrok;
  11. public bool arrowOrmodel;//true为小箭头,,false为坦克飞机,人模型
  12. public Material[] arrowmatreial;
  13. public GameObject[] TypeOfWorkModel;
  14. private bool isArrowModle;
  15. private string currentModles;//当前模式
  16. private bool isNullParnet;
  17. private GameObject gamesPoint;//用来 记录 arrow针对于地图 的locatPos;
  18. public bool isOpen;
  19. public string des;//描述
  20. public GameObject map3DParent;
  21. public Transform aIAircraftLookAtpos;
  22. public Vector3 GpsPos;
  23. public int ModelMinMapArrowCurrentMinMapFitIndex;
  24. //public string textGps;//GPS坐标
  25. void Start() {
  26. if(typeOfWrok == "AIAircraft") {
  27. if(aIAircraftLookAtpos != null) {
  28. aIAircraftLookAtpos.gameObject.transform.parent = transform.parent.transform;
  29. }
  30. }
  31. Debug.Log(this.transform.GetChild(1).name + "名字");
  32. //textMesh = this.transform.Find("PosTest").GetComponent<TextMesh>();
  33. // SerColorMg(typeOfWrok);
  34. if(this.transform.Find("twotext") != null)
  35. this.transform.Find("twotext").gameObject.SetActive(false);
  36. textMesh.gameObject.transform.localPosition = new Vector3(textMesh.gameObject.transform.localPosition.x, ModelMinMapArrowCurrentMinMapFitIndex * 3f, textMesh.gameObject.transform.localPosition.z);
  37. //Debug.Log(textMesh.gameObject.transform.localPosition + "文档位置");
  38. }
  39. /*
  40. *
  41. *
  42. *
  43. *
  44. *
  45. */
  46. /// <summary>
  47. /// 箭头赋值颜色
  48. /// </summary>
  49. public void SerColorMg(string str, bool arrowOrmodle, string currentModle) {
  50. // this.transform.GetChild(0).gameObject.GetComponent<MeshRenderer>().material.color = ArrowColor;
  51. isArrowModle = arrowOrmodle;
  52. currentModles = currentModle;
  53. switch(str) {
  54. //友方单位
  55. case "AIAircraft"://无人机
  56. if(arrowOrmodle == true) {
  57. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[0].mainTexture;
  58. }
  59. ShowGameObj(6);
  60. textMesh.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  61. break;
  62. case "FriendlyForcesTypeOfWork1":
  63. if(arrowOrmodle == true) {
  64. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[0].mainTexture;
  65. } else {
  66. ShowGameObj(1);
  67. textMesh.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  68. }
  69. break;
  70. case "FriendlyForcesTypeOfWork2":
  71. if(arrowOrmodle == true) {
  72. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[0].mainTexture;
  73. } else {
  74. ShowGameObj(2);
  75. textMesh.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  76. }
  77. break;
  78. case "FriendlyForcesTank":
  79. if(arrowOrmodle == true) {
  80. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[0].mainTexture;
  81. } else {
  82. ShowGameObj(0);
  83. textMesh.color = new Color(255f / 255f, 4f / 255f, 4f / 255f);//红色
  84. }
  85. break;
  86. //敌方单位
  87. case "EnemyTypeOfWork1":
  88. if(arrowOrmodle == true) {
  89. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[1].mainTexture;
  90. } else {
  91. ShowGameObj(4);
  92. textMesh.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  93. }
  94. // textMesh.gameObject.transform.localPosition = new Vector3(textMesh.gameObject.transform.localPosition.x, ModelMinMapArrowCurrentMinMapFitIndex * 3f, textMesh.gameObject.transform.localPosition.z);
  95. Debug.Log(ModelMinMapArrowCurrentMinMapFitIndex + "位置");
  96. break;
  97. case "EnemyTypeOfWork2":
  98. if(arrowOrmodle == true) {
  99. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[1].mainTexture;
  100. } else {
  101. ShowGameObj(5);
  102. textMesh.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  103. }
  104. break;
  105. case "EnemyTanke":
  106. if(arrowOrmodle == true) {
  107. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[1].mainTexture;
  108. } else {
  109. ShowGameObj(3);
  110. textMesh.color = new Color(0, 125f / 255f, 255f / 255f);//蓝色
  111. }
  112. break;
  113. //中方单位
  114. case "EneutralTypeOfWork1":
  115. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[2].mainTexture;
  116. break;
  117. case "EneutralTypeOfWork2":
  118. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[2].mainTexture;
  119. break;
  120. case "EneutralTanke":
  121. this.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].mainTexture = arrowmatreial[2].mainTexture;
  122. break;
  123. }
  124. if(isArrowModle == false) {
  125. gamesPoint = Instantiate(Resources.Load("LocatArrowPos") as GameObject, this.transform.transform);
  126. gamesPoint.transform.parent = this.transform.parent.transform;
  127. }
  128. }
  129. private void ShowGameObj(int index) {
  130. //if (TypeOfWorkModel[0] == null)
  131. //{
  132. // return;//如过没有为空则跳出
  133. //}
  134. for(int i = 0; i < TypeOfWorkModel.Length; i++) {
  135. if(i == index) {
  136. TypeOfWorkModel[i].SetActive(true);
  137. } else {
  138. TypeOfWorkModel[i].SetActive(false);
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 坐标旋转度赋值
  144. /// </summary>
  145. public void TransformMg(Vector3 ArrowPos) {
  146. //this.transform.position = ArrowPos;
  147. this.transform.Find("text/PosTest").gameObject.GetComponent<TextMesh>().text = ArrowPos.ToString("");
  148. // this.transform.rotation = Quaternion.Euler(Arrowrota);
  149. }
  150. private void LateUpdate() {
  151. textMesh.transform.parent.LookAt(new Vector3(SvrManager.Instance.head.transform.position.x, 0, SvrManager.Instance.head.transform.position.z));
  152. // UpdatePlayerTypeOfWorkMapArrowTrasn(pos);
  153. }
  154. private Vector3 playerTypeOfWorkPos;
  155. private Vector3 playerTypeOfWorkPosStr;
  156. private string playerPosStr;
  157. float PlayerGPS_iNIT_X;
  158. float PlayerGPS_iNIT_Z;
  159. GameObject gamescenes = null;
  160. GameObject scenes4_3D = null;
  161. bool ismap3DParent = false;//True为3DParent移动到箭头下仅运行一次,false为3DParent位移到Gamescence下仅运行一次
  162. public void SetUpdatePlayerTypeOfWorkMapArrowTrasn(String PlayerPos) {
  163. playerTypeOfWorkPos = StrSubstringToV3(PlayerPos);
  164. //Debug.Log("PlayerPos:+===============" + playerTypeOfWorkPos);
  165. if(isOpen == false) {
  166. // AudoLookAT(PlayerPos);
  167. }
  168. if(typeOfWrok == "AIAircraft") {
  169. float MyX = (mapScaleX / this.transform.parent.transform.Find("ModelMinMap").transform.localScale.x) * 2;//比例尺
  170. float MyZ = (mapScaleZ / this.transform.parent.transform.Find("ModelMinMap").transform.localScale.z) * 2;//比例尺
  171. RangofFloorX(playerTypeOfWorkPos.x);
  172. RangofFloorZ(playerTypeOfWorkPos.z);
  173. if(aIAircraftLookAtpos != null) {
  174. aIAircraftLookAtpos.transform.localPosition = new Vector3(playerTypeOfWorkPos.x / MyX, 0.01f, playerTypeOfWorkPos.z / MyZ);
  175. // transform.LookAt(new Vector3(aIAircraftLookAtpos.position.x, 0.01f, aIAircraftLookAtpos.position.z));
  176. transform.LookAt(aIAircraftLookAtpos.transform.position);
  177. //if (GameManager.instacne.scenesName == "scenes4")
  178. //{
  179. // // Debug.Log(transform.localEulerAngles + "旋转度");
  180. // transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);
  181. //}
  182. }
  183. this.transform.localPosition = new Vector3(playerTypeOfWorkPos.x / MyX, 0.01f, playerTypeOfWorkPos.z / MyZ);
  184. } else {
  185. float MyX = (mapScaleX / this.transform.parent.transform.Find("ModelMinMap").transform.localScale.x);//比例尺
  186. float MyZ = (mapScaleZ / this.transform.parent.transform.Find("ModelMinMap").transform.localScale.z);//比例尺
  187. Debug.Log(this.gameObject.name + GpsPos.x);
  188. PlayerGPS_iNIT_X = playerTypeOfWorkPos.x;// - GameManager.instacne.gpsPosInitx;
  189. PlayerGPS_iNIT_Z = playerTypeOfWorkPos.z;// - GameManager.instacne.gpsPosInitz;
  190. // Debug.Log("位置X"+ GameManager.instacne.gpsPosInitx + "原始位置Z" + GameManager.instacne.gpsPosInitz);
  191. // Debug.Log("人物名称: "+ typeOfWrok + "GpsPosX " + GpsPos.x + "GpsPosZ" + GpsPos.z + "+++++++++++++++++++++++++++" + PlayerGPS_iNIT_X + "+++++++" + PlayerGPS_iNIT_Z);
  192. // RangofFloorX(PlayerGPS_iNIT_X);
  193. // RangofFloorZ(PlayerGPS_iNIT_Z);
  194. // Debug.Log((PlayerGPS_iNIT_X *= -1) / MyX + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  195. if(isArrowModle == false)//置身模式
  196. {
  197. //gamesPoint.transform.localPosition = new Vector3(playerTypeOfWorkPos.x / MyX, 0.01f, playerTypeOfWorkPos.z / MyZ);
  198. //Vector3 localPos = gamesPoint.transform.localPosition;
  199. //this.transform.Translate(localPos);
  200. this.transform.localPosition = new Vector3(PlayerGPS_iNIT_Z / MyZ, 0.01f, (PlayerGPS_iNIT_X *= -1) / MyX);
  201. } else {
  202. this.transform.localPosition = new Vector3(PlayerGPS_iNIT_Z / MyZ, 0.01f, (PlayerGPS_iNIT_X *= -1) / MyX);
  203. }
  204. }
  205. if(map3DParent != null) {
  206. if(des == "MakeThisPlayerUser" && GameManager.instacne.scenesName == "scenes4") {
  207. if(map3DParent != null) {
  208. map3DParent.SetActive(true);
  209. isNullParnet = true;
  210. }
  211. } else {
  212. if(map3DParent != null) {
  213. map3DParent.SetActive(false);
  214. isNullParnet = false;
  215. }
  216. }
  217. if(isNullParnet) {
  218. gamescenes = this.transform.parent.parent.parent.gameObject;
  219. scenes4_3D = this.transform.parent.parent.gameObject;
  220. map3DParent.transform.parent = this.transform.parent.parent.parent.transform;
  221. this.transform.parent.parent.parent = map3DParent.transform;
  222. if(ismap3DParent == false) {
  223. ismap3DParent = true;//false时,map3DParent仅赋值一次
  224. map3DParent.transform.position = new Vector3(SvrManager.Instance.head.transform.position.x, -1.7f, SvrManager.Instance.head.transform.position.z);
  225. map3DParent.transform.eulerAngles = new Vector3(0, GameManager.instacne.scence3followPos.transform.eulerAngles.y, 0);
  226. }
  227. } else {
  228. if(GameManager.instacne.scenesName != "scenes4") {
  229. if(ismap3DParent == true) {
  230. ismap3DParent = false;//修改ismap3DParent状态
  231. }
  232. if(scenes4_3D != null) {
  233. scenes4_3D.transform.parent = GameObject.Find("GameScenes").transform;
  234. map3DParent.transform.parent = this.transform;
  235. }
  236. }
  237. }
  238. }
  239. // AudoLookAT(PlayerPos);
  240. //this.GetComponent<Rigidbody>().transform.localPosition= new Vector3(playerTypeOfWorkPos.x / MyX, 0.01f, playerTypeOfWorkPos.z / MyZ);
  241. // this.transform.Translate(localPos);
  242. }
  243. /// <summary>
  244. /// 限制Arrow X轴行走范围(0---1000)
  245. /// </summary>
  246. public void RangofFloorX(float disX) {
  247. if(mapScaleX <= disX) {
  248. if(typeOfWrok == "AIAircraft") {
  249. disX = mapScaleX;
  250. playerTypeOfWorkPos.x = disX;//无人机Pos换算方法
  251. } else {
  252. disX = mapScaleX;
  253. PlayerGPS_iNIT_X = disX;//敌人Pos换算方法
  254. }
  255. }
  256. if(0 >= disX) {
  257. if(typeOfWrok == "AIAircraft") {
  258. playerTypeOfWorkPos.x = 0;
  259. } else {
  260. PlayerGPS_iNIT_X = 0;
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 限制Arrow Z轴行走范围(0---1000)
  266. /// </summary>
  267. public void RangofFloorZ(float disZ) {
  268. if(mapScaleZ <= PlayerGPS_iNIT_Z) {
  269. //PlayerGPS_iNIT_Z = mapScaleZ;
  270. if(typeOfWrok == "AIAircraft") {
  271. disZ = mapScaleZ;
  272. playerTypeOfWorkPos.z = disZ;
  273. } else {
  274. disZ = mapScaleZ;
  275. PlayerGPS_iNIT_Z = disZ;
  276. }
  277. }
  278. if(0 >= disZ) {
  279. if(typeOfWrok == "AIAircraft") {
  280. playerTypeOfWorkPos.z = 0;
  281. } else {
  282. PlayerGPS_iNIT_Z = 0;
  283. }
  284. //PlayerGPS_iNIT_Z = 0;
  285. }
  286. }
  287. public void SetUpdateTextPos(String PlayerPos) {
  288. playerTypeOfWorkPosStr = StrSubstringToV3(PlayerPos);
  289. //if (typeOfWrok == "FriendlyForcesTypeOfWork1")
  290. //{
  291. // Debug.Log("友军坐标" + playerTypeOfWorkPosStr);
  292. //}
  293. if(typeOfWrok == "AIAircraft") {
  294. Vector3 AIAircraft = new Vector3(playerTypeOfWorkPosStr.x + GameManager.instacne.gpsPosInitz, playerTypeOfWorkPosStr.y, playerTypeOfWorkPosStr.z + GameManager.instacne.gpsPosInitz);
  295. textMesh.text = AIAircraft.ToString();
  296. } else if(typeOfWrok == "FriendlyForcesTypeOfWork1" || typeOfWrok == "FriendlyForcesTypeOfWork2" || typeOfWrok == "FriendlyForcesTank")//显示友方沙盘坐标
  297. {
  298. textMesh.text = playerTypeOfWorkPosStr.ToString();
  299. }
  300. }
  301. public void SetGPSPlayerPos(string GpsPos)//接收gps真实坐标
  302. {
  303. //if (typeOfWrok != "AIAircraft")//显示敌方沙盘坐标
  304. //{
  305. // textMesh.text = GpsPos.ToString(); ;
  306. //}
  307. if(typeOfWrok == "EnemyTypeOfWork1" || typeOfWrok == "EnemyTypeOfWork2" || typeOfWrok == "EnemyTanke")//显示敌方沙盘坐标
  308. {
  309. textMesh.text = playerTypeOfWorkPosStr.ToString(); ;
  310. }
  311. }
  312. public Vector3 StrSubstringToV3(string str) {
  313. if(str == null) {
  314. return new Vector3();
  315. }
  316. string[] s = str.Split('_');
  317. return new Vector3(float.Parse(s[0]), float.Parse(s[1]), float.Parse(s[2]));
  318. }
  319. private float rotSpeed = 0.5f;
  320. public void AudoLookAT(string str) {
  321. Vector3 pos = StrSubstringToV3(str);
  322. //Quaternion lookRot = Quaternion.LookRotation(pos); //dir为前方节点的pos
  323. //transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, Mathf.Clamp01(rotSpeed * Time.deltaTime));
  324. transform.LookAt(new Vector3(pos.x, 0, pos.z));
  325. }
  326. }