NxrPluginEditor.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. namespace Nxr.Internal
  5. {
  6. public class NxrPluginEditor : EditorWindow
  7. {
  8. // Add menu named "My Window" to the Window menu
  9. [MenuItem("NibiruXR/Plugin Manager", false, 88)]
  10. static void Init()
  11. {
  12. // Get existing open window or if none, make a new one:
  13. NxrPluginEditor window = (NxrPluginEditor)EditorWindow.GetWindow(typeof(NxrPluginEditor));
  14. window.minSize = new Vector2(320, 300);
  15. string data = Read("AndroidManifest.xml");
  16. string[] lines = data.Split('\n');
  17. for (int i = 0, l = lines.Length; i < l; i++)
  18. {
  19. string lineContent = lines[i];
  20. if (lineContent.Contains("NIBIRU_PLUGIN_IDS"))
  21. {
  22. if (lineContent.Contains("6DOF"))
  23. {
  24. window.mSixDofMode = NxrSDKApi.Instance.GetSettingsAssetConfig().mSixDofMode;
  25. }
  26. if (lineContent.Contains("RECORD"))
  27. {
  28. window.useRecord = true;
  29. }
  30. if (lineContent.Contains("MARKER"))
  31. {
  32. window.useMarker = true;
  33. }
  34. break;
  35. }
  36. }
  37. window.Show();
  38. }
  39. //public bool useVoice = false;
  40. //public bool useGesture = false;
  41. public bool useRecord = false;
  42. public bool useMarker = false;
  43. //public bool useRecoginize = false;
  44. SixDofMode mSixDofMode = SixDofMode.Head_3Dof_Ctrl_3Dof;
  45. void OnGUI()
  46. {
  47. GUILayout.Space(20);
  48. GUILayout.Label("Choose the plugin ids :", EditorStyles.boldLabel);
  49. //"VOICE", "6DOF", "GESTURE", "RECORD", "MARKER", "RECOGINIZE"
  50. //useVoice = EditorGUILayout.Toggle("VOICE", useVoice);
  51. mSixDofMode = (SixDofMode)EditorGUILayout.EnumPopup(new GUIContent("SixDof Mode", "Choose SixDofMode: position is absolute data") , mSixDofMode);
  52. //useGesture = EditorGUILayout.Toggle("GESTURE", useGesture);
  53. useRecord = EditorGUILayout.Toggle("RECORD", useRecord);
  54. useMarker = EditorGUILayout.Toggle("MARKER", useMarker);
  55. //useRecoginize = EditorGUILayout.Toggle("RECOGINIZE", useRecoginize);
  56. bool use6DOF = mSixDofMode != SixDofMode.Head_3Dof_Ctrl_3Dof;
  57. string declaredStr = //(useVoice ? "VOICE," : "") +
  58. (use6DOF ? "6DOF," : "") +
  59. //(useGesture ? "GESTURE," : "") +
  60. (useRecord ? "RECORD," : "") +
  61. (useMarker ? "MARKER," : "");
  62. //(useRecoginize ? "RECOGINIZE," : "");
  63. if (declaredStr.Length > 0 && declaredStr.LastIndexOf(',') == declaredStr.Length - 1)
  64. {
  65. declaredStr = declaredStr.Remove(declaredStr.Length - 1);
  66. }
  67. int useCameraPluginIds = 0;
  68. if(useMarker)
  69. {
  70. useCameraPluginIds++;
  71. }
  72. if (useRecord)
  73. {
  74. useCameraPluginIds++;
  75. }
  76. EditorGUILayout.TextField("Declared Plugin Ids",
  77. declaredStr
  78. , EditorStyles.boldLabel);
  79. if (GUILayout.Button("Confirm Choose", GUILayout.Width(200), GUILayout.Height(30)))
  80. {
  81. var asset = NxrSDKApi.Instance.GetSettingsAssetConfig();
  82. asset.mSixDofMode = mSixDofMode;
  83. EditorUtility.SetDirty(asset);
  84. AssetDatabase.SaveAssets();
  85. //must Refresh
  86. AssetDatabase.Refresh();
  87. if (useCameraPluginIds > 1)
  88. {
  89. bool res = EditorUtility.DisplayDialog("Conflict Warning",
  90. "Can not choose them at the same time : [GESTURE][RECORD] [MARKER] [RECOGINIZE], because they all use camera, choose only one is ok !!!",
  91. "Force Use", "Cancel");
  92. Debug.Log(res);
  93. if(res)
  94. {
  95. NxrPluginEditor window = (NxrPluginEditor)EditorWindow.GetWindow(typeof(NxrPluginEditor));
  96. window.Close();
  97. } else
  98. {
  99. return;
  100. }
  101. }
  102. else
  103. {
  104. NxrPluginEditor window = (NxrPluginEditor)EditorWindow.GetWindow(typeof(NxrPluginEditor));
  105. window.Close();
  106. }
  107. string data = Read("AndroidManifest.xml");
  108. string[] lines = data.Split('\n');
  109. string newdata = "";
  110. for (int i = 0, l = lines.Length; i < l; i++)
  111. {
  112. string lineContent = lines[i];
  113. if (lineContent.Contains("NIBIRU_PLUGIN_IDS"))
  114. {
  115. lineContent = " <meta-data android:value=\"" + declaredStr + "\" android:name=\"NIBIRU_PLUGIN_IDS\"/>";
  116. }
  117. newdata = newdata + lineContent + "\n";
  118. }
  119. Write("AndroidManifest.xml", newdata);
  120. }
  121. }
  122. private static string GetFullPath(string name)
  123. {
  124. return Application.dataPath + "/Plugins/Android/" + name;
  125. }
  126. /// 检测文件是否存在Application.dataPath目录
  127. public static bool IsFileExists(string fileName)
  128. {
  129. if (fileName.Equals(string.Empty))
  130. {
  131. return false;
  132. }
  133. return System.IO.File.Exists(GetFullPath(fileName));
  134. }
  135. /// 从对应文件读取数据
  136. public static string Read(string fileName)
  137. {
  138. if (IsFileExists(fileName))
  139. {
  140. return System.IO.File.ReadAllText(GetFullPath(fileName));
  141. }
  142. else
  143. {
  144. return "";
  145. }
  146. }
  147. public static void Write(string fileName, string contents)
  148. {
  149. int lio = fileName.LastIndexOf('/');
  150. if (lio > 0) CreateFolder(fileName.Substring(0, fileName.LastIndexOf('/')));
  151. System.IO.TextWriter tw = new System.IO.StreamWriter(GetFullPath(fileName), false);
  152. tw.Write(contents);
  153. tw.Close();
  154. AssetDatabase.Refresh();
  155. }
  156. /// 检测是否存在文件夹
  157. public static bool IsFolderExists(string folderPath)
  158. {
  159. if (folderPath.Equals(string.Empty))
  160. {
  161. return false;
  162. }
  163. return System.IO.Directory.Exists(GetFullPath(folderPath));
  164. }
  165. /// 创建文件夹
  166. public static void CreateFolder(string folderPath)
  167. {
  168. if (!IsFolderExists(folderPath))
  169. {
  170. System.IO.Directory.CreateDirectory(GetFullPath(folderPath));
  171. AssetDatabase.Refresh();
  172. }
  173. }
  174. }
  175. }