123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- //================================================================================
- //
- //================================================================================
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- //================================================================================
- //
- //================================================================================
- namespace ReaderRabbit
- {
- //================================================================================
- //
- //================================================================================
- public enum PaintingFrameColor
- {
- GREEN_PAINTING_FRAME,
- RED_PAINTING_FRAME,
- PINK_PAINTING_FRAME,
- BLUE_PAINTING_FRAME,
- CYAN_PAINTING_FRAME
- }
- //================================================================================
- //
- //================================================================================
- [RequireComponent(typeof(PolygonCollider2D))]
- public class CloudNinePaintingFrame : MonoBehaviour
- {
- //================================================================================
- //
- //================================================================================
- [SerializeField] private PaintingFrameColor m_Color;
- [SerializeField] private GameObject m_MouseOverSprite;
- [SerializeField] private GameObject m_MouseOutsideSprite;
-
- [SerializeField] private List<SpriteRenderer> m_PaintingFrames;
- [SerializeField] private float m_TimePerFrame = 0.16f;
-
- [SerializeField] private bool m_IsSidePainting;
-
- private int m_TotalNumberOfFrames = 0;
- private int m_CurrentFrame;
- private SceneCloudNineArtGallery m_Scene;
- //================================================================================
- //
- //================================================================================
- void Start()
- {
- m_Scene = GameObject.Find ("SceneCode").GetComponent<SceneCloudNineArtGallery>();
- m_MouseOverSprite.SetActive(false);
- m_MouseOutsideSprite.SetActive(true);
- m_CurrentFrame = 0;
-
- // HACK: Delay RRButton call to collider and elements sizes and positions update in order to avoid it
- // being executed before ScreenResolutionManager has started itself
- Invoke ("UpdateSizesAndPositions", 0.01f);
- }
- //================================================================================
- //
- //================================================================================
- void UpdateSizesAndPositions()
- {
- KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(m_MouseOverSprite);
-
- if (!m_IsSidePainting)
- {
- foreach (SpriteRenderer sr in m_PaintingFrames)
- {
- KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(sr.gameObject);
- }
- CreateOrLoadFrames();
- }
- }
- //================================================================================
- //
- //================================================================================
- void OnMouseEnter()
- {
- m_MouseOverSprite.SetActive(true);
- m_MouseOutsideSprite.SetActive(false);
- }
- //================================================================================
- //
- //================================================================================
- void OnMouseExit()
- {
- if (!Playing)
- {
- m_MouseOverSprite.SetActive(false);
- m_MouseOutsideSprite.SetActive(true);
- }
- }
- //================================================================================
- //
- //================================================================================
- void OnMouseUpAsButton()
- {
- m_MouseOverSprite.SetActive(true);
- m_MouseOutsideSprite.SetActive(false);
- }
- //================================================================================
- //
- //================================================================================
- public void CreateOrLoadFrames()
- {
- if (!LoadFrames())
- {
- CreateFrames ();
- }
-
- this.m_PaintingFrames[0].gameObject.SetActive(true);
- }
- //================================================================================
- //
- //================================================================================
- private bool LoadFrames()
- {
- int frameNumber = 0;
- Rect textureRect = new Rect(0, 0, 0, 0);
- string basePath = "";
-
- switch(m_Color)
- {
- case PaintingFrameColor.GREEN_PAINTING_FRAME:
- basePath = "Painting01/";
- break;
- case PaintingFrameColor.RED_PAINTING_FRAME:
- basePath = "Painting02/";
- break;
- case PaintingFrameColor.PINK_PAINTING_FRAME:
- basePath = "Painting03/";
- break;
- case PaintingFrameColor.BLUE_PAINTING_FRAME:
- basePath = "Painting04/";
- break;
- case PaintingFrameColor.CYAN_PAINTING_FRAME:
- basePath = "Painting05/";
- break;
- }
-
- foreach (SpriteRenderer sr in m_PaintingFrames)
- {
- string path = basePath + "frame" + string.Format("{0:00}", frameNumber++);
- Texture2D texture = PlayerData.Instance().LoadImage(path, PaintingFrame.ReferenceTextureDimensions);
-
- if (texture == null && frameNumber <= 1)
- {
- return false;
- }
- else if (texture == null)
- {
- return true;
- }
-
- TextureScale.Bilinear(texture, (int)sr.sprite.rect.width, (int)sr.sprite.rect.height);
- textureRect.width = texture.width;
- textureRect.height = texture.height;
-
- sr.sprite = Sprite.Create(texture, textureRect, new Vector2(0, 1), 1);
-
- if (!IsFrameBlank(sr))
- {
- m_TotalNumberOfFrames = frameNumber;
- }
- }
-
- return true;
- }
- //================================================================================
- //
- //================================================================================
- private void CreateFrames()
- {
- int frameNumber = 0;
- Rect textureRect = new Rect(0, 0, 0, 0);
-
- string basePath = "";
-
- switch(m_Color)
- {
- case PaintingFrameColor.GREEN_PAINTING_FRAME:
- basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting01/";
- break;
- case PaintingFrameColor.RED_PAINTING_FRAME:
- basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting02/";
- break;
- case PaintingFrameColor.PINK_PAINTING_FRAME:
- basePath = "Resources_EN/AmazingArtGallery/Paintings/Painting03/";
- break;
- default:
- return;
- }
-
- foreach (SpriteRenderer sr in m_PaintingFrames)
- {
- string path = basePath + "frame" + string.Format("{0:00}", frameNumber++);
- Texture2D texture = Resources.Load<Texture2D>(path);
-
- if (texture == null)
- {
- break;
- }
-
- TextureScale.Bilinear(texture, (int)sr.sprite.rect.width, (int)sr.sprite.rect.height);
- textureRect.width = texture.width;
- textureRect.height = texture.height;
-
- sr.sprite = Sprite.Create(texture, textureRect, new Vector2(0, 1), 1);
- sr.gameObject.SetActive(true);
- sr.gameObject.SetActive(false);
-
- if (!IsFrameBlank(sr))
- {
- m_TotalNumberOfFrames = frameNumber;
- }
- }
- // m_TotalNumberOfFrames = frameNumber - 1;
-
- m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
- }
- //================================================================================
- //
- //================================================================================
- public void Play()
- {
- Invoke ("NextFrame", m_TimePerFrame);
- Playing = true;
-
- OnMouseEnter();
- }
- //================================================================================
- //
- //================================================================================
- private void NextFrame()
- {
- m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(false);
- m_CurrentFrame++;
- if (m_CurrentFrame >= m_TotalNumberOfFrames)
- {
- m_CurrentFrame = 0;
- m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
- m_Scene.PlayNextFrame();
- Playing = false;
- OnMouseExit();
- return;
- }
-
- m_PaintingFrames[m_CurrentFrame].gameObject.SetActive(true);
- Invoke ("NextFrame", m_TimePerFrame);
- }
- //================================================================================
- //
- //================================================================================
- public void EnableClick()
- {
- GetComponent<Collider2D>().enabled = true;
- }
- //================================================================================
- //
- //================================================================================
- public void DisableClick()
- {
- GetComponent<Collider2D>().enabled = false;
- }
- //================================================================================
- //
- //================================================================================
- private string GetFramePath(int frameNumber)
- {
- string path = "";
-
- switch(m_Color)
- {
- case PaintingFrameColor.GREEN_PAINTING_FRAME:
- path+="Painting01/";
- break;
- case PaintingFrameColor.RED_PAINTING_FRAME:
- path+="Painting02/";
- break;
- case PaintingFrameColor.PINK_PAINTING_FRAME:
- path+="Painting03/";
- break;
- case PaintingFrameColor.BLUE_PAINTING_FRAME:
- path+="Painting04/";
- break;
- case PaintingFrameColor.CYAN_PAINTING_FRAME:
- path+="Painting05/";
- break;
- }
-
- path += "frame" + string.Format("{0:00}", frameNumber);
- return path;
- }
- public bool IsFrameBlank(SpriteRenderer frame)
- {
- if (frame.sprite.texture == null)
- {
- return true;
- }
- Color[] colors = frame.sprite.texture.GetPixels ();
-
- foreach (Color c in colors)
- {
- if (c != Color.white)
- {
- return false;
- }
- }
- return true;
- }
- //================================================================================
- //
- //================================================================================
- public bool Playing
- {
- get;
- private set;
- }
- //================================================================================
- //
- //================================================================================
- public int TotalNumberOfFrames
- {
- get { return m_TotalNumberOfFrames; }
- }
- } // public class CloudNinePaintingFrame : MonoBehaviour
- } // namespace ReaderRabbit
|