/** * Copyright 2019 The Knights Of Unity, created by Pawel Stolarczyk * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using DemoGame.Scripts.Profile; using DemoGame.Scripts.Session; using UnityEngine; using UnityEngine.UI; namespace DemoGame.Scripts.Menus { /// /// Menu displayed to the user at the beginning of the game. /// Blocks any action before connection with the server can be established. /// public class LoadingMenu : SingletonMenu { #region Fields #region Connecting [Space] /// /// Panel containing UI showing connection progress. /// [SerializeField] private GameObject _connectingPanel = null; /// /// Spinning connection icon. /// [SerializeField] private GameObject _loadingIcon = null; /// /// Speed at which is spinning in degrees per second. /// [SerializeField] private float rotationSpeed = -90; #endregion #region Retry [Space] /// /// Panel containing reconnect UI. /// [SerializeField] private GameObject _retryPanel = null; /// /// Reconnect with server button. /// [SerializeField] private Button _retryButton = null; #endregion #endregion #region Mono /// /// Rotates around Z-axis. /// private void Update() { if (base.IsShown == true) { _loadingIcon.transform.Rotate(Vector3.forward, rotationSpeed * Time.deltaTime); } } #endregion #region Methods /// /// Sets up Nakama session success and failure handlers. /// Adds listener to and shows this menu. /// public override void Show() { base.Show(); _retryButton.onClick.AddListener(Retry); AwaitConnection(); } /// /// Shows connection awaiting panel. /// Subscribes to /// and events. /// public void AwaitConnection() { _connectingPanel.SetActive(true); _retryPanel.SetActive(false); NakamaSessionManager.Instance.OnConnectionSuccess += ConnectionSuccess; NakamaSessionManager.Instance.OnNewAccountCreated += NewAccountCreated; NakamaSessionManager.Instance.OnConnectionFailure += ConnectionFailed; } /// /// Hides this panel. /// Unsubscribes from /// and events. /// private void ConnectionSuccess() { NakamaSessionManager.Instance.OnConnectionSuccess -= ConnectionSuccess; NakamaSessionManager.Instance.OnNewAccountCreated -= NewAccountCreated; NakamaSessionManager.Instance.OnConnectionFailure -= ConnectionFailed; MenuManager.Instance.HideTopMenu(); } /// /// Opens /// private void NewAccountCreated() { ConnectionSuccess(); ProfileUpdatePanel.Instance.ShowUpdatePanel(null, false); } /// /// Shows on connection failure. /// private void ConnectionFailed() { _connectingPanel.SetActive(false); _retryPanel.SetActive(true); } /// /// Tries to reconnect with Nakama server. /// private async void Retry() { _connectingPanel.SetActive(true); _retryPanel.SetActive(false); await NakamaSessionManager.Instance.ConnectAsync(); } #endregion } }