MegaBook.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. [ExecuteInEditMode]
  5. public class MegaBook : MonoBehaviour
  6. {
  7. public float dragsensi = 1.0f;
  8. public float keysensi = 1.0f;
  9. public GameObject front;
  10. public GameObject back;
  11. public GameObject page1;
  12. public GameObject page2;
  13. public GameObject page3;
  14. public List<Texture> pages = new List<Texture>();
  15. public float bookalpha;
  16. public float covergap = 0.0f;
  17. public float pagespace = 0.01f;
  18. public bool interactive = false;
  19. public bool useMouse = false;
  20. public KeyCode prevPageKey = KeyCode.A;
  21. public KeyCode nextPageKey = KeyCode.D;
  22. MegaPageFlip pf1;
  23. MegaPageFlip pf2;
  24. MegaPageFlip pf3;
  25. MeshRenderer mrpg1;
  26. MeshRenderer mrpg2;
  27. MeshRenderer mrpg3;
  28. int currentPage = 0;
  29. bool pageTurning = false;
  30. MegaModifyObject pobj1;
  31. MegaModifyObject pobj2;
  32. MegaModifyObject pobj3;
  33. float page1turn = -1.0f;
  34. float page2turn = -1.0f;
  35. float page3turn = -1.0f;
  36. void SetPageTexture(MeshRenderer mr, int i, Texture t)
  37. {
  38. if ( mr.sharedMaterials[i].mainTexture != t )
  39. mr.sharedMaterials[i].mainTexture = t;
  40. }
  41. void Update()
  42. {
  43. if ( interactive )
  44. Interactive();
  45. if ( page1 == null || page2 == null || page3 == null )
  46. return;
  47. if ( page1 != null && pf1 == null )
  48. pf1 = page1.GetComponent<MegaPageFlip>();
  49. if ( mrpg1 == null )
  50. mrpg1 = page1.GetComponent<MeshRenderer>();
  51. if ( page2 != null && pf2 == null )
  52. pf2 = page2.GetComponent<MegaPageFlip>();
  53. if ( mrpg2 == null )
  54. mrpg2 = page2.GetComponent<MeshRenderer>();
  55. if ( page3 != null && pf3 == null )
  56. pf3 = page3.GetComponent<MegaPageFlip>();
  57. if ( mrpg3 == null )
  58. mrpg3 = page3.GetComponent<MeshRenderer>();
  59. if ( pf1 == null || pf2 == null || pf3 == null || front == null || back == null )
  60. return;
  61. if ( pobj1 == null )
  62. pobj1 = page1.GetComponent<MegaModifyObject>();
  63. if ( pobj2 == null )
  64. pobj2 = page2.GetComponent<MegaModifyObject>();
  65. if ( pobj3 == null )
  66. pobj3 = page3.GetComponent<MegaModifyObject>();
  67. int pagecount = (pages.Count / 2) + 2;
  68. if ( bookalpha < 0.0f )
  69. bookalpha = 0.0f;
  70. if ( bookalpha > 100.0f )
  71. bookalpha = 100.0f;
  72. if ( front.transform.childCount > 0 )
  73. {
  74. Transform child = front.transform.GetChild(0);
  75. if ( child != null )
  76. {
  77. Vector3 off = Vector3.zero;
  78. off.y = covergap * 0.5f;
  79. child.localPosition = off;
  80. }
  81. }
  82. if ( back.transform.childCount > 0 )
  83. {
  84. Transform child = back.transform.GetChild(0);
  85. if ( child != null )
  86. {
  87. Vector3 off = Vector3.zero;
  88. off.y = -covergap * 0.5f;
  89. child.localPosition = off;
  90. }
  91. }
  92. double alpha = (double)bookalpha / 100.0;
  93. int page = (int)((double)pagecount * (double)alpha);
  94. double step = 1.0 / (double)pagecount;
  95. double turn = (alpha % step) / step;
  96. Vector3 ang = Vector3.zero;
  97. // Front cover
  98. if ( page == 0 )
  99. ang.z = 180.0f * (float)turn;
  100. else
  101. ang.z = 180.0f;
  102. front.transform.localRotation = Quaternion.Euler(ang);
  103. // Back cover
  104. if ( page >= pagecount - 1 )
  105. ang.z = 180.0f * (float)turn;
  106. else
  107. ang.z = 0.0f;
  108. back.transform.localRotation = Quaternion.Euler(ang);
  109. if ( pagecount < 3 )
  110. return;
  111. // Set PageFlip values
  112. if ( page == 1 )
  113. {
  114. pf1.turn = Mathf.Clamp((float)(turn * 100.0), 0.0f, 100.0f);
  115. pf2.turn = 0.0f;
  116. pf3.turn = 0.0f;
  117. }
  118. else
  119. {
  120. if ( page == pagecount - 2 )
  121. {
  122. pf1.turn = 100.0f;
  123. pf2.turn = 100.0f;
  124. pf3.turn = Mathf.Clamp((float)(turn * 100.0), 0.0f, 100.0f); //(float)turn * 100.0f;
  125. }
  126. else
  127. {
  128. if ( page == 0 )
  129. {
  130. pf1.turn = 0.0f;
  131. pf2.turn = 0.0f;
  132. pf3.turn = 0.0f;
  133. }
  134. else
  135. {
  136. if ( page >= pagecount - 1 )
  137. {
  138. pf1.turn = 100.0f;
  139. pf2.turn = 100.0f;
  140. pf3.turn = 100.0f;
  141. }
  142. else
  143. {
  144. pf1.turn = 100.0f;
  145. pf2.turn = Mathf.Clamp((float)(turn * 100.0), 0.0f, 100.0f); //(float)turn * 100.0f;
  146. pf3.turn = 0.0f;
  147. }
  148. }
  149. }
  150. }
  151. // Page offsets
  152. Vector3 poff = Vector3.zero;
  153. //float po = pagespace; // * 2.0f;
  154. poff.y = Mathf.Lerp(pagespace, -pagespace, pf1.turn * 0.01f);
  155. page1.transform.localPosition = poff;
  156. poff.y = Mathf.Lerp(0.0f, 0.0f, pf2.turn * 0.01f);
  157. page2.transform.localPosition = poff;
  158. poff.y = Mathf.Lerp(-pagespace, pagespace, pf3.turn * 0.01f);
  159. page3.transform.localPosition = poff;
  160. // Page textures
  161. int pg = page - 2;
  162. if ( pg < 0 )
  163. pg = 0;
  164. pg *= 2;
  165. if ( pg < pages.Count - 1 )
  166. {
  167. SetPageTexture(mrpg1, 0, pages[pg]);
  168. SetPageTexture(mrpg1, 1, pages[pg + 1]);
  169. }
  170. if ( pg < pages.Count - 3 )
  171. {
  172. if ( pg < pages.Count - 5 )
  173. {
  174. SetPageTexture(mrpg2, 0, pages[pg + 2]);
  175. SetPageTexture(mrpg2, 1, pages[pg + 3]);
  176. }
  177. }
  178. if ( pg < pages.Count - 5 )
  179. {
  180. SetPageTexture(mrpg3, 0, pages[pg + 4]);
  181. SetPageTexture(mrpg3, 1, pages[pg + 5]);
  182. }
  183. if ( pf1.turn != page1turn )
  184. {
  185. page1turn = pf1.turn;
  186. pobj1.Enabled = true;
  187. }
  188. else
  189. {
  190. if ( page1turn == 100.0f || page1turn == 0.0f )
  191. pobj1.Enabled = false;
  192. else
  193. pobj1.Enabled = true;
  194. }
  195. if ( pf2.turn != page2turn )
  196. {
  197. page2turn = pf2.turn;
  198. pobj2.Enabled = true;
  199. }
  200. else
  201. {
  202. if ( page2turn == 100.0f || page2turn == 0.0f )
  203. pobj2.Enabled = false;
  204. else
  205. pobj2.Enabled = true;
  206. }
  207. if ( pf3.turn != page3turn )
  208. {
  209. page3turn = pf3.turn;
  210. pobj3.Enabled = true;
  211. }
  212. else
  213. {
  214. if ( page3turn == 100.0f || page3turn == 0.0f )
  215. pobj3.Enabled = false;
  216. else
  217. pobj3.Enabled = true;
  218. }
  219. }
  220. void Interactive()
  221. {
  222. if ( !pageTurning )
  223. {
  224. // Mouse version
  225. if ( useMouse )
  226. {
  227. bookalpha += Input.GetAxis("Mouse X") * dragsensi;
  228. bookalpha = Mathf.Clamp(bookalpha, 0.0f, 100.0f);
  229. }
  230. else
  231. {
  232. // Keyboard version
  233. if ( Input.GetKeyDown(prevPageKey) )
  234. StartCoroutine(FlipToPage(currentPage - 1));
  235. if ( Input.GetKeyDown(nextPageKey) )
  236. StartCoroutine(FlipToPage(currentPage + 1));
  237. }
  238. }
  239. }
  240. IEnumerator FlipToPage(int page)
  241. {
  242. pageTurning = true;
  243. double totalPageCount = (double)((pages.Count + 4) / 2);
  244. double target = (double)page / totalPageCount * 99.999;
  245. if ( target < 0.0 )
  246. target = 0.0;
  247. if ( target > 100.0 )
  248. target = 100.0;
  249. while ( bookalpha != (float)target )
  250. {
  251. bookalpha = Mathf.MoveTowards(bookalpha, (float)target, keysensi * Time.deltaTime);
  252. yield return 0;
  253. }
  254. currentPage = page;
  255. pageTurning = false;
  256. }
  257. }