CloudNinePaintingFrame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //================================================================================
  2. //
  3. //================================================================================
  4. using UnityEngine;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. //================================================================================
  8. //
  9. //================================================================================
  10. namespace ReaderRabbit
  11. {
  12. //================================================================================
  13. //
  14. //================================================================================
  15. public enum PaintingFrameColor
  16. {
  17. GREEN_PAINTING_FRAME,
  18. RED_PAINTING_FRAME,
  19. PINK_PAINTING_FRAME,
  20. BLUE_PAINTING_FRAME,
  21. CYAN_PAINTING_FRAME
  22. }
  23. //================================================================================
  24. //
  25. //================================================================================
  26. [RequireComponent(typeof(PolygonCollider2D))]
  27. public class CloudNinePaintingFrame : MonoBehaviour
  28. {
  29. //================================================================================
  30. //
  31. //================================================================================
  32. [SerializeField] private PaintingFrameColor m_Color;
  33. [SerializeField] private GameObject m_MouseOverSprite;
  34. [SerializeField] private GameObject m_MouseOutsideSprite;
  35. [SerializeField] private List<SpriteRenderer> m_PaintingFrames;
  36. [SerializeField] private float m_TimePerFrame = 0.16f;
  37. [SerializeField] private bool m_IsSidePainting;
  38. private int m_TotalNumberOfFrames = 0;
  39. private int m_CurrentFrame;
  40. private SceneCloudNineArtGallery m_Scene;
  41. //================================================================================
  42. //
  43. //================================================================================
  44. void Start()
  45. {
  46. m_Scene = GameObject.Find ("SceneCode").GetComponent<SceneCloudNineArtGallery>();
  47. m_MouseOverSprite.SetActive(false);
  48. m_MouseOutsideSprite.SetActive(true);
  49. m_CurrentFrame = 0;
  50. // HACK: Delay RRButton call to collider and elements sizes and positions update in order to avoid it
  51. // being executed before ScreenResolutionManager has started itself
  52. Invoke ("UpdateSizesAndPositions", 0.01f);
  53. }
  54. //================================================================================
  55. //
  56. //================================================================================
  57. void UpdateSizesAndPositions()
  58. {
  59. KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(m_MouseOverSprite);
  60. if (!m_IsSidePainting)
  61. {
  62. foreach (SpriteRenderer sr in m_PaintingFrames)
  63. {
  64. KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(sr.gameObject);
  65. }
  66. CreateOrLoadFrames();
  67. }
  68. }
  69. //================================================================================
  70. //
  71. //================================================================================
  72. void OnMouseEnter()
  73. {
  74. m_MouseOverSprite.SetActive(true);
  75. m_MouseOutsideSprite.SetActive(false);
  76. }
  77. //================================================================================
  78. //
  79. //================================================================================
  80. void OnMouseExit()
  81. {
  82. if (!Playing)
  83. {
  84. m_MouseOverSprite.SetActive(false);
  85. m_MouseOutsideSprite.SetActive(true);
  86. }
  87. }
  88. //================================================================================
  89. //
  90. //================================================================================
  91. void OnMouseUpAsButton()
  92. {
  93. m_MouseOverSprite.SetActive(true);
  94. m_MouseOutsideSprite.SetActive(false);
  95. }
  96. //================================================================================
  97. //
  98. //================================================================================
  99. public void CreateOrLoadFrames()
  100. {
  101. if (!LoadFrames())
  102. {
  103. CreateFrames ();
  104. }
  105. this.m_PaintingFrames[0].gameObject.SetActive(true);
  106. }
  107. //================================================================================
  108. //
  109. //================================================================================
  110. private bool LoadFrames()
  111. {
  112. int frameNumber = 0;
  113. Rect textureRect = new Rect(0, 0, 0, 0);
  114. string basePath = "";
  115. switch(m_Color)
  116. {
  117. case PaintingFrameColor.GREEN_PAINTING_FRAME:
  118. basePath = "Painting01/";
  119. break;
  120. case PaintingFrameColor.RED_PAINTING_FRAME:
  121. basePath = "Painting02/";
  122. break;
  123. case PaintingFrameColor.PINK_PAINTING_FRAME:
  124. basePath = "Painting03/";
  125. break;
  126. case PaintingFrameColor.BLUE_PAINTING_FRAME:
  127. basePath = "Painting04/";
  128. break;
  129. case PaintingFrameColor.CYAN_PAINTING_FRAME:
  130. basePath = "Painting05/";
  131. break;
  132. }
  133. foreach (SpriteRenderer sr in m_PaintingFrames)
  134. {
  135. string path = basePath + "frame" + string.Format("{0:00}", frameNumber++);
  136. Texture2D texture = PlayerData.Instance().LoadImage(path, PaintingFrame.ReferenceTextureDimensions);
  137. if (texture == null && frameNumber <= 1)
  138. {
  139. return false;
  140. }
  141. else if (texture == null)
  142. {
  143. return true;
  144. }
  145. TextureScale.Bilinear(texture, (int)sr.sprite.rect.width, (int)sr.sprite.rect.height);
  146. textureRect.width = texture.width;
  147. textureRect.height = texture.height;
  148. sr.sprite = Sprite.Create(texture, textureRect, new Vector2(0, 1), 1);
  149. if (!IsFrameBlank(sr))
  150. {
  151. m_TotalNumberOfFrames = frameNumber;
  152. }
  153. }
  154. return true;
  155. }
  156. //================================================================================
  157. //
  158. //================================================================================
  159. private void CreateFrames()
  160. {
  161. int frameNumber = 0;
  162. Rect textureRect = new Rect(0, 0, 0, 0);
  163. string basePath = "";
  164. switch(m_Color)
  165. {
  166. case PaintingFrameColor.GREEN_PAINTING_FRAME:
  167. basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting01/";
  168. break;
  169. case PaintingFrameColor.RED_PAINTING_FRAME:
  170. basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting02/";
  171. break;
  172. case PaintingFrameColor.PINK_PAINTING_FRAME:
  173. basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting03/";
  174. break;
  175. default:
  176. return;
  177. }
  178. foreach (SpriteRenderer sr in m_PaintingFrames)
  179. {
  180. string path = basePath + "frame" + string.Format("{0:00}", frameNumber++);
  181. Texture2D texture = Resources.Load<Texture2D>(path);
  182. if (texture == null)
  183. {
  184. break;
  185. }
  186. TextureScale.Bilinear(texture, (int)sr.sprite.rect.width, (int)sr.sprite.rect.height);
  187. textureRect.width = texture.width;
  188. textureRect.height = texture.height;
  189. sr.sprite = Sprite.Create(texture, textureRect, new Vector2(0, 1), 1);
  190. sr.gameObject.SetActive(true);
  191. sr.gameObject.SetActive(false);
  192. if (!IsFrameBlank(sr))
  193. {
  194. m_TotalNumberOfFrames = frameNumber;
  195. }
  196. }
  197. // m_TotalNumberOfFrames = frameNumber - 1;
  198. m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
  199. }
  200. //================================================================================
  201. //
  202. //================================================================================
  203. public void Play()
  204. {
  205. Invoke ("NextFrame", m_TimePerFrame);
  206. Playing = true;
  207. OnMouseEnter();
  208. }
  209. //================================================================================
  210. //
  211. //================================================================================
  212. private void NextFrame()
  213. {
  214. m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(false);
  215. m_CurrentFrame++;
  216. if (m_CurrentFrame >= m_TotalNumberOfFrames)
  217. {
  218. m_CurrentFrame = 0;
  219. m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
  220. m_Scene.PlayNextFrame();
  221. Playing = false;
  222. OnMouseExit();
  223. return;
  224. }
  225. m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
  226. Invoke ("NextFrame", m_TimePerFrame);
  227. }
  228. //================================================================================
  229. //
  230. //================================================================================
  231. public void EnableClick()
  232. {
  233. GetComponent<Collider2D>().enabled = true;
  234. }
  235. //================================================================================
  236. //
  237. //================================================================================
  238. public void DisableClick()
  239. {
  240. GetComponent<Collider2D>().enabled = false;
  241. }
  242. //================================================================================
  243. //
  244. //================================================================================
  245. private string GetFramePath(int frameNumber)
  246. {
  247. string path = "";
  248. switch(m_Color)
  249. {
  250. case PaintingFrameColor.GREEN_PAINTING_FRAME:
  251. path+="Painting01/";
  252. break;
  253. case PaintingFrameColor.RED_PAINTING_FRAME:
  254. path+="Painting02/";
  255. break;
  256. case PaintingFrameColor.PINK_PAINTING_FRAME:
  257. path+="Painting03/";
  258. break;
  259. case PaintingFrameColor.BLUE_PAINTING_FRAME:
  260. path+="Painting04/";
  261. break;
  262. case PaintingFrameColor.CYAN_PAINTING_FRAME:
  263. path+="Painting05/";
  264. break;
  265. }
  266. path += "frame" + string.Format("{0:00}", frameNumber);
  267. return path;
  268. }
  269. public bool IsFrameBlank(SpriteRenderer frame)
  270. {
  271. if (frame.sprite.texture == null)
  272. {
  273. return true;
  274. }
  275. Color[] colors = frame.sprite.texture.GetPixels ();
  276. foreach (Color c in colors)
  277. {
  278. if (c != Color.white)
  279. {
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. //================================================================================
  286. //
  287. //================================================================================
  288. public bool Playing
  289. {
  290. get;
  291. private set;
  292. }
  293. //================================================================================
  294. //
  295. //================================================================================
  296. public int TotalNumberOfFrames
  297. {
  298. get { return m_TotalNumberOfFrames; }
  299. }
  300. } // public class CloudNinePaintingFrame : MonoBehaviour
  301. } // namespace ReaderRabbit