PDFImporterContextMenu.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using System.IO;
  10. using UnityEditor;
  11. using UnityEngine;
  12. namespace Paroxe.PdfRenderer.Internal
  13. {
  14. public class PDFImporterContextMenu
  15. {
  16. static string extension = ".pdf";
  17. static string newExtension = ".asset";
  18. [MenuItem("Assets/PDF Renderer/Convert to .asset")]
  19. public static void ConvertPDFToAsset()
  20. {
  21. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  22. string newPath = ConvertToInternalPath(path);
  23. PDFAsset numSeq = AssetDatabase.LoadAssetAtPath(newPath, typeof (PDFAsset)) as PDFAsset;
  24. bool loaded = (numSeq != null);
  25. if (!loaded)
  26. {
  27. numSeq = ScriptableObject.CreateInstance<PDFAsset>();
  28. }
  29. numSeq.Load(File.ReadAllBytes(path));
  30. if (!loaded)
  31. {
  32. AssetDatabase.CreateAsset(numSeq, newPath);
  33. }
  34. AssetDatabase.SaveAssets();
  35. }
  36. public static string ConvertToInternalPath(string asset)
  37. {
  38. string left = asset.Substring(0, asset.Length - extension.Length);
  39. return left + newExtension;
  40. }
  41. public static bool HasExtension(string asset)
  42. {
  43. return asset.EndsWith(extension, System.StringComparison.OrdinalIgnoreCase);
  44. }
  45. [MenuItem("Assets/PDF Renderer/Convert to .asset", true)]
  46. static bool ValidateConvertPDFToAsset()
  47. {
  48. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  49. return HasExtension(path);
  50. }
  51. }
  52. }