SimpleScripting.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace ZenFulcrum.EmbeddedBrowser {
  4. /**
  5. * A simple set of examples of scripting/JavaScript interacting with a Browser.
  6. */
  7. [RequireComponent(typeof(Browser))]
  8. public class SimpleScripting : MonoBehaviour {
  9. private Browser browser;
  10. public void Start() {
  11. browser = GetComponent<Browser>();
  12. //Load some HTML. Normally you'd want to use BrowserAssets + localGame://,
  13. //but let's just put everything here for simplicity/visibility right now.
  14. browser.LoadHTML(@"
  15. <button style='background: green; color: white' onclick='greenButtonClicked(event.x, event.y)'>Green Button</button>
  16. <br><br>
  17. Username: <input type='text' id='username' value='CouchPotato47'>
  18. <br><br>
  19. <div id='box' style='width: 200px; height: 200px;border: 1px solid black'>
  20. Click ""Change Color""
  21. </div>
  22. <script>
  23. function changeColor(r, g, b, text) {
  24. var el = document.getElementById('box');
  25. el.style.background = 'rgba(' + (r * 255) + ', ' + (g * 255) + ', ' + (b * 255) + ', 1)';
  26. el.textContent = text;
  27. }
  28. </script>
  29. ");
  30. //Set up a function. Notice how the <button> above calls this function when it's clicked.
  31. browser.RegisterFunction("greenButtonClicked", args => {
  32. //Args is an array of arguments passed to the function.
  33. //args[n] is a JSONNode. When you use it, it will implicitly cast to the type at hand.
  34. int xPos = args[0];
  35. int yPos = args[1];
  36. //Note that if, say, args[0] was a string instead of an integer we'd get default(int) above.
  37. //See JSONNode.cs for more information.
  38. Debug.Log("The <color=green>green</color> button was clicked at " + xPos + ", " + yPos);
  39. });
  40. }
  41. /** Fetches the username and logs it to the Unity console. */
  42. public void GetUsername() {
  43. browser.EvalJS("document.getElementById('username').value").Then(username => {
  44. Debug.Log("The username is: " + username);
  45. }).Done();
  46. //Note that the fetch above is asynchronous, this line of code will happen before the Debug.Log above:
  47. Debug.Log("Fetching username");
  48. }
  49. private int colorIdx;
  50. private Color[] colors = {
  51. new Color(1, 0, 0),
  52. new Color(1, 1, 0),
  53. new Color(1, 1, 1),
  54. new Color(1, 1, 0),
  55. };
  56. /** Changes the color of the box on the page by calling a function in the page. */
  57. public void ChangeColor() {
  58. var color = colors[colorIdx++ % colors.Length];
  59. browser.CallFunction("changeColor", color.r, color.g, color.b, "Selection Number " + colorIdx).Done();
  60. }
  61. /** Fetches the username and logs it to the Unity console (alternate method). */
  62. public void GetUsername2() {
  63. StartCoroutine(_GetUsername2());
  64. }
  65. private IEnumerator _GetUsername2() {
  66. //This method is more useful if you are already in a coroutine, or you have a lot of logic
  67. //you need to work with that is ugly/painful to express with .Then() chaining.
  68. var promise = browser.EvalJS("document.getElementById('username').value");
  69. Debug.Log("Fetching username");
  70. //Waits for the JS to run and get the result back to us.
  71. yield return promise.ToWaitFor();
  72. Debug.Log("The username is: " + promise.Value);
  73. }
  74. }
  75. }