Пример #1
0
        private void HandlePushFinished(Screen screen)
        {
            screen.onPushFinished -= HandlePushFinished;

            _state = State.Ready;

            if (_activePushCallback != null)
            {
                _activePushCallback(screen);
                _activePushCallback = null;
            }

            if (CanExecuteNextQueueItem())
            {
                ExecuteNextQueueItem();
            }
        }
Пример #2
0
        private void ExecuteNextQueueItem()
        {
            // Get next queued item.
            QueuedScreen queued = _queue.Dequeue();

            #if PRINT_QUEUE
            DebugPrintQueue(string.Format("[UIManager] Dequeued Screen: {0}, Frame: {1}", queued, Time.frameCount));
            #endif

            if (queued is QueuedScreenPush)
            {
                // Push screen.
                QueuedScreenPush queuedPush = (QueuedScreenPush)queued;
                Screen           screenInstance;

                if (_cache.TryGetValue(queuedPush.prefabName, out screenInstance))
                {
                    // Use cached instance of screen.
                    _cache.Remove(queuedPush.prefabName);

                    #if PRINT_CACHE
                    DebugPrintCache(string.Format("[UIManager] Screen retrieved from Cache: {0}", queuedPush.prefabName));
                    #endif

                    // Move cached to the front of the transfrom heirarchy so that it is sorted properly.
                    screenInstance.transform.SetAsLastSibling();

                    screenInstance.gameObject.SetActive(true);
                }
                else
                {
                    // Instantiate new instance of screen.
                    string path   = System.IO.Path.Combine(resourcePrefabDirectory, queuedPush.prefabName);
                    Screen prefab = Resources.Load <Screen>(path);

                    screenInstance = Object.Instantiate(prefab, rootCanvas.transform);
                    screenInstance.Setup(queuedPush.id, queuedPush.prefabName);
                }

                if (this.inputOrderFixEnabled)
                {
                    this.UpdateSortOrderOverrides();
                }

                // Tell previous top screen that it is losing focus.
                var topScreen = GetTopScreen();
                if (topScreen != null)
                {
                    #if PRINT_FOCUS
                    Debug.Log(string.Format("[UIManager] Lost Focus: {0}", topScreen.id));
                    #endif

                    topScreen.OnFocusLost();
                }

                // Insert new screen at the top of the stack.
                _state = State.Push;
                _stack.Insert(0, screenInstance);

                _activePushCallback = queuedPush.callback;

                #if PRINT_STACK
                DebugPrintStack(string.Format("[UIManager] Pushing Screen: {0}, Frame: {1}", queued.id, Time.frameCount));
                #endif

                screenInstance.onPushFinished += HandlePushFinished;
                screenInstance.OnPush(queuedPush.data);

                if (_queue.Count == 0)
                {
                    #if PRINT_FOCUS
                    Debug.Log(string.Format("[UIManager] Gained Focus: {0}", screenInstance.id));
                    #endif

                    // Screen gains focus when it is on top of the screen stack and no other items in the queue.
                    screenInstance.OnFocus();
                }
            }
            else
            {
                // Pop screen.
                QueuedScreenPop queuedPop   = (QueuedScreenPop)queued;
                Screen          screenToPop = GetTopScreen();

                if (screenToPop.id != queued.id)
                {
                    throw new System.Exception(string.Format("The top screen does not match the queued pop. " +
                                                             "TopScreen: {0}, QueuedPop: {1}", screenToPop.id, queued.id));
                }

                #if PRINT_FOCUS
                Debug.Log(string.Format("[UIManager] Lost Focus: {0}", screenToPop.id));
                #endif

                screenToPop.OnFocusLost();

                _state = State.Pop;
                _stack.RemoveAt(0);

                // Tell new top screen that it is gaining focus.
                var newTopScreen = GetTopScreen();
                if (newTopScreen != null)
                {
                    if (_queue.Count == 0)
                    {
                        #if PRINT_FOCUS
                        Debug.Log(string.Format("[UIManager] Gained Focus: {0}", newTopScreen.id));
                        #endif

                        // Screen gains focus when it is on top of the screen stack and no other items in the queue.
                        newTopScreen.OnFocus();
                    }
                }

                _activePopCallback = queuedPop.callback;

                #if PRINT_STACK
                DebugPrintStack(string.Format("[UIManager] Popping Screen: {0}, Frame: {1}", queued.id, Time.frameCount));
                #endif

                screenToPop.onPopFinished += HandlePopFinished;
                screenToPop.OnPop();
            }
        }
Пример #3
0
        /// <summary>
        /// Queue the screen to be pushed onto the screen stack.
        /// Callback will be invoked when the screen is pushed to the stack.
        /// </summary>
        public void QueuePush(RP.Screen.Id id, RP.Screen.Data data, string prefabName = null, PushedDelegate callback = null)
        {
            string prefab = prefabName ?? id.defaultPrefabName;

            #if PRINT_QUEUE
            DebugPrintQueue(string.Format("[UIManager] QueuePush id: {0}, prefabName: {1}", id, prefab));
            #endif

            if (GetScreen(id) != null)
            {
                Debug.LogWarning(string.Format("Screen {0} already exists in the stack. Ignoring push request.", id));
                return;
            }

            //if (ScreenWillExist(id))
            //{
            //    Debug.LogWarning(string.Format("Screen {0} will exist in the stack after the queue is fully executed. Ignoring push request.", id));
            //    return;
            //}

            QueuedScreenPush push = new QueuedScreenPush();
            push.id         = id;
            push.data       = data;
            push.prefabName = prefab;
            push.callback   = callback;

            _queue.Enqueue(push);

            #if PRINT_QUEUE
            DebugPrintQueue(string.Format("[UIManager] Enqueued Screen: {0}, Frame: {1}", push, Time.frameCount));
            #endif

            if (CanExecuteNextQueueItem())
            {
                ExecuteNextQueueItem();
            }
        }