PDFImporterContextMenu.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Paroxe.PdfRenderer.Internal
  5. {
  6. public class PDFImporterContextMenu
  7. {
  8. static string extension = ".pdf";
  9. static string newExtension = ".asset";
  10. [MenuItem("Assets/PDF Renderer/Convert to .asset")]
  11. public static void ConvertPDFToAsset()
  12. {
  13. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  14. string newPath = ConvertToInternalPath(path);
  15. PDFAsset numSeq = AssetDatabase.LoadAssetAtPath(newPath, typeof(PDFAsset)) as PDFAsset;
  16. bool loaded = (numSeq != null);
  17. if (!loaded)
  18. {
  19. numSeq = ScriptableObject.CreateInstance<PDFAsset>();
  20. }
  21. numSeq.Load(File.ReadAllBytes(path));
  22. if (!loaded)
  23. {
  24. AssetDatabase.CreateAsset(numSeq, newPath);
  25. }
  26. AssetDatabase.SaveAssets();
  27. }
  28. public static string ConvertToInternalPath(string asset)
  29. {
  30. string left = asset.Substring(0, asset.Length - extension.Length);
  31. return left + newExtension;
  32. }
  33. public static bool HasExtension(string asset)
  34. {
  35. return asset.EndsWith(extension, System.StringComparison.OrdinalIgnoreCase);
  36. }
  37. [MenuItem("Assets/PDF Renderer/Convert to .asset", true)]
  38. static bool ValidateConvertPDFToAsset()
  39. {
  40. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  41. return HasExtension(path);
  42. }
  43. }
  44. }