using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Picplay : MonoBehaviour
{
Image m_image;
int picindex;//圖片名稱
string filepath;//圖片所在文件夾
public float loadspeed;//每幀加載速度
bool bload;
bool bsub;
// Use this for initialization
void Start()
{
m_image = GameObject.Find("Image").GetComponent<Image>();
filepath = Application.streamingAssetsPath;
m_image.fillAmount = 0;
bload = false;
bsub = false;
Picplaycontrol(0);
}
// Update is called once per frame
void Update()
{
if (bload)
{
m_image.fillAmount += loadspeed;
if (m_image.fillAmount == 1) bload = false;
}
if (bsub)
{
m_image.fillAmount -= loadspeed;
if (m_image.fillAmount == 0) bsub = false;
}
}
/// <summary>
/// 調用輪播
/// </summary>
/// <param name="i">圖片名稱</param>
public void Picplaycontrol(int i)
{
picindex = i;
print(i);
InvokeRepeating("Loadpic", 0, 5f);
}
void Loadpic()
{
string[] dirs = System.IO.Directory.GetFileSystemEntries(filepath);
if (picindex > dirs.Length)
{
picindex = 0;
}
string picpath = filepath + "/" + picindex.ToString() + ".png";
if (File.Exists(picpath))
{
print(2);
byte[] bt = File.ReadAllBytes(picpath);
Texture2D t2d = new Texture2D(1920, 1080);
t2d.LoadImage(bt);
Sprite sprite = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), Vector2.zero);
m_image.sprite = sprite;
Invoke("Setboolload", 0.2f);//因爲加載圖片需要時間
Invoke("Setboolsub", 3f);
picindex++;
}
}
public void Setboolload()
{
bload = true;
}
public void Setboolsub()
{
bsub = true;
}
}