1.直接利用三方API获取:
1.1 利用bilibili的api 【未知稳定性】
public void Awake() {StartCoroutine(GetLocationInfoNew());}/// <summary>/// 利用bilibili的接口通过ip直接获取城市信息/// </summary>IEnumerator GetLocationInfoNew() {//UnityWebRequest publicIpReq = UnityWebRequest.Get(@"https://api.live.bilibili.com/client/v1/Ip/getInfoNew");var publicIpReq = new UnityWebRequest("https://api.live.bilibili.com/client/v1/Ip/getInfoNew", UnityWebRequest.kHttpVerbGET);publicIpReq.downloadHandler = new DownloadHandlerBuffer();yield return publicIpReq.SendWebRequest();if (!string.IsNullOrEmpty(publicIpReq.error)) {Debug.Log($"获取城市信息失败:{publicIpReq.error}");yield break;}var info = publicIpReq.downloadHandler.text;Debug.Log(info);//将json解析为objectvar resData = JsonUtility.FromJson<ResponseRootData>(info);Debug.Log($"address:{resData.data.addr}|province:{resData.data.province}|city:{resData.data.city}");}#region 用于接收返回值json的反序列化数据[System.Serializable]public class ResponseRootData {public int code;public string message;public ResponseData data;}[System.Serializable]public class ResponseData {public string addr;public string country;public string province;public string city;public string isp;public string latitude;public string longitude;}#endregion
lua代码
local info = request.downloadHandler.textlog:Info(info)local json_parser_ = RapidJsonParser.new()info = json_parser_:Decode(info)if IsNull(info) or IsNull(info.data) thenre