//================================================================================ // //================================================================================ 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 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(); 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(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().enabled = true; } //================================================================================ // //================================================================================ public void DisableClick() { GetComponent().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