Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using UnityEngine.InputSystem;
  7. [System.Serializable]
  8. public class InputManager : MonoBehaviour
  9. {
  10.     protected ScrollRect scrollRect;
  11.     protected RectTransform contentPanel;
  12.  
  13.     public TMP_InputField searchBar, userInput, currentInput;
  14.  
  15.  
  16.     public MainPlayerControls controls;
  17.    
  18.  
  19.     private void Awake()
  20.     {
  21.         searchBar.enabled = false;
  22.         userInput.enabled = true;
  23.         currentInput = userInput;
  24.         KeepFocus();
  25.        
  26.         controls = new MainPlayerControls();
  27.  
  28.         // the "context" variable stores a few things in it, but since we're just using a button it doesn't matter what we call it -- just a variable
  29.         controls.Player.Search.performed += context => ChangeFocus(searchBar);
  30.         controls.Player.CancelSearch.performed += context => ChangeFocus(userInput);
  31.  
  32.        
  33.     }
  34.  
  35.     private void Update()
  36.     {
  37.         if (currentInput.isFocused == false)
  38.         {
  39.             KeepFocus();
  40.         }
  41.     }
  42.  
  43.  
  44.     public void KeepFocus()
  45.     {
  46.         currentInput.Select();
  47.         currentInput.ActivateInputField();
  48.     }
  49.  
  50.     public void ChangeFocus(TMP_InputField i)
  51.     {
  52.         Debug.Log("PLAYER HIT SEARCH.");
  53.         currentInput = i;
  54.         KeepFocus();
  55.     }
  56.  
  57.     public void SnapTo(RectTransform target)
  58.     {
  59.         Canvas.ForceUpdateCanvases();
  60.  
  61.         contentPanel.anchoredPosition =
  62.             (Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)
  63.             - (Vector2)scrollRect.transform.InverseTransformPoint(target.position);
  64.     }
  65.     private void OnEnable()
  66.     {
  67.         controls.Enable();
  68.     }
  69.     private void OnDisable()
  70.     {
  71.         controls.Disable();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement