PostProcessBuild.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditor.Callbacks;
  8. namespace Imagine.WebAR.Editor
  9. {
  10. public class PostProcessBuild : MonoBehaviour
  11. {
  12. [PostProcessBuild]
  13. public static void OnPostProcessBuild(BuildTarget target, string buildPath)
  14. {
  15. Debug.Log(buildPath);
  16. var targetsHtml = "";
  17. if(!Directory.Exists(buildPath + "/targets"))
  18. {
  19. Directory.CreateDirectory(buildPath + "/targets");
  20. }
  21. foreach (var info in ImageTrackerGlobalSettings.Instance.imageTargetInfos)
  22. {
  23. var src = AssetDatabase.GetAssetPath(info.texture);
  24. var fileName = Path.GetFileName(src);
  25. Debug.Log(info.id + "->" + src);
  26. File.Copy(src, buildPath + "/targets/" + fileName, true);
  27. targetsHtml += ("\t\t<imagetarget id='" + info.id + "' src='targets/" + fileName + "'></imagetarget>\n");
  28. }
  29. Debug.Log(targetsHtml);
  30. var lines = File.ReadAllLines(buildPath + "/index.html").ToList();
  31. var html = "";
  32. foreach(var line in lines)
  33. {
  34. if (string.IsNullOrEmpty(line))
  35. continue;
  36. var trimmed = line.Trim();
  37. if (trimmed.StartsWith("<imagetarget") && trimmed.EndsWith("</imagetarget>"))
  38. continue;
  39. html += line + "\n";
  40. }
  41. html = html.Replace("<!--IMAGETARGETS-->", "<!--IMAGETARGETS-->\n" + targetsHtml);
  42. File.WriteAllText(buildPath + "/index.html", html);
  43. }
  44. }
  45. }