DemoMenu.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace Imagine.WebAR.Demo.Editor
  7. {
  8. public class DemoMenu
  9. {
  10. [MenuItem("Assets/Imagine WebAR/Add Demo Imagetargets", false, 1301)]
  11. public static void AddDemoImageTargets()
  12. {
  13. var settings = ImageTrackerGlobalSettings.Instance;
  14. foreach(var info in GetDemoTargets())
  15. {
  16. var index = settings.imageTargetInfos.FindIndex(i => i.id == info.id);
  17. if(index < 0)
  18. {
  19. settings.imageTargetInfos.Add(info);
  20. }
  21. }
  22. EditorUtility.SetDirty(ImageTrackerGlobalSettings.Instance);
  23. Selection.activeObject = ImageTrackerGlobalSettings.Instance;
  24. }
  25. [MenuItem("Assets/Imagine WebAR/Remove Demo Imagetargets", false, 1302)]
  26. public static void RemoveDemoImageTargets()
  27. {
  28. var settings = ImageTrackerGlobalSettings.Instance;
  29. foreach (var info in GetDemoTargets())
  30. {
  31. var index = settings.imageTargetInfos.FindIndex(i => i.id == info.id);
  32. if (index >= 0)
  33. {
  34. settings.imageTargetInfos.RemoveAt(index);
  35. }
  36. }
  37. EditorUtility.SetDirty(ImageTrackerGlobalSettings.Instance);
  38. Selection.activeObject = ImageTrackerGlobalSettings.Instance;
  39. }
  40. static List<ImageTargetInfo> GetDemoTargets()
  41. {
  42. var infos = new List<ImageTargetInfo>();
  43. string[] files = Directory.GetFiles(Application.dataPath + "/Imagine/ImageTracker/Demos/Imagetargets", "*.prefab", SearchOption.TopDirectoryOnly);
  44. foreach(var file in files) {
  45. var id = Path.GetFileNameWithoutExtension(file);
  46. var path = file.Replace(Application.dataPath, "Assets");
  47. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  48. var tex = (Texture2D)prefab.GetComponent<Renderer>().sharedMaterial.mainTexture;
  49. infos.Add(new ImageTargetInfo() {
  50. id = id,
  51. texture = tex
  52. });
  53. }
  54. return infos;
  55. }
  56. }
  57. }