Advertisement
halleman19

update mapping from server in editor | unity3d

Jan 7th, 2025 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | Software | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Xml;
  4. using System.Collections;
  5. using UnityEngine.Networking;
  6.  
  7. namespace g144.debug
  8. {
  9.     [ExecuteInEditMode]
  10.     public class gameMapping_topMenu : MonoBehaviour
  11.     {
  12.         private string titlePar = "markersHouse";
  13.        
  14.         private static gameMapping_topMenu cache;
  15.  
  16.         private void OnEnable()
  17.         {
  18.             cache = this;
  19.         }
  20.  
  21.         [MenuItem("mapping/Update house markers")]
  22.         public static void updateHouseMarkers()
  23.         {
  24.             cache.reqUpdateMarkers();
  25.         }
  26.  
  27.         private void reqUpdateMarkers()
  28.         {
  29.             StartCoroutine(getMarkersInfo());
  30.         }
  31.  
  32.         private IEnumerator getMarkersInfo()
  33.         {
  34.             Debug.LogWarning("send request. please wait...");
  35.  
  36.             string url = "http://127.0.0.1/house.php";
  37.  
  38.             WWWForm form = new WWWForm();
  39.             form.AddField("action", "getMarkers"); // response snapshot https://imgur.com/a/IzchLvB
  40.  
  41.             UnityWebRequest req = UnityWebRequest.Post(url, form);
  42.             yield return req.SendWebRequest();
  43.  
  44.             webResponse resp = GlobalEnv.parseWebReq(req); // method snapshot https://imgur.com/a/EaTgZY1
  45.  
  46.             Transform markerPar = GameObject.Find(titlePar).transform;
  47.  
  48.             if (markerPar)
  49.             {
  50.                 int countOldMarkers = markerPar.childCount;
  51.  
  52.                 for (int i = 0; i < countOldMarkers; i++)
  53.                     DestroyImmediate(markerPar.GetChild(0).gameObject);
  54.             }
  55.             else
  56.                 markerPar = new GameObject(titlePar).transform;
  57.  
  58.             if (resp.isSuccess)
  59.             {
  60.                 XmlNodeList nodes = resp.node.ChildNodes;
  61.  
  62.                 for(int i = 0; i < nodes.Count; i++)
  63.                 {
  64.                     string[] cord = nodes[i]["cord"].InnerText.Split(',');
  65.  
  66.                     Transform marker = new GameObject(string.Format("marker_{0}", i)).transform;
  67.                     marker.parent = markerPar;
  68.                     marker.position = new Vector3(float.Parse(cord[0]), float.Parse(cord[1]), float.Parse(cord[2]));
  69.                 }
  70.             }
  71.             else
  72.                 Debug.LogWarning("No find markers");
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement