PDFViewerBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.Collections.Generic;
  10. using System.Reflection;
  11. #if UNITY_EDITOR
  12. using UnityEditor;
  13. #endif
  14. using UnityEngine;
  15. using UnityEngine.EventSystems;
  16. using UnityEngine.UI;
  17. namespace Paroxe.PdfRenderer.Internal.Viewer
  18. {
  19. [ExecuteInEditMode]
  20. public class PDFViewerBuilder : UIBehaviour
  21. {
  22. #if UNITY_EDITOR
  23. public static void BuildPDFViewerWithin(GameObject root, PDFViewer prefabViewer)
  24. {
  25. if (root.transform.childCount > 0)
  26. {
  27. int c = root.transform.childCount;
  28. for (int i = 0; i < c; ++i)
  29. {
  30. Destroy(root.transform.GetChild(0).gameObject);
  31. }
  32. }
  33. PDFViewer viewer = root.GetComponent<PDFViewer>();
  34. if (viewer == null)
  35. {
  36. viewer = root.AddComponent<PDFViewer>();
  37. }
  38. if (prefabViewer != null)
  39. {
  40. CopyViewer(prefabViewer, viewer);
  41. }
  42. RectTransform rootTransform = root.GetComponent<RectTransform>();
  43. if (rootTransform == null)
  44. {
  45. rootTransform = root.AddComponent<RectTransform>();
  46. rootTransform.anchorMin = Vector2.zero;
  47. rootTransform.anchorMax = Vector2.one;
  48. rootTransform.offsetMin = Vector2.zero;
  49. rootTransform.offsetMax = Vector2.zero;
  50. }
  51. GameObject internalPrefab = (GameObject) AssetDatabase.LoadAssetAtPath(
  52. AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("PDFViewer_Internal")[0]), typeof (GameObject));
  53. GameObject viewerInternal = (GameObject) Instantiate(internalPrefab);
  54. PrefabUtility.DisconnectPrefabInstance(viewerInternal);
  55. viewerInternal.GetComponent<PDFViewerInternal>().m_PDFViewer = root.GetComponent<PDFViewer>();
  56. viewer.m_Internal = viewerInternal.GetComponent<PDFViewerInternal>();
  57. viewerInternal.name = "_Internal";
  58. RectTransform internalRectTransform = viewerInternal.transform as RectTransform;
  59. internalRectTransform.SetParent(viewer.transform);
  60. internalRectTransform.anchorMin = Vector2.zero;
  61. internalRectTransform.anchorMax = Vector2.one;
  62. internalRectTransform.offsetMin = Vector2.zero;
  63. internalRectTransform.offsetMax = Vector2.zero;
  64. Selection.activeGameObject = root;
  65. }
  66. public static string[] GetNewFeatures(GameObject root)
  67. {
  68. float version = float.Parse(root.GetComponent<PDFViewer>().m_Internal.m_Version);
  69. List<string> newFeatures = new List<string>();
  70. if (version < 2.2f)
  71. {
  72. newFeatures.Add("Version 2.2 includes:");
  73. newFeatures.Add(" -Bookmarks");
  74. newFeatures.Add(" -Thumbnails");
  75. newFeatures.Add(" -Links");
  76. }
  77. if (version < 3.0f)
  78. {
  79. newFeatures.Add("Version 3.0 includes:");
  80. newFeatures.Add(" -Search Panel");
  81. }
  82. return newFeatures.ToArray();
  83. }
  84. public static void UpdateToLatestVersion(GameObject root)
  85. {
  86. float version = float.Parse(root.GetComponent<PDFViewer>().m_Internal.m_Version);
  87. if (version < 2.2f)
  88. UpdateToVersion_22(root);
  89. if (version < 3.0f)
  90. UpdateToVersion_30(root);
  91. }
  92. public static void UpdateToVersion_22(GameObject root)
  93. {
  94. Transform internalTransform = null;
  95. int c = root.transform.childCount;
  96. for (int i = 0; i < c; ++i)
  97. {
  98. var obj = root.transform.GetChild(i);
  99. if (obj.name == "_Internal")
  100. {
  101. internalTransform = obj;
  102. }
  103. }
  104. if (internalTransform != null)
  105. {
  106. GameObject leftPanelPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(
  107. AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("PDFViewer_Internal_LeftPanel")[0]), typeof(GameObject));
  108. GameObject leftPanelInternal = (GameObject) Instantiate(leftPanelPrefab);
  109. PrefabUtility.DisconnectPrefabInstance(leftPanelInternal);
  110. PDFViewer pdfViewer = root.GetComponent<PDFViewer>();
  111. internalTransform.GetComponent<PDFViewerInternal>().m_PDFViewer = pdfViewer;
  112. pdfViewer.m_Internal.m_LeftPanel = leftPanelInternal.GetComponent<PDFViewerLeftPanel>();
  113. leftPanelInternal.name = "_LeftPanel";
  114. RectTransform internalRectTransform = leftPanelInternal.transform as RectTransform;
  115. internalRectTransform.SetParent(internalTransform);
  116. internalRectTransform.SetSiblingIndex(pdfViewer.m_Internal.m_Overlay.transform.GetSiblingIndex());
  117. internalRectTransform.anchorMin = Vector2.zero;
  118. internalRectTransform.anchorMax = Vector2.up;
  119. internalRectTransform.offsetMin = new Vector2(0.0f, 0.0f);
  120. internalRectTransform.offsetMax = new Vector2(0.0f, 0.0f);
  121. internalRectTransform.pivot = Vector2.zero;
  122. internalRectTransform.sizeDelta = new Vector2(350.0f,
  123. -pdfViewer.m_Internal.m_TopPanel.sizeDelta.y + 1.0f);
  124. if (pdfViewer.m_Internal.m_PageSample.gameObject.GetComponent<PDFViewerPage>() == null)
  125. {
  126. PDFViewerPage pageSample =
  127. pdfViewer.m_Internal.m_PageSample.gameObject.AddComponent<PDFViewerPage>();
  128. string[] assetPaths = AssetDatabase.FindAssets("HandLinkCursor");
  129. string path = "";
  130. foreach (string asset in assetPaths)
  131. {
  132. if (AssetDatabase.GUIDToAssetPath(asset).Contains("PDFRenderer"))
  133. path = AssetDatabase.GUIDToAssetPath(asset);
  134. }
  135. pageSample.m_HandCursor =
  136. (Texture2D)
  137. AssetDatabase.LoadAssetAtPath(path, typeof (Texture2D));
  138. }
  139. pdfViewer.m_Internal.m_Viewport.GetComponent<ScrollRect>().inertia = true;
  140. pdfViewer.m_Internal.m_Viewport.GetComponent<ScrollRect>().decelerationRate = 0.025f;
  141. pdfViewer.m_Internal.m_Version = "2.2";
  142. }
  143. }
  144. public static void UpdateToVersion_30(GameObject root)
  145. {
  146. PDFViewer pdfViewer = root.GetComponent<PDFViewer>();
  147. Transform documentIcon = pdfViewer.m_Internal.m_TopPanel.Find("DocumentIcon");
  148. if (documentIcon != null)
  149. DestroyImmediate(documentIcon.gameObject);
  150. GameObject searchPanelPrefab =
  151. (GameObject)
  152. AssetDatabase.LoadAssetAtPath(
  153. AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("PDFViewer_SearchPanel")[0]), typeof (GameObject));
  154. GameObject searchPanel = (GameObject) Instantiate(searchPanelPrefab);
  155. PrefabUtility.DisconnectPrefabInstance(searchPanel);
  156. pdfViewer.m_Internal.m_SearchPanel = searchPanel.transform as RectTransform;
  157. pdfViewer.m_Internal.m_SearchPanel.SetParent(pdfViewer.m_Internal.transform);
  158. pdfViewer.m_Internal.m_SearchPanel.SetSiblingIndex(pdfViewer.m_Internal.m_Viewport.GetSiblingIndex() + 1);
  159. pdfViewer.m_Internal.m_SearchPanel.anchoredPosition = new Vector2(-234.0f, -59.0f);
  160. pdfViewer.m_Internal.m_SearchPanel.sizeDelta = new Vector2(400.0f, 150.0f);
  161. pdfViewer.m_Internal.m_SearchPanel.name = "_SearchPanel";
  162. GameObject topPanelSearchPrefab =
  163. (GameObject)
  164. AssetDatabase.LoadAssetAtPath(
  165. AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("PDFViewer_TopPanel_Search")[0]),
  166. typeof (GameObject));
  167. GameObject topPanelSearch = (GameObject) Instantiate(topPanelSearchPrefab);
  168. PrefabUtility.DisconnectPrefabInstance(topPanelSearch);
  169. topPanelSearch.transform.SetParent(pdfViewer.m_Internal.m_TopPanel);
  170. (topPanelSearch.transform as RectTransform).anchoredPosition = Vector2.zero;
  171. topPanelSearch.name = "_SearchButton";
  172. pdfViewer.m_Internal.m_PageCountLabel.resizeTextForBestFit = true;
  173. pdfViewer.m_Internal.m_PageCountLabel.resizeTextMinSize = 10;
  174. pdfViewer.m_Internal.m_PageCountLabel.resizeTextMaxSize = 22;
  175. pdfViewer.m_Internal.m_Version = "3.0";
  176. }
  177. private static void CopyViewer(PDFViewer prefabViewer, PDFViewer viewer)
  178. {
  179. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
  180. BindingFlags.Default | BindingFlags.DeclaredOnly;
  181. PropertyInfo[] pinfos = typeof (PDFViewer).GetProperties(flags);
  182. foreach (var pinfo in pinfos)
  183. {
  184. if (pinfo.CanWrite)
  185. {
  186. try
  187. {
  188. pinfo.SetValue(viewer, pinfo.GetValue(prefabViewer, null), null);
  189. }
  190. catch
  191. {
  192. }
  193. }
  194. }
  195. FieldInfo[] finfos = typeof (PDFViewer).GetFields(flags);
  196. foreach (var finfo in finfos)
  197. {
  198. finfo.SetValue(viewer, finfo.GetValue(prefabViewer));
  199. }
  200. }
  201. protected override void Start()
  202. {
  203. GameObject pdfViewer = new GameObject("PDFViewer");
  204. RectTransform rootTransform = pdfViewer.AddComponent<RectTransform>();
  205. rootTransform.SetParent(transform.parent);
  206. rootTransform.anchorMin = Vector2.zero;
  207. rootTransform.anchorMax = Vector2.one;
  208. rootTransform.offsetMin = Vector2.zero;
  209. rootTransform.offsetMax = Vector2.zero;
  210. BuildPDFViewerWithin(pdfViewer, GetComponent<PDFViewer>());
  211. UpdateToLatestVersion(pdfViewer);
  212. DestroyImmediate(gameObject);
  213. }
  214. #endif
  215. }
  216. }