using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
public enum SequencePatternType
{
/// <summary>
/// 正循环//自动播放
/// </summary>
ForthLoop,
/// <summary>
/// 反循环//自动播放
/// </summary>
BackLoop,
/// <summary>
/// 正反循环//自动播放
/// </summary>
ForthBackLoop,
/// <summary>
/// 正一次//按钮点击事件
/// </summary>
ForthOne,
/// <summary>
/// 反一次//按钮点击事件
/// </summary>
BackOne,
/// <summary>
/// 正反一次//按钮点击事件
/// </summary>
ForthBackOne,
/// <summary>
/// 空
/// </summary>
Max
}
/// <summary>
/// forth back pattern type
/// </summary>
public enum ForthBackState
{
Forth,
ForthStop,
BackStop,
Back
}
/// <summary>
/// 序列帧动画播放
/// 19.06.28 by Rocketjie
/// </summary>
public class SequenceSprite : MonoBehaviour {
/// <summary>
/// sprites 播放模式
/// </summary>
[Tooltip("Sequence Pattern (Default Is Loop)")]
public SequencePatternType PatternType = SequencePatternType.ForthLoop;
/// <summary>
/// SpriteList(当SpriteList添加图片后不再读取Texture2D)
/// </summary>
[Tooltip("Sequence Sprite List")]
public List<Sprite> SpriteList = new List<Sprite>();
/// <summary>
/// Texture2D
/// 图集中图片数量大于1;
/// 图集中图片名称格式为:字母 + “_” + 数字(数字必须以0开始);
/// 图集中图片名称格式例如:effect_0、effect_1、effect_2 ;
/// 当SpriteList没有添加图片时读取Texture2D
/// </summary>
[Tooltip("Texture2D Atlas")]
public Texture2D Texture2DAtlas = null;
/// <summary>
/// 间隔时间
/// </summary>
[Tooltip("Sequence Sprite Turn Time")]
public float TurnTime = 0.05f;
/// <summary>
/// 序列帧播放 sprite
/// </summary>
[Tooltip("Sprite Is Self Component (Default)")]
public Image TargetSprite = null;
/// <summary>
/// 序列帧播放 触发按钮
/// </summary>
[Tooltip("Button Is Self Component (Default)")]
public Button TargetButton = null;
private int img_index = 0;
private float deltTime = 0;
private SequencePatternType currentType = SequencePatternType.Max;
private void Awake()
{
if (TargetSprite == null)
{
TargetSprite = gameObject.GetComponent<Image>();
}
if (TargetButton == null)
{
TargetButton = gameObject.GetComponent<Button>();
}
if (SpriteList.Count <= 0 && Texture2DAtlas != null)
{
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Texture2DAtlas));
string path = rootPath + "/" + Texture2DAtlas.name + ".PNG";
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;
if(texImp.spritesheet.Length <= 1)
{
Logger.LogError("Texture2D Format incorrect");
return;
}
int indexOf = 0;
for (int index = 0; index < texImp.spritesheet.Length; index++)
{
foreach (SpriteMetaData temp in texImp.spritesheet)
{
indexOf = temp.name.IndexOf('_');
if (indexOf == -1)
{
Logger.LogError("Texture2D Format incorrect");
return;
}
else
{
if (int.Parse(temp.name.Substring(indexOf + 1)) == index)
{
Sprite spr = Sprite.Create(Texture2DAtlas, temp.rect, Vector2.zero);
SpriteList.Add(spr);
}
}
}
}
}
}
// Use this for initialization
void Start ()
{
if(TargetButton != null)
{
TargetButton.onClick.AddListener(OnClick);
}
}
/// <summary>
/// button click
/// </summary>
private void OnClick()
{
switch (PatternType)
{
case SequencePatternType.BackOne:
SetBackOneState();
break;
case SequencePatternType.ForthOne:
SetForthOneState();
break;
case SequencePatternType.ForthBackOne:
SetForthBackOneState();
break;
}
}
/// <summary>
/// 设置 forth one state
/// </summary>
private void SetForthOneState()
{
if (forthOne_State == ForthBackState.ForthStop)
{
forthOne_State = ForthBackState.Forth;
}
}
/// <summary>
/// 设置 back one state
/// </summary>
private void SetBackOneState()
{
if (backOne_State == ForthBackState.BackStop)
{
backOne_State = ForthBackState.Back;
}
}
/// <summary>
/// 设置 forth back one state
/// </summary>
private void SetForthBackOneState()
{
if (forthBackOneState == ForthBackState.ForthStop)
{
forthBackOneState = ForthBackState.Back;
}
else if (forthBackOneState == ForthBackState.BackStop)
{
forthBackOneState = ForthBackState.Forth;
}
}
/// <summary>
/// 检测 pattern type
/// </summary>
private void CheckPatterType()
{
if (currentType != PatternType)
{
currentType = PatternType;
switch (PatternType)
{
case SequencePatternType.ForthLoop:
img_index = 0;
break;
case SequencePatternType.BackLoop:
img_index = SpriteList.Count - 1;
break;
case SequencePatternType.ForthBackLoop:
img_index = 0;
forthBack_loopState = ForthBackState.Forth;
break;
case SequencePatternType.ForthOne:
img_index = 0;
forthOne_State = ForthBackState.ForthStop;
break;
case SequencePatternType.BackOne:
img_index = SpriteList.Count - 1;
backOne_State = ForthBackState.BackStop;
break;
case SequencePatternType.ForthBackOne:
img_index = 0;
forthBackOneState = ForthBackState.BackStop;
break;
}
}
}
private void SetImageSprite()
{
if (img_index >= 0 && img_index < SpriteList.Count)
{
TargetSprite.sprite = SpriteList[img_index];
}
else
{
Logger.LogError("SequenceSprite SpriteList'img_index is error !");
}
}
// Update is called once per frame
void Update()
{
if (TargetSprite == null || SpriteList.Count <= 1)
{
return;
}
CheckPatterType();
switch (PatternType)
{
case SequencePatternType.ForthLoop:
SequenceForthLoop();
break;
case SequencePatternType.BackLoop:
SequenceBackLoop();
break;
case SequencePatternType.ForthBackLoop:
SequenceForthBackLoop();
break;
case SequencePatternType.ForthOne:
SequenceForthOne();
break;
case SequencePatternType.BackOne:
SequenceBackOne();
break;
case SequencePatternType.ForthBackOne:
SequenceForthBackOne();
break;
}
}
#region Forth Loop
/// <summary>
/// Forth Loop
/// </summary>
private void SequenceForthLoop()
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Forth Loop
img_index++;
if (img_index == SpriteList.Count - 1)
{
img_index = 0;
}
}
}
#endregion Forth Loop
#region Back Loop
/// <summary>
/// Back Loop
/// </summary>
private void SequenceBackLoop()
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Back Loop
img_index--;
if (img_index == 0)
{
img_index = SpriteList.Count - 1;
}
}
}
#endregion Back Loop
#region Forth Back Loop
/// <summary>
/// Forth Back Loop 状态
/// </summary>
private ForthBackState forthBack_loopState = ForthBackState.Forth;
/// <summary>
/// Forth Back Loop
/// </summary>
private void SequenceForthBackLoop()
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Forth Back Loop
if (forthBack_loopState == ForthBackState.Forth)
{
img_index++;
}
else if (forthBack_loopState == ForthBackState.Back)
{
img_index--;
}
if (img_index >= SpriteList.Count - 1)
{
img_index = SpriteList.Count - 1;
forthBack_loopState = ForthBackState.Back;
}
else if (img_index <= 0)
{
img_index = 0;
forthBack_loopState = ForthBackState.Forth;
}
}
}
#endregion Forth Back Loop
#region Forth One
/// <summary>
/// Forth One 状态
/// </summary>
private ForthBackState forthOne_State = ForthBackState.ForthStop;
/// <summary>
/// Forth One
/// </summary>
private void SequenceForthOne()
{
if (forthOne_State == ForthBackState.Forth)
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Forth One
img_index++;
if (img_index == SpriteList.Count - 1)
{
img_index = 0;
forthOne_State = ForthBackState.ForthStop;
}
}
}
}
#endregion Forth One
#region Back One
/// <summary>
/// Back One 状态
/// </summary>
private ForthBackState backOne_State = ForthBackState.BackStop;
/// <summary>
/// Back One
/// </summary>
private void SequenceBackOne()
{
if (backOne_State == ForthBackState.Back)
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Back One
img_index--;
if (img_index == 0)
{
img_index = SpriteList.Count - 1;
backOne_State = ForthBackState.BackStop;
}
}
}
}
#endregion Back One
#region Forth Back One
/// <summary>
/// Forth Back One 状态
/// </summary>
private ForthBackState forthBackOneState = ForthBackState.BackStop;
/// <summary>
/// forth back one
/// </summary>
private void SequenceForthBackOne()
{
if (forthBackOneState == ForthBackState.Forth)
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Forth Back One (Forth)
img_index++;
}
if (img_index == SpriteList.Count - 1)
{
forthBackOneState = ForthBackState.ForthStop;
}
}
else if (forthBackOneState == ForthBackState.Back)
{
deltTime += Time.deltaTime;
if (deltTime > TurnTime)
{
deltTime = 0;
SetImageSprite();//Forth Back One (Back)
img_index--;
}
if (img_index == 0)
{
forthBackOneState = ForthBackState.BackStop;
}
}
}
#endregion Forth Back One
}