SimpleController.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. namespace ZenFulcrum.EmbeddedBrowser {
  5. /**
  6. * A very simple controller for a Browser.
  7. * Call GoToURLInput() to go to the URL typed in urlInput
  8. */
  9. [RequireComponent(typeof(Browser))]
  10. public class SimpleController : MonoBehaviour {
  11. private Browser browser;
  12. public InputField urlInput;
  13. public void Start() {
  14. browser = GetComponent<Browser>();
  15. //Update text field when the browser loads a page
  16. browser.onNavStateChange += () => {
  17. if (!urlInput.isFocused) {
  18. //but only if it's not focused (don't steal kill something the user is typing)
  19. urlInput.text = browser.Url;
  20. }
  21. };
  22. urlInput.onEndEdit.AddListener(v => {
  23. if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter)) {
  24. //only nav if they hit enter, not just because they unfocused it
  25. urlInput.DeactivateInputField();
  26. GoToURLInput();
  27. } else {
  28. //revert text to URL if it updated while they were typing
  29. urlInput.text = browser.Url;
  30. }
  31. });
  32. }
  33. public void GoToURLInput() {
  34. browser.Url = urlInput.text;
  35. }
  36. }
  37. }