PDFBookmarksViewer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.EventSystems;
  13. using UnityEngine.UI;
  14. namespace Paroxe.PdfRenderer.Internal.Viewer
  15. {
  16. public class PDFBookmarksViewer : UIBehaviour
  17. {
  18. public RectTransform m_BooksmarksContainer;
  19. public PDFBookmarkListItem m_ItemPrefab;
  20. public Image m_LastHighlightedImage;
  21. #if !UNITY_WEBGL
  22. private CanvasGroup m_CanvasGroup;
  23. private bool m_Initialized = false;
  24. private RectTransform m_LeftPanel;
  25. private bool m_Loaded = false;
  26. private PDFDocument m_Document;
  27. private PDFViewer m_PDFViewer;
  28. private RectTransform m_RectTransform;
  29. private List<RectTransform> m_TopLevelItems;
  30. #endif
  31. public void DoUpdate()
  32. {
  33. #if !UNITY_WEBGL
  34. if (m_Initialized)
  35. {
  36. float containerHeight = 0.0f;
  37. foreach (RectTransform child in m_TopLevelItems)
  38. {
  39. containerHeight += child.sizeDelta.y;
  40. }
  41. }
  42. if (m_RectTransform != null && m_LeftPanel != null &&
  43. m_RectTransform.sizeDelta.x != m_LeftPanel.sizeDelta.x - 24.0f)
  44. {
  45. m_RectTransform.sizeDelta = new Vector2(m_LeftPanel.sizeDelta.x - 24.0f, m_RectTransform.sizeDelta.y);
  46. }
  47. #endif
  48. }
  49. private void Cleanup()
  50. {
  51. #if !UNITY_WEBGL
  52. if (m_Loaded)
  53. {
  54. m_Loaded = false;
  55. m_Initialized = false;
  56. m_TopLevelItems = null;
  57. m_Document = null;
  58. int childCount = m_BooksmarksContainer.transform.childCount;
  59. for (int i = 1; i < childCount; ++i)
  60. {
  61. Destroy(m_BooksmarksContainer.transform.GetChild(i).gameObject);
  62. }
  63. m_ItemPrefab.gameObject.SetActive(false);
  64. m_CanvasGroup.alpha = 0.0f;
  65. }
  66. #endif
  67. }
  68. public void OnDocumentLoaded(PDFDocument document)
  69. {
  70. #if !UNITY_WEBGL
  71. if (!m_Loaded && gameObject.activeInHierarchy)
  72. {
  73. m_Loaded = true;
  74. m_Document = document;
  75. m_TopLevelItems = new List<RectTransform>();
  76. m_RectTransform = transform as RectTransform;
  77. m_LeftPanel = transform.parent as RectTransform;
  78. PDFViewer viewer = GetComponentInParent<PDFViewer>();
  79. PDFBookmark rootBookmark = m_Document.GetRootBookmark(viewer);
  80. if (rootBookmark != null)
  81. {
  82. m_ItemPrefab.gameObject.SetActive(true);
  83. foreach (PDFBookmark child in rootBookmark.EnumerateChildrenBookmarks())
  84. {
  85. PDFBookmarkListItem item = null;
  86. item = ((GameObject)Instantiate(m_ItemPrefab.gameObject)).GetComponent<PDFBookmarkListItem>();
  87. RectTransform itemTransform = item.transform as RectTransform;
  88. itemTransform.SetParent(m_BooksmarksContainer);
  89. itemTransform.localScale = Vector3.one;
  90. itemTransform.anchorMin = new Vector2(0.0f, 1.0f);
  91. itemTransform.anchorMax = new Vector2(0.0f, 1.0f);
  92. itemTransform.offsetMin = Vector2.zero;
  93. itemTransform.offsetMax = Vector2.zero;
  94. m_TopLevelItems.Add(item.transform as RectTransform);
  95. item.Initilize(child, 0, false);
  96. }
  97. m_ItemPrefab.gameObject.SetActive(false);
  98. m_Initialized = true;
  99. if (GetComponentInParent<PDFViewerLeftPanel>().m_Thumbnails.gameObject.GetComponent<CanvasGroup>().alpha == 0.0f)
  100. StartCoroutine(DelayedShow());
  101. }
  102. }
  103. #endif
  104. }
  105. #if !UNITY_WEBGL
  106. IEnumerator DelayedShow()
  107. {
  108. yield return null;
  109. yield return null;
  110. yield return null;
  111. m_CanvasGroup.alpha = 1.0f;
  112. }
  113. #endif
  114. public void OnDocumentUnloaded()
  115. {
  116. #if !UNITY_WEBGL
  117. Cleanup();
  118. #endif
  119. }
  120. #if !UNITY_WEBGL
  121. protected override void OnDisable()
  122. {
  123. base.OnDisable();
  124. if (m_Loaded)
  125. {
  126. Cleanup();
  127. }
  128. }
  129. #endif
  130. #if !UNITY_WEBGL
  131. protected override void OnEnable()
  132. {
  133. base.OnEnable();
  134. DoOnEnable();
  135. }
  136. #endif
  137. public void DoOnEnable()
  138. {
  139. #if !UNITY_WEBGL
  140. if (m_PDFViewer == null)
  141. m_PDFViewer = GetComponentInParent<PDFViewer>();
  142. if (m_CanvasGroup == null)
  143. m_CanvasGroup = GetComponent<CanvasGroup>();
  144. if (m_RectTransform == null)
  145. m_RectTransform = transform as RectTransform;
  146. m_ItemPrefab.gameObject.SetActive(false);
  147. m_CanvasGroup.alpha = 0.0f;
  148. if (!m_Loaded && m_PDFViewer.Document != null && m_PDFViewer.Document.IsValid)
  149. OnDocumentLoaded(m_PDFViewer.Document);
  150. #endif
  151. }
  152. }
  153. }