BrowserEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. namespace ZenFulcrum.EmbeddedBrowser {
  7. [CustomEditor(typeof(Browser))]
  8. [CanEditMultipleObjects]
  9. public class BrowserEditor : Editor {
  10. private static string script = "document.body.style.background = 'red';\n";
  11. private static string html = "Hello, <i>world</i>!\n";
  12. private static string[] commandNames;
  13. private static BrowserNative.FrameCommand[] commandValues;
  14. static BrowserEditor() {
  15. var els = Enum.GetValues(typeof(BrowserNative.FrameCommand));
  16. commandNames = new string[els.Length];
  17. commandValues = new BrowserNative.FrameCommand[els.Length];
  18. int i = 0;
  19. foreach (BrowserNative.FrameCommand cmd in els) {
  20. commandNames[i] = cmd.ToString();
  21. commandValues[i] = cmd;
  22. ++i;
  23. }
  24. }
  25. public override bool RequiresConstantRepaint() {
  26. //The buttons get stale if we don't keep repainting them.
  27. return Application.isPlaying;
  28. }
  29. public override void OnInspectorGUI() {
  30. base.OnInspectorGUI();
  31. if (Application.isPlaying && !serializedObject.isEditingMultipleObjects) {
  32. RenderActions();
  33. } else if (!Application.isPlaying) {
  34. GUILayout.Label("Additional options available in play mode");
  35. }
  36. }
  37. private void RenderActions() {
  38. var browser = (Browser)target;
  39. if (!browser.IsReady) {
  40. GUILayout.Label("Starting...");
  41. return;
  42. }
  43. GUILayout.BeginVertical("box");
  44. GUILayout.Label("Apply items above:");
  45. GUILayout.BeginHorizontal("box");
  46. {
  47. if (GUILayout.Button("Go to URL")) browser.LoadURL(serializedObject.FindProperty("_url").stringValue, false);
  48. if (GUILayout.Button("Force to URL")) browser.Url = serializedObject.FindProperty("_url").stringValue;
  49. if (GUILayout.Button("Resize")) {
  50. browser.Resize(
  51. serializedObject.FindProperty("_width").intValue,
  52. serializedObject.FindProperty("_height").intValue
  53. );
  54. }
  55. if (GUILayout.Button("Set Zoom")) browser.Zoom = serializedObject.FindProperty("_zoom").floatValue;
  56. }
  57. GUILayout.EndHorizontal();
  58. GUILayout.Label("Actions:");
  59. GUILayout.BeginHorizontal();
  60. {
  61. GUI.enabled = browser.CanGoBack;
  62. if (GUILayout.Button("Go back")) browser.GoBack();
  63. GUI.enabled = browser.CanGoForward;
  64. if (GUILayout.Button("Go forward")) browser.GoForward();
  65. GUI.enabled = true;
  66. if (browser.IsLoadingRaw) {
  67. if (GUILayout.Button("Stop")) browser.Stop();
  68. } else {
  69. if (GUILayout.Button("Reload")) browser.Reload();
  70. }
  71. if (GUILayout.Button("Force Reload")) browser.Reload(true);
  72. }
  73. GUILayout.EndHorizontal();
  74. GUILayout.BeginHorizontal();
  75. {
  76. if (GUILayout.Button("Show Dev Tools")) browser.ShowDevTools();
  77. if (GUILayout.Button("Hide Dev Tools")) browser.ShowDevTools(false);
  78. }
  79. GUILayout.EndHorizontal();
  80. GUILayout.Label("Script:");
  81. script = GUILayout.TextArea(script);
  82. if (GUILayout.Button("Eval JavaScript")) {
  83. browser.EvalJS(script, "editor command");
  84. }
  85. int pVal = EditorGUILayout.Popup("Send Command:", -1, commandNames);
  86. if (pVal != -1) {
  87. browser.SendFrameCommand(commandValues[pVal]);
  88. }
  89. GUILayout.Label("HTML:");
  90. html = GUILayout.TextArea(html);
  91. if (GUILayout.Button("Load HTML")) {
  92. browser.LoadHTML(html);
  93. }
  94. GUILayout.EndVertical();
  95. }
  96. }
  97. }