20 private const int MaximumCharacterLength = 15;
22 public string IpAddress {
get {
return ipAddress.text; } }
27 [Tooltip(
"Hides the UI when connection is made.")]
34 [Tooltip(
"Hides the UI after this many seconds.")]
35 public float HideAfterSeconds = 1f;
41 [Tooltip(
"How many seconds before server connection times out.")]
42 public int Timeout = 5;
45 private Text ipAddress = null;
48 private Image connectionIndicator = null;
50 private bool timerRunning;
54 private bool isTryingToConnect;
56 private bool firstRun;
60 ipAddress.text = PlayerPrefs.GetString(
"SharingServerIp",
"Not Connected");
73 Debug.LogError(
"Unable to subscribe to Sharing Stage!");
77 private void OnEnable()
82 isTryingToConnect =
true;
83 ConnectToSharingService();
87 isTryingToConnect =
false;
93 if (timerRunning && timer - Time.time < 0)
95 if (isTryingToConnect)
97 isTryingToConnect =
false;
100 PlayerPrefs.SetString(
"SharingServerIp",
"Not Connected");
105 private void OnDestroy()
114 private void CheckConnection()
121 else if (!timerRunning)
123 timer = Time.time + Timeout;
128 private void OnConnected(
object sender = null, EventArgs e = null)
130 timerRunning =
false;
131 connectionIndicator.color = Color.green;
134 PlayerPrefs.SetString(
"SharingServerIp", ipAddress.text);
136 if (HideWhenConnected && isTryingToConnect)
138 StartCoroutine(Hide());
141 isTryingToConnect =
false;
144 private void OnDisconnected(
object sender = null, EventArgs e = null)
146 timerRunning =
false;
148 if (!isTryingToConnect)
150 connectionIndicator.color = Color.red;
151 ipAddress.text =
"Not Connected";
157 timerRunning =
false;
158 isTryingToConnect =
false;
160 if (ipAddress.text.Contains(
"Connected") || ipAddress.text.Contains(
"127.0.0.1"))
162 ipAddress.text =
string.Empty;
165 if (ipAddress.text.Length < MaximumCharacterLength)
167 ipAddress.text += input;
173 timerRunning =
false;
174 isTryingToConnect =
false;
176 if (!
string.IsNullOrEmpty(ipAddress.text))
178 ipAddress.text = ipAddress.text.Substring(0, ipAddress.text.Length - 1);
184 timerRunning =
false;
185 isTryingToConnect =
false;
192 timerRunning =
false;
193 isTryingToConnect =
false;
201 isTryingToConnect =
true;
202 connectionIndicator.color = Color.yellow;
207 private IEnumerator Hide()
209 yield
return new WaitForSeconds(HideAfterSeconds);
211 gameObject.SetActive(
false);