Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

привет у меня есть класс UINavigator в Unity и я бы хотел чтобы ты помог мне вынести…

  1. User

    привет у меня есть класс UINavigator в Unity и я бы хотел чтобы ты помог мне вынести работу с Input в какой ни будь IInputService public class UINavigator : MonoBehaviour { public event Action OnNextScreen; public event Action OnEarlyScreen;

    private PanelItem currentPanel = null; private int selectedPopupIndex = 0; private bool isEmptyScreen = false; private int selectedCurrentPopupIndex = 0;

    public void SetCurrentPopup(PanelItem panelItem) { currentPanel = panelItem;

    if (currentPanel.Popup.Length != 0) { ResetPopupSelection(); EventSystem.current.SetSelectedGameObject(currentPanel.Popup[currentPanel.SelectedPopupIndex].gameObject); currentPanel.Popup[currentPanel.SelectedPopupIndex].Highlighted(); selectedCurrentPopupIndex = currentPanel.SelectedPopupIndex; } else { isEmptyScreen = true; } }

    private void Update() { if (isEmptyScreen) { HandleEmptyScreenInput(); } else { HandlePopupInput(); } }

    private void HandleEmptyScreenInput() { if (currentPanel.NextItemName == ItemName.None) { if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.LeftArrow)) { OnEarlyScreen?.Invoke(); } } else { if (Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.RightArrow)) { OnNextScreen?.Invoke(currentPanel.NextItemName); } if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.LeftArrow)) { OnEarlyScreen?.Invoke(); } } }

    private void HandlePopupInput() { if (EventSystem.current.currentSelectedGameObject == null) { EventSystem.current.SetSelectedGameObject(currentPanel.Popup[selectedPopupIndex].gameObject); }

    if (Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.UpArrow)) { selectedPopupIndex = selectedCurrentPopupIndex; SelectFocus(-1); } if (Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.DownArrow)) { selectedPopupIndex = selectedCurrentPopupIndex; SelectFocus(1); }

    if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.LeftArrow)) { OnEarlyScreen?.Invoke(); } if (Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.RightArrow)) { currentPanel.Popup[selectedPopupIndex].Selected(); currentPanel.SelectedPopupIndex = selectedPopupIndex; OnNextScreen?.Invoke(currentPanel.Popup[selectedPopupIndex].WhichScreen); } }

    private void SelectFocus(int direction) { EventSystem.current.SetSelectedGameObject(null);

    ResetPopupSelection();

    selectedPopupIndex = (selectedPopupIndex + direction + currentPanel.Popup.Length) % currentPanel.Popup.Length; EventSystem.current.SetSelectedGameObject(currentPanel.Popup[selectedPopupIndex].gameObject); currentPanel.Popup[selectedPopupIndex].Highlighted(); selectedCurrentPopupIndex = selectedPopupIndex; }

    private void ResetPopupSelection() { isEmptyScreen = false;

    EventSystem.current.SetSelectedGameObject(null);

    foreach (var item in currentPanel.Popup) { item.Deselected(); } } }

  2. Assistant

    Для вынесения работы с Input в отдельный сервис IInputService, вам придется создать интерфейс IInputService и имплементировать его в классе InputService, который будет обрабатывать логику связанную с вводом. Затем вы можете использовать этот сервис вместо непосредственного доступа к Input в классе UINavigator.

    Примерно так будет выглядеть новый код:

    public interface IInputService { bool GetUpArrowKeyDown(); bool GetDownArrowKeyDown(); bool GetLeftArrowKeyDown(); bool GetRightArrowKeyDown(); bool GetKeypad4KeyDown(); bool GetKeypad6KeyDown(); }

    public class InputService : IInputService { public bool GetUpArrowKeyDown() { return Input.GetKeyDown(KeyCode.UpArrow); }

    public bool GetDownArrowKeyDown()
    {
        return Input.GetKeyDown(KeyCode.DownArrow);
    }
    
    public bool GetLeftArrowKeyDown()
    {
        return Input.GetKeyDown(KeyCode.LeftArrow);
    }
    
    public bool GetRightArrowKeyDown()
    {
        return Input.GetKeyDown(KeyCode.RightArrow);
    }
    
    public bool GetKeypad4KeyDown()
    {
        return Input.GetKeyDown(KeyCode.Keypad4);
    }
    
    public bool GetKeypad6KeyDown()
    {
        return Input.GetKeyDown(KeyCode.Keypad6);
    }
    

    }

    public class UINavigator : MonoBehaviour { private IInputService inputService;

    // Инициализация сервиса в Awake
    private void Awake()
    {
        inputService = new InputService();
    }
    
    // Обработка ввода, используя IInputService
    private void HandleEmptyScreenInput()
    {
        if (currentPanel.NextItemName == ItemName.None)
        {
            if (inputService.GetKeypad4KeyDown() || inputService.GetLeftArrowKeyDown())
            {
                OnEarlyScreen?.Invoke();
            }
        }
        else
        {
            if (inputService.GetKeypad6KeyDown() || inputService.GetRightArrowKeyDown())
            {
                OnNextScreen?.Invoke(currentPanel.NextItemName);
            }
            if (inputService.GetKeypad4KeyDown() || inputService.GetLeftArrowKeyDown())
            {
                OnEarlyScreen?.Invoke();
            }
        }
    }
    
    private void HandlePopupInput()
    {
        if (EventSystem.current.currentSelectedGameObject == null)
        {
            EventSystem.current.SetSelectedGameObject(currentPanel.Popup[selectedPopupIndex].gameObject);
        }
    
        if (inputService.GetUpArrowKeyDown())
        {
            selectedPopupIndex = selectedCurrentPopupIndex;
            SelectFocus(-1);
        }
        if (inputService.GetDownArrowKeyDown())
        {
            selectedPopupIndex = selectedCurrentPopupIndex;
            SelectFocus(1);
        }
    
        if (inputService.GetLeftArrowKeyDown())
        {
            OnEarlyScreen?.Invoke();
        }
        if (inputService.GetRightArrowKeyDown())
        {
            currentPanel.Popup[selectedPopupIndex].Selected();
            currentPanel.SelectedPopupIndex = selectedPopupIndex;
            OnNextScreen?.Invoke(currentPanel.Popup[selectedPopupIndex].WhichScreen);
        }
    }
    

    }

    Теперь можно использовать InputService для обработки логики ввода вместо непосредственного доступа к Input. Вы сможете создать экземпляр IInputService, предположим, внутри класса GameManager и передать его UINavigator для работы с вводом.