multiple xr toolkit package
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VRC_WebPanelEditor.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.Build;
  6. using System;
  7. using System.Linq;
  8. namespace VRCSDK2
  9. {
  10. [CustomEditor(typeof(VRC_WebPanel))]
  11. public class VRC_WebPanelEditor : Editor
  12. {
  13. private void InspectorField(string propertyName, string humanName)
  14. {
  15. SerializedProperty propertyField = serializedObject.FindProperty(propertyName);
  16. EditorGUILayout.PropertyField(propertyField, new GUIContent(humanName), true);
  17. }
  18. bool showFiles = false;
  19. bool showHelp = true;
  20. System.Collections.Generic.List<string> directories = null;
  21. System.Collections.Generic.List<string> files = null;
  22. public override void OnInspectorGUI()
  23. {
  24. serializedObject.Update();
  25. EditorGUI.BeginChangeCheck();
  26. EditorGUILayout.BeginVertical();
  27. EditorGUILayout.HelpBox("Do not play any videos with Web Panels, use VRC_VideoPlayerSync instead!", MessageType.Error);
  28. showHelp = EditorGUILayout.Toggle("Show Help", showHelp);
  29. EditorGUILayout.Space();
  30. InspectorField("proximity", "Proximity for Interactivity");
  31. EditorGUILayout.Space();
  32. VRC_WebPanel web = (VRC_WebPanel)target;
  33. if (Application.isPlaying)
  34. {
  35. InspectorField("webRoot", "Web Root");
  36. InspectorField("defaultUrl", "URI");
  37. showFiles = web.webData != null && EditorGUILayout.Foldout(showFiles, web.webData.Count.ToString() + " files imported");
  38. if (showFiles)
  39. foreach (var file in web.webData)
  40. {
  41. EditorGUILayout.BeginHorizontal();
  42. EditorGUILayout.PrefixLabel(file.path);
  43. EditorGUILayout.LabelField(file.data.Length.ToString());
  44. EditorGUILayout.EndHorizontal();
  45. }
  46. }
  47. else
  48. {
  49. SerializedProperty webRoot = serializedObject.FindProperty("webRoot");
  50. RenderDirectoryList(serializedObject, "webRoot", "Path To Web Content");
  51. if (string.IsNullOrEmpty(webRoot.stringValue))
  52. {
  53. InspectorField("defaultUrl", "Start URI");
  54. }
  55. else
  56. {
  57. RenderWebRootSelector(serializedObject, "defaultUrl", "Start Page");
  58. if (showHelp)
  59. {
  60. EditorGUILayout.HelpBox("Javascript API bindings are called with engine.call('methodName', ...), which returns a promise-like object.", MessageType.Info);
  61. EditorGUILayout.HelpBox("Javascript may call ListBindings() to discover available API bindings.", MessageType.Info);
  62. EditorGUILayout.HelpBox("Javascript may listen for the 'onBindingsReady' event to execute script when the page is fully loaded and API bindings are available.", MessageType.Info);
  63. }
  64. }
  65. }
  66. EditorGUILayout.Space();
  67. InspectorField("cookiesEnabled", "Enable Cookies");
  68. InspectorField("interactive", "Is Interactive");
  69. InspectorField("localOnly", "Only Visible Locally");
  70. if (!web.localOnly)
  71. {
  72. InspectorField("syncURI", "Synchronize URI");
  73. InspectorField("syncInput", "Synchronize Mouse Position");
  74. }
  75. InspectorField("transparent", "Transparent Background");
  76. InspectorField("autoFormSubmit", "Input should Submit Forms");
  77. EditorGUILayout.Space();
  78. InspectorField("station", "Interaction Station");
  79. EditorGUILayout.Space();
  80. InspectorField("cursor", "Mouse Cursor Object");
  81. EditorGUILayout.Space();
  82. InspectorField("resolutionWidth", "Resolution Width");
  83. InspectorField("resolutionHeight", "Resolution Height");
  84. InspectorField("displayRegion", "Display Region");
  85. EditorGUILayout.Space();
  86. InspectorField("extraVideoScreens", "Duplicate Screens");
  87. EditorGUILayout.EndVertical();
  88. if (EditorGUI.EndChangeCheck())
  89. serializedObject.ApplyModifiedProperties();
  90. }
  91. private void AddSubDirectories(ref System.Collections.Generic.List<string> l, string root)
  92. {
  93. if (!Directory.Exists(root))
  94. {
  95. return;
  96. }
  97. if (!root.StartsWith(Application.dataPath + Path.DirectorySeparatorChar + "VRCSDK")
  98. || root == Application.dataPath + Path.DirectorySeparatorChar + "VRCSDK" + Path.DirectorySeparatorChar + "Examples" + Path.DirectorySeparatorChar + "Sample Assets" + Path.DirectorySeparatorChar + "WebRoot")
  99. l.Add(root.Substring(Application.dataPath.Length));
  100. string[] subdirectories = Directory.GetDirectories(root);
  101. foreach (string dir in subdirectories)
  102. AddSubDirectories(ref l, dir);
  103. }
  104. private void RenderDirectoryList(SerializedObject obj, string propertyName, string humanName)
  105. {
  106. if (directories == null)
  107. {
  108. directories = new System.Collections.Generic.List<string>();
  109. directories.Add("No Web Content Directory");
  110. AddSubDirectories(ref directories, Application.dataPath + Path.DirectorySeparatorChar);
  111. }
  112. SerializedProperty target = serializedObject.FindProperty(propertyName);
  113. int selectedIdx = target.stringValue == null ? 0 : directories.IndexOf(target.stringValue);
  114. if (selectedIdx < 0 || selectedIdx >= directories.Count)
  115. selectedIdx = 0;
  116. selectedIdx = EditorGUILayout.Popup(humanName, selectedIdx, directories.ToArray());
  117. if (selectedIdx > 0 && selectedIdx < directories.Count)
  118. target.stringValue = directories[selectedIdx];
  119. else
  120. target.stringValue = null;
  121. }
  122. private void AddSubDirectoryFiles(ref System.Collections.Generic.List<string> l, string root)
  123. {
  124. if (!Directory.Exists(root))
  125. return;
  126. string[] files = Directory.GetFiles(root);
  127. foreach (string file in files.Where(f => f.ToLower().EndsWith(".html") || f.ToLower().EndsWith(".htm")))
  128. l.Add(file.Substring(Application.dataPath.Length));
  129. string[] subdirectories = Directory.GetDirectories(root);
  130. foreach (string dir in subdirectories)
  131. AddSubDirectoryFiles(ref l, dir);
  132. }
  133. private void RenderWebRootSelector(SerializedObject obj, string propertyName, string humanName)
  134. {
  135. SerializedProperty webRoot = obj.FindProperty("webRoot");
  136. SerializedProperty target = serializedObject.FindProperty(propertyName);
  137. if (files == null)
  138. {
  139. files = new System.Collections.Generic.List<string>();
  140. AddSubDirectoryFiles(ref files, Application.dataPath + webRoot.stringValue);
  141. if (files.Count == 0)
  142. {
  143. EditorGUILayout.HelpBox("No suitable html files found in Web Content path.", MessageType.Error);
  144. return;
  145. }
  146. }
  147. int selectedIdx = 0;
  148. try
  149. {
  150. System.Uri uri = string.IsNullOrEmpty(target.stringValue) ? null : new Uri(target.stringValue);
  151. selectedIdx = uri == null ? 0 : files.IndexOf(uri.AbsolutePath.Replace('/', System.IO.Path.DirectorySeparatorChar));
  152. if (selectedIdx < 0 || selectedIdx >= files.Count)
  153. selectedIdx = 0;
  154. }
  155. catch { }
  156. selectedIdx = EditorGUILayout.Popup(humanName, selectedIdx, files.ToArray());
  157. if (selectedIdx >= 0 && selectedIdx < files.Count)
  158. {
  159. System.UriBuilder builder = new UriBuilder()
  160. {
  161. Scheme = "file",
  162. Path = files[selectedIdx].Replace(System.IO.Path.DirectorySeparatorChar, '/'),
  163. Host = ""
  164. };
  165. target.stringValue = builder.Uri.ToString();
  166. }
  167. }
  168. }
  169. }
  170. #endif