-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch_Ip.cs
75 lines (63 loc) · 2.17 KB
/
Search_Ip.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Networking;
//��ѯ����ip
public class Search_Ip : MonoBehaviour
{
public static Search_Ip ins;
public string player_ip = null;
private const string apiUrl = "http://httpbin.org/ip";
// Start is called before the first frame update
private void Awake()
{
ins = this;
}
private void Start()
{
StartCoroutine(FetchPublicIP());
}
IEnumerator FetchPublicIP()
{
using (UnityWebRequest www = UnityWebRequest.Get(apiUrl))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Error fetching public IP: " + www.error);
}
else
{
string responseText = www.downloadHandler.text;
ParseIPResponse(responseText);
}
}
}
//��ѯip���ص����ݸ�ʽ
[System.Serializable]
public class TextCallback
{
public string origin;
}
private void ParseIPResponse(string response)
{
// Parse the response to extract the public IP address
// The response might be in JSON format, so you need to use a JSON parser or string manipulation
// to extract the relevant information.
// This is just a basic example and may need modification based on the actual response format.
string[] splitResponse = response.Split(':');
if (splitResponse.Length > 1)
{
//�������ص�����
TextCallback textCallback = JsonUtility.FromJson<TextCallback>(response);
string ipAddress = textCallback.origin;
Debug.Log("Public IP Address: " + ipAddress);
player_ip = ipAddress;
}
else
{
Debug.LogError("Unable to parse public IP address from response.");
}
}
}