11using System ;
2+ using System . Collections ;
23using UnityEngine ;
34using UnityEngine . UI ;
45
@@ -17,9 +18,17 @@ public class ManualIpConfiguration : MonoBehaviour
1718 [ Tooltip ( "Hides the UI when connection is made." ) ]
1819 public bool HideWhenConnected ;
1920
21+ /// <summary>
22+ /// Hides the UI after this many seconds.
23+ /// </summary>
24+ [ Range ( 0.1f , 5f ) ]
25+ [ Tooltip ( "Hides the UI after this many seconds." ) ]
26+ public float HideAfterSeconds = 1f ;
27+
2028 /// <summary>
2129 /// How many seconds before server connection times out.
2230 /// </summary>
31+ [ Range ( 1 , 30 ) ]
2332 [ Tooltip ( "How many seconds before server connection times out." ) ]
2433 public int Timeout = 5 ;
2534
@@ -35,34 +44,55 @@ public class ManualIpConfiguration : MonoBehaviour
3544
3645 private bool isTryingToConnect ;
3746
47+ private bool firstRun ;
48+
3849 private void Awake ( )
3950 {
4051 ipAddress . text = PlayerPrefs . GetString ( "SharingServerIp" , "Not Connected" ) ;
52+ firstRun = true ;
4153 }
4254
43- private void OnEnable ( )
55+ private void Start ( )
4456 {
4557 if ( SharingStage . Instance != null )
4658 {
4759 SharingStage . Instance . SharingManagerConnected += OnConnected ;
4860 SharingStage . Instance . SharingManagerDisconnected += OnDisconnected ;
4961 }
62+ else
63+ {
64+ Debug . LogError ( "Unable to subscribe to Sharing Stage!" ) ;
65+ }
66+ }
5067
51- ConnectToSharingService ( ) ;
68+ private void OnEnable ( )
69+ {
70+ if ( ! firstRun )
71+ {
72+ isTryingToConnect = true ;
73+ ConnectToSharingService ( ) ;
74+ }
75+ else
76+ {
77+ firstRun = false ;
78+ }
5279 }
5380
5481 private void Update ( )
5582 {
5683 if ( timerRunning && timer - Time . time < 0 )
5784 {
58- isTryingToConnect = false ;
59- OnDisconnected ( ) ;
85+ if ( isTryingToConnect )
86+ {
87+ isTryingToConnect = false ;
88+ OnDisconnected ( ) ;
6089
61- PlayerPrefs . SetString ( "SharingServerIp" , "Not Connected" ) ;
90+ PlayerPrefs . SetString ( "SharingServerIp" , "Not Connected" ) ;
91+ }
6292 }
6393 }
6494
65- private void OnDisable ( )
95+ private void OnDestroy ( )
6696 {
6797 if ( SharingStage . Instance != null )
6898 {
@@ -89,16 +119,17 @@ private void CheckConnection()
89119 private void OnConnected ( object sender = null , EventArgs e = null )
90120 {
91121 timerRunning = false ;
92- isTryingToConnect = false ;
93122 connectionIndicator . color = Color . green ;
94123 ipAddress . text = SharingStage . Instance . Connection . GetRemoteAddress ( ) . ToString ( ) ;
95124
96125 PlayerPrefs . SetString ( "SharingServerIp" , ipAddress . text ) ;
97126
98- if ( HideWhenConnected )
127+ if ( HideWhenConnected && isTryingToConnect )
99128 {
100- gameObject . SetActive ( false ) ;
129+ StartCoroutine ( Hide ( ) ) ;
101130 }
131+
132+ isTryingToConnect = false ;
102133 }
103134
104135 private void OnDisconnected ( object sender = null , EventArgs e = null )
@@ -114,6 +145,9 @@ private void OnDisconnected(object sender = null, EventArgs e = null)
114145
115146 public void InputString ( string intput )
116147 {
148+ timerRunning = false ;
149+ isTryingToConnect = false ;
150+
117151 if ( ipAddress . text . Contains ( "Connected" ) || ipAddress . text . Contains ( "127.0.0.1" ) )
118152 {
119153 ipAddress . text = string . Empty ;
@@ -127,6 +161,9 @@ public void InputString(string intput)
127161
128162 public void DeleteLastCharacter ( )
129163 {
164+ timerRunning = false ;
165+ isTryingToConnect = false ;
166+
130167 if ( ! string . IsNullOrEmpty ( ipAddress . text ) )
131168 {
132169 ipAddress . text = ipAddress . text . Substring ( 0 , ipAddress . text . Length - 1 ) ;
@@ -135,11 +172,17 @@ public void DeleteLastCharacter()
135172
136173 public void ClearIpAddressString ( )
137174 {
175+ timerRunning = false ;
176+ isTryingToConnect = false ;
177+
138178 ipAddress . text = "" ;
139179 }
140180
141181 public void ConnectToSharingService ( )
142182 {
183+ timerRunning = false ;
184+ isTryingToConnect = false ;
185+
143186 if ( ipAddress . text . Contains ( "Connected" ) )
144187 {
145188 OnDisconnected ( ) ;
@@ -151,5 +194,12 @@ public void ConnectToSharingService()
151194 SharingStage . Instance . ConnectToServer ( ipAddress . text , SharingStage . Instance . ServerPort ) ;
152195 CheckConnection ( ) ;
153196 }
197+
198+ private IEnumerator Hide ( )
199+ {
200+ yield return new WaitForSeconds ( HideAfterSeconds ) ;
201+
202+ gameObject . SetActive ( false ) ;
203+ }
154204 }
155205}
0 commit comments