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.

VRCSdkControlPanelContent.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using VRC.Core;
  7. public partial class VRCSdkControlPanel : EditorWindow
  8. {
  9. const int PageLimit = 20;
  10. static List<ApiAvatar> uploadedAvatars = null;
  11. static List<ApiWorld> uploadedWorlds = null;
  12. public static Dictionary<string, Texture2D> ImageCache = new Dictionary<string, Texture2D>();
  13. static List<string> justDeletedContents;
  14. static List<ApiAvatar> justUpdatedAvatars;
  15. static EditorCoroutine fetchingAvatars = null, fetchingWorlds = null;
  16. private static string searchString = "";
  17. private static bool WorldsToggle = true;
  18. private static bool AvatarsToggle = true;
  19. const int SCROLLBAR_RESERVED_REGION_WIDTH = 50;
  20. const int WORLD_DESCRIPTION_FIELD_WIDTH = 140;
  21. const int WORLD_IMAGE_BUTTON_WIDTH = 100;
  22. const int WORLD_IMAGE_BUTTON_HEIGHT = 100;
  23. const int WORLD_RELEASE_STATUS_FIELD_WIDTH = 150;
  24. const int COPY_WORLD_ID_BUTTON_WIDTH = 75;
  25. const int DELETE_WORLD_BUTTON_WIDTH = 75;
  26. const int WORLD_ALL_INFORMATION_MAX_WIDTH = WORLD_DESCRIPTION_FIELD_WIDTH + WORLD_IMAGE_BUTTON_WIDTH + WORLD_RELEASE_STATUS_FIELD_WIDTH + COPY_WORLD_ID_BUTTON_WIDTH + DELETE_WORLD_BUTTON_WIDTH + SCROLLBAR_RESERVED_REGION_WIDTH;
  27. const int WORLD_REDUCED_INFORMATION_MAX_WIDTH = WORLD_DESCRIPTION_FIELD_WIDTH + WORLD_IMAGE_BUTTON_WIDTH + WORLD_RELEASE_STATUS_FIELD_WIDTH + SCROLLBAR_RESERVED_REGION_WIDTH;
  28. const int AVATAR_DESCRIPTION_FIELD_WIDTH = 140;
  29. const int AVATAR_IMAGE_BUTTON_WIDTH = WORLD_IMAGE_BUTTON_WIDTH;
  30. const int AVATAR_IMAGE_BUTTON_HEIGHT = WORLD_IMAGE_BUTTON_HEIGHT;
  31. const int AVATAR_RELEASE_STATUS_FIELD_WIDTH = 150;
  32. const int SET_AVATAR_STATUS_BUTTON_WIDTH = 100;
  33. const int COPY_AVATAR_ID_BUTTON_WIDTH = COPY_WORLD_ID_BUTTON_WIDTH;
  34. const int DELETE_AVATAR_BUTTON_WIDTH = DELETE_WORLD_BUTTON_WIDTH;
  35. const int AVATAR_ALL_INFORMATION_MAX_WIDTH = AVATAR_DESCRIPTION_FIELD_WIDTH + AVATAR_IMAGE_BUTTON_WIDTH + AVATAR_RELEASE_STATUS_FIELD_WIDTH + SET_AVATAR_STATUS_BUTTON_WIDTH + COPY_AVATAR_ID_BUTTON_WIDTH + DELETE_AVATAR_BUTTON_WIDTH + SCROLLBAR_RESERVED_REGION_WIDTH;
  36. const int AVATAR_REDUCED_INFORMATION_MAX_WIDTH = AVATAR_DESCRIPTION_FIELD_WIDTH + AVATAR_IMAGE_BUTTON_WIDTH + AVATAR_RELEASE_STATUS_FIELD_WIDTH + SCROLLBAR_RESERVED_REGION_WIDTH;
  37. const int MAX_ALL_INFORMATION_WIDTH = WORLD_ALL_INFORMATION_MAX_WIDTH > AVATAR_ALL_INFORMATION_MAX_WIDTH ? WORLD_ALL_INFORMATION_MAX_WIDTH : AVATAR_ALL_INFORMATION_MAX_WIDTH;
  38. const int MAX_REDUCED_INFORMATION_WIDTH = WORLD_REDUCED_INFORMATION_MAX_WIDTH > AVATAR_REDUCED_INFORMATION_MAX_WIDTH ? WORLD_REDUCED_INFORMATION_MAX_WIDTH : AVATAR_REDUCED_INFORMATION_MAX_WIDTH;
  39. public static void ClearContent()
  40. {
  41. if (uploadedWorlds != null)
  42. uploadedWorlds = null;
  43. if (uploadedAvatars != null)
  44. uploadedAvatars = null;
  45. ImageCache.Clear();
  46. }
  47. IEnumerator FetchUploadedData()
  48. {
  49. if (!RemoteConfig.IsInitialized())
  50. RemoteConfig.Init();
  51. if (!APIUser.IsLoggedInWithCredentials)
  52. yield break;
  53. ApiCache.ClearResponseCache();
  54. VRCCachedWWW.ClearOld();
  55. if (fetchingAvatars == null)
  56. fetchingAvatars = EditorCoroutine.Start(() => FetchAvatars());
  57. if (fetchingWorlds == null)
  58. fetchingWorlds = EditorCoroutine.Start(() => FetchWorlds());
  59. }
  60. static void FetchAvatars(int offset = 0)
  61. {
  62. ApiAvatar.FetchList(
  63. delegate (IEnumerable<ApiAvatar> obj)
  64. {
  65. Debug.LogFormat("<color=yellow>Fetching Avatar Bucket {0}</color>", offset);
  66. if (obj.FirstOrDefault() != null)
  67. fetchingAvatars = EditorCoroutine.Start(() =>
  68. {
  69. var l = obj.ToList();
  70. int count = l.Count;
  71. SetupAvatarData(l);
  72. FetchAvatars(offset + count);
  73. });
  74. else
  75. {
  76. fetchingAvatars = null;
  77. foreach (ApiAvatar a in uploadedAvatars)
  78. DownloadImage(a.id, a.thumbnailImageUrl);
  79. }
  80. },
  81. delegate (string obj)
  82. {
  83. Debug.LogError("Error fetching your uploaded avatars:\n" + obj);
  84. fetchingAvatars = null;
  85. },
  86. ApiAvatar.Owner.Mine,
  87. ApiAvatar.ReleaseStatus.All,
  88. null,
  89. PageLimit,
  90. offset,
  91. ApiAvatar.SortHeading.None,
  92. ApiAvatar.SortOrder.Descending,
  93. null,
  94. null,
  95. true,
  96. false,
  97. null,
  98. false
  99. );
  100. }
  101. static void FetchWorlds(int offset = 0)
  102. {
  103. ApiWorld.FetchList(
  104. delegate (IEnumerable<ApiWorld> obj)
  105. {
  106. Debug.LogFormat("<color=yellow>Fetching World Bucket {0}</color>", offset);
  107. if (obj.FirstOrDefault() != null)
  108. fetchingWorlds = EditorCoroutine.Start(() =>
  109. {
  110. var l = obj.ToList();
  111. int count = l.Count;
  112. SetupWorldData(l);
  113. FetchWorlds(offset + count);
  114. });
  115. else
  116. {
  117. fetchingWorlds = null;
  118. foreach (ApiWorld w in uploadedWorlds)
  119. DownloadImage(w.id, w.thumbnailImageUrl);
  120. }
  121. },
  122. delegate (string obj)
  123. {
  124. Debug.LogError("Error fetching your uploaded worlds:\n" + obj);
  125. fetchingWorlds = null;
  126. },
  127. ApiWorld.SortHeading.Updated,
  128. ApiWorld.SortOwnership.Mine,
  129. ApiWorld.SortOrder.Descending,
  130. offset,
  131. PageLimit,
  132. "",
  133. null,
  134. null,
  135. null,
  136. "",
  137. ApiWorld.ReleaseStatus.All,
  138. null,
  139. null,
  140. true,
  141. false);
  142. }
  143. static void SetupWorldData(List<ApiWorld> worlds)
  144. {
  145. if (worlds == null || uploadedWorlds == null)
  146. return;
  147. worlds.RemoveAll(w => w == null || w.name == null || uploadedWorlds.Any(w2 => w2.id == w.id));
  148. if (worlds.Count > 0)
  149. {
  150. uploadedWorlds.AddRange(worlds);
  151. uploadedWorlds.Sort((w1, w2) => w1.name.CompareTo(w2.name));
  152. }
  153. }
  154. static void SetupAvatarData(List<ApiAvatar> avatars)
  155. {
  156. if (avatars == null || uploadedAvatars == null )
  157. return;
  158. avatars.RemoveAll(a => a == null || a.name == null || uploadedAvatars.Any(a2 => a2.id == a.id));
  159. if (avatars.Count > 0)
  160. {
  161. uploadedAvatars.AddRange(avatars);
  162. uploadedAvatars.Sort((w1, w2) => w1.name.CompareTo(w2.name));
  163. }
  164. }
  165. static void DownloadImage(string id, string url)
  166. {
  167. if (ImageCache.ContainsKey(id) && ImageCache[id] != null)
  168. return;
  169. System.Action<WWW> onDone = (www) =>
  170. {
  171. if (string.IsNullOrEmpty(www.error))
  172. {
  173. try
  174. { // converting Texture2D to use linear color space fixes issue with SDK world & avatar thumbnails appearing too dark (also enables mipmaps to improve appearance of thumbnails)
  175. Texture2D newTexture2DWithLinearEnabled;
  176. newTexture2DWithLinearEnabled = new Texture2D(4, 4, TextureFormat.DXT1, true, true);
  177. www.LoadImageIntoTexture(newTexture2DWithLinearEnabled);
  178. ImageCache[id] = newTexture2DWithLinearEnabled;
  179. }
  180. catch (System.Exception e)
  181. {
  182. Debug.LogException(e);
  183. }
  184. }
  185. else if (ImageCache.ContainsKey(id))
  186. ImageCache.Remove(id);
  187. };
  188. EditorCoroutine.Start(VRCCachedWWW.Get(url, onDone));
  189. }
  190. Vector2 contentScrollPos;
  191. bool OnGUIUserInfo()
  192. {
  193. bool updatedContent = false;
  194. if (!RemoteConfig.IsInitialized())
  195. RemoteConfig.Init();
  196. if (APIUser.IsLoggedInWithCredentials && uploadedWorlds != null && uploadedAvatars != null)
  197. {
  198. contentScrollPos = EditorGUILayout.BeginScrollView(contentScrollPos);
  199. GUIStyle descriptionStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  200. descriptionStyle.wordWrap = true;
  201. bool expandedLayout = (position.width > MAX_ALL_INFORMATION_WIDTH);
  202. EditorGUILayout.BeginHorizontal();
  203. searchString = EditorGUILayout.TextField(searchString, GUI.skin.FindStyle("SearchTextField"));
  204. GUIStyle searchButtonStyle = searchString == string.Empty
  205. ? GUI.skin.FindStyle("SearchCancelButtonEmpty")
  206. : GUI.skin.FindStyle("SearchCancelButton");
  207. if (GUILayout.Button(string.Empty, searchButtonStyle))
  208. {
  209. searchString = string.Empty;
  210. GUI.FocusControl(null);
  211. }
  212. EditorGUILayout.EndHorizontal();
  213. if (uploadedWorlds.Count > 0)
  214. {
  215. EditorGUILayout.Space();
  216. EditorGUILayout.BeginHorizontal();
  217. EditorGUILayout.LabelField("WORLDS", EditorStyles.boldLabel, GUILayout.ExpandWidth(false), GUILayout.Width(58));
  218. WorldsToggle = EditorGUILayout.Foldout(WorldsToggle, new GUIContent(""));
  219. EditorGUILayout.EndHorizontal();
  220. EditorGUILayout.Space();
  221. if (WorldsToggle)
  222. {
  223. List<ApiWorld> tmpWorlds = new List<ApiWorld>();
  224. if (uploadedWorlds.Count > 0)
  225. tmpWorlds = new List<ApiWorld>(uploadedWorlds);
  226. foreach (ApiWorld w in tmpWorlds)
  227. {
  228. if (justDeletedContents != null && justDeletedContents.Contains(w.id))
  229. {
  230. uploadedWorlds.Remove(w);
  231. continue;
  232. }
  233. if (!w.name.ToLowerInvariant().Contains(searchString.ToLowerInvariant()))
  234. {
  235. continue;
  236. }
  237. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  238. EditorGUILayout.BeginHorizontal(GUILayout.Width(WORLD_IMAGE_BUTTON_WIDTH));
  239. if (ImageCache.ContainsKey(w.id))
  240. {
  241. if (GUILayout.Button(ImageCache[w.id], GUILayout.Height(WORLD_IMAGE_BUTTON_HEIGHT),
  242. GUILayout.Width(WORLD_IMAGE_BUTTON_WIDTH)))
  243. {
  244. Application.OpenURL(w.imageUrl);
  245. }
  246. }
  247. else
  248. {
  249. if (GUILayout.Button("", GUILayout.Height(WORLD_IMAGE_BUTTON_HEIGHT),
  250. GUILayout.Width(WORLD_IMAGE_BUTTON_WIDTH)))
  251. {
  252. Application.OpenURL(w.imageUrl);
  253. }
  254. }
  255. if (expandedLayout)
  256. {
  257. EditorGUILayout.BeginHorizontal();
  258. EditorGUILayout.LabelField(w.name, descriptionStyle,
  259. GUILayout.Width(position.width - MAX_ALL_INFORMATION_WIDTH +
  260. WORLD_DESCRIPTION_FIELD_WIDTH));
  261. }
  262. else
  263. {
  264. EditorGUILayout.BeginVertical();
  265. EditorGUILayout.LabelField(w.name, descriptionStyle);
  266. }
  267. EditorGUILayout.LabelField("Release Status: " + w.releaseStatus,
  268. GUILayout.Width(WORLD_RELEASE_STATUS_FIELD_WIDTH));
  269. if (GUILayout.Button("Copy ID", GUILayout.Width(COPY_WORLD_ID_BUTTON_WIDTH)))
  270. {
  271. TextEditor te = new TextEditor();
  272. te.text = w.id;
  273. te.SelectAll();
  274. te.Copy();
  275. }
  276. if (GUILayout.Button("Delete", GUILayout.Width(DELETE_WORLD_BUTTON_WIDTH)))
  277. {
  278. if (EditorUtility.DisplayDialog("Delete " + w.name + "?",
  279. "Are you sure you want to delete " + w.name + "? This cannot be undone.", "Delete",
  280. "Cancel"))
  281. {
  282. foreach (VRC.Core.PipelineManager pm in FindObjectsOfType<VRC.Core.PipelineManager>()
  283. .Where(pm => pm.blueprintId == w.id))
  284. {
  285. pm.blueprintId = "";
  286. pm.completedSDKPipeline = false;
  287. UnityEditor.EditorUtility.SetDirty(pm);
  288. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(pm.gameObject.scene);
  289. UnityEditor.SceneManagement.EditorSceneManager.SaveScene(pm.gameObject.scene);
  290. }
  291. API.Delete<ApiWorld>(w.id);
  292. uploadedWorlds.RemoveAll(world => world.id == w.id);
  293. if (ImageCache.ContainsKey(w.id))
  294. ImageCache.Remove(w.id);
  295. if (justDeletedContents == null) justDeletedContents = new List<string>();
  296. justDeletedContents.Add(w.id);
  297. updatedContent = true;
  298. }
  299. }
  300. if (expandedLayout)
  301. EditorGUILayout.EndHorizontal();
  302. else
  303. EditorGUILayout.EndVertical();
  304. EditorGUILayout.EndHorizontal();
  305. EditorGUILayout.EndHorizontal();
  306. EditorGUILayout.Space();
  307. }
  308. }
  309. }
  310. if (uploadedAvatars.Count > 0)
  311. {
  312. EditorGUILayout.Space();
  313. EditorGUILayout.BeginHorizontal();
  314. EditorGUILayout.LabelField("AVATARS", EditorStyles.boldLabel, GUILayout.ExpandWidth(false), GUILayout.Width(65));
  315. AvatarsToggle = EditorGUILayout.Foldout(AvatarsToggle, new GUIContent(""));
  316. EditorGUILayout.EndHorizontal();
  317. EditorGUILayout.Space();
  318. if (AvatarsToggle)
  319. {
  320. List<ApiAvatar> tmpAvatars = new List<ApiAvatar>();
  321. if (uploadedAvatars.Count > 0)
  322. tmpAvatars = new List<ApiAvatar>(uploadedAvatars);
  323. if (justUpdatedAvatars != null)
  324. {
  325. foreach (ApiAvatar a in justUpdatedAvatars)
  326. {
  327. int index = tmpAvatars.FindIndex((av) => av.id == a.id);
  328. if (index != -1)
  329. tmpAvatars[index] = a;
  330. }
  331. }
  332. foreach (ApiAvatar a in tmpAvatars)
  333. {
  334. if (justDeletedContents != null && justDeletedContents.Contains(a.id))
  335. {
  336. uploadedAvatars.Remove(a);
  337. continue;
  338. }
  339. if (!a.name.ToLowerInvariant().Contains(searchString.ToLowerInvariant()))
  340. {
  341. continue;
  342. }
  343. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  344. EditorGUILayout.BeginHorizontal(GUILayout.Width(AVATAR_DESCRIPTION_FIELD_WIDTH));
  345. if (ImageCache.ContainsKey(a.id))
  346. {
  347. if (GUILayout.Button(ImageCache[a.id], GUILayout.Height(AVATAR_IMAGE_BUTTON_HEIGHT),
  348. GUILayout.Width(AVATAR_IMAGE_BUTTON_WIDTH)))
  349. {
  350. Application.OpenURL(a.imageUrl);
  351. }
  352. }
  353. else
  354. {
  355. if (GUILayout.Button("", GUILayout.Height(AVATAR_IMAGE_BUTTON_HEIGHT),
  356. GUILayout.Width(AVATAR_IMAGE_BUTTON_WIDTH)))
  357. {
  358. Application.OpenURL(a.imageUrl);
  359. }
  360. }
  361. if (expandedLayout)
  362. EditorGUILayout.BeginHorizontal();
  363. else
  364. EditorGUILayout.BeginVertical();
  365. EditorGUILayout.LabelField(a.name, descriptionStyle,
  366. GUILayout.Width(expandedLayout
  367. ? position.width - MAX_ALL_INFORMATION_WIDTH + AVATAR_DESCRIPTION_FIELD_WIDTH
  368. : AVATAR_DESCRIPTION_FIELD_WIDTH));
  369. EditorGUILayout.LabelField("Release Status: " + a.releaseStatus,
  370. GUILayout.Width(AVATAR_RELEASE_STATUS_FIELD_WIDTH));
  371. string oppositeReleaseStatus = a.releaseStatus == "public" ? "private" : "public";
  372. if (GUILayout.Button("Make " + oppositeReleaseStatus,
  373. GUILayout.Width(SET_AVATAR_STATUS_BUTTON_WIDTH)))
  374. {
  375. a.releaseStatus = oppositeReleaseStatus;
  376. a.SaveReleaseStatus((c) =>
  377. {
  378. ApiAvatar savedBP = (ApiAvatar) c.Model;
  379. if (justUpdatedAvatars == null) justUpdatedAvatars = new List<ApiAvatar>();
  380. justUpdatedAvatars.Add(savedBP);
  381. },
  382. (c) =>
  383. {
  384. Debug.LogError(c.Error);
  385. EditorUtility.DisplayDialog("Avatar Updated",
  386. "Failed to change avatar release status", "OK");
  387. });
  388. }
  389. if (GUILayout.Button("Copy ID", GUILayout.Width(COPY_AVATAR_ID_BUTTON_WIDTH)))
  390. {
  391. TextEditor te = new TextEditor();
  392. te.text = a.id;
  393. te.SelectAll();
  394. te.Copy();
  395. }
  396. if (GUILayout.Button("Delete", GUILayout.Width(DELETE_AVATAR_BUTTON_WIDTH)))
  397. {
  398. if (EditorUtility.DisplayDialog("Delete " + a.name + "?",
  399. "Are you sure you want to delete " + a.name + "? This cannot be undone.", "Delete",
  400. "Cancel"))
  401. {
  402. foreach (VRC.Core.PipelineManager pm in FindObjectsOfType<VRC.Core.PipelineManager>()
  403. .Where(pm => pm.blueprintId == a.id))
  404. {
  405. pm.blueprintId = "";
  406. pm.completedSDKPipeline = false;
  407. UnityEditor.EditorUtility.SetDirty(pm);
  408. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(pm.gameObject.scene);
  409. UnityEditor.SceneManagement.EditorSceneManager.SaveScene(pm.gameObject.scene);
  410. }
  411. API.Delete<ApiAvatar>(a.id);
  412. uploadedAvatars.RemoveAll(avatar => avatar.id == a.id);
  413. if (ImageCache.ContainsKey(a.id))
  414. ImageCache.Remove(a.id);
  415. if (justDeletedContents == null) justDeletedContents = new List<string>();
  416. justDeletedContents.Add(a.id);
  417. updatedContent = true;
  418. }
  419. }
  420. if (expandedLayout)
  421. EditorGUILayout.EndHorizontal();
  422. else
  423. EditorGUILayout.EndVertical();
  424. EditorGUILayout.EndHorizontal();
  425. EditorGUILayout.EndHorizontal();
  426. EditorGUILayout.Space();
  427. }
  428. }
  429. }
  430. EditorGUILayout.EndScrollView();
  431. if ((updatedContent) && (null != window)) window.Reset();
  432. return true;
  433. }
  434. else
  435. {
  436. return false;
  437. }
  438. }
  439. void ShowContent()
  440. {
  441. if (uploadedWorlds == null || uploadedAvatars == null)
  442. {
  443. if (uploadedWorlds == null)
  444. uploadedWorlds = new List<ApiWorld>();
  445. if (uploadedAvatars == null)
  446. uploadedAvatars = new List<ApiAvatar>();
  447. EditorCoroutine.Start(FetchUploadedData());
  448. }
  449. if( fetchingWorlds != null || fetchingAvatars != null )
  450. {
  451. GUILayout.BeginVertical(boxGuiStyle, GUILayout.Width(SdkWindowWidth));
  452. EditorGUILayout.Space();
  453. EditorGUILayout.LabelField("Fetching Records", titleGuiStyle);
  454. EditorGUILayout.Space();
  455. GUILayout.EndVertical();
  456. }
  457. else
  458. {
  459. GUILayout.BeginVertical(boxGuiStyle, GUILayout.Width(SdkWindowWidth));
  460. EditorGUILayout.Space();
  461. EditorGUILayout.BeginHorizontal();
  462. GUILayout.Label("Fetch updated records from the VRChat server");
  463. if( GUILayout.Button("Fetch") )
  464. {
  465. ClearContent();
  466. }
  467. EditorGUILayout.EndHorizontal();
  468. EditorGUILayout.Space();
  469. GUILayout.EndVertical();
  470. }
  471. OnGUIUserInfo();
  472. }
  473. }