用协程进行实现UI的缩放、平移、淡入淡出
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
public class PanelAni : MonoBehaviour {
static public PanelAni GetPanelAniCompFrom(GameObject panel)
{
PanelAni ani_comp = panel.GetComponent<PanelAni>();
if (ani_comp == null) ani_comp = panel.AddComponent<PanelAni>();
return ani_comp;
}
//------------------------------ 初始化 ------------------------------//
Image pic_comp;
private void Awake()
{
}
//------------------------------ 接口 ----------------------------------//
//移动
public void Move(Vector2 start_position, Vector3 to_position, float time, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopMoving();
this.transform.localPosition = start_position;
move_coroutine = StartCoroutine(MoveTo(to_position, time, call_back));
}
public void Move(Vector3[] position, float[] time, bool is_loop, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopMoving();
if (position.Length != time.Length)
{
Debug.LogWarning("数组长度不一致");
return;
}
move_coroutine = StartCoroutine(MoveLoop(position, time, is_loop, call_back));
}
//缩放
public void Scale(Vector3 start_scale, Vector3 to_scale, float time, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopZooming();
this.transform.localScale = start_scale;
scale_coroutine = StartCoroutine(ScaleTo(to_scale, time, call_back));
}
public void Scale(Vector3[] scale, float[] time, bool is_loop, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopZooming();
if (scale.Length != time.Length)
{
Debug.LogWarning("数组长度不一致");
return;
}
scale_coroutine = StartCoroutine(ScaleLoop(scale, time, is_loop, call_back));
}
//旋转
public void Rotate(float start_angle, float to_angle, float time, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopRotating();
this.transform.rotation = Quaternion.AngleAxis(start_angle, new Vector3(0,1,0));
scale_coroutine = StartCoroutine(RotateTo(to_angle, time, call_back));
}
public void Rotate(float[] angle, float[] time, bool is_loop, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (interrupt) StopRotating();
if (angle.Length != time.Length)
{
Debug.LogWarning("数组长度不一致");
return;
}
rotate_coroutine = StartCoroutine(RotateLoop(angle, time, is_loop, call_back));
}
//淡入淡出
public void Fade(float start_alpha, float to_alpha, float time, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (pic_comp == null)
{
pic_comp = this.GetComponent<Image>();
if (pic_comp == null)
{
Debug.LogWarning("找不到图片组件");
return;
}
}
if (interrupt) StopFading();
Color col = pic_comp.color;
col.a = start_alpha;
this.pic_comp.color = col;
fade_coroutine = StartCoroutine(FadeTo(to_alpha, time, call_back));
}
public void Fade(float[] alpha, float[] time, bool is_loop, bool interrupt, Action call_back)
{
if (!this.gameObject.activeInHierarchy) return;
if (pic_comp == null)
{
pic_comp = this.GetComponent<Image>();
if (pic_comp == null)
{
Debug.LogWarning("找不到图片组件");
return;
}
}
if (interrupt) StopFading();
if (alpha.Length != time.Length)
{
Debug.LogWarning("数组长度不一致");
return;
}
fade_coroutine = StartCoroutine(FadeLoop(alpha, time, is_loop, call_back));
}
//停止动画
public void StopMoving()
{
if (move_coroutine == null) return;
StopCoroutine(move_coroutine);
}
public void StopFading()
{
if (fade_coroutine == null) return;
StopCoroutine(fade_coroutine);
}
public void StopZooming()
{
if (scale_coroutine == null) return;
StopCoroutine(scale_coroutine);
}
public void StopRotating()
{
if (rotate_coroutine == null) return;
StopCoroutine(rotate_coroutine);
}
public void StopAllAni()
{
StopAllCoroutines();
}
// -------------------------- 状态标记 ---------------------------//
readonly public bool is_moving;
readonly public bool is_fading;
readonly public bool is_zooming;
readonly public bool is_rotating;
Coroutine move_coroutine;
Coroutine fade_coroutine;
Coroutine rotate_coroutine;
Coroutine scale_coroutine;
// ----------------------------- 实现 -----------------------------//
IEnumerator FadeTo(float to_alpha, float time, Action call_back)
{
float timer = 0;
Color col = pic_comp.color;
float start_alpha = col.a;
while (timer <= time && Mathf.Abs(col.a - to_alpha) > 0.03f)
{
timer += Time.deltaTime;
float t = timer / time;
float alpha = Mathf.Lerp(col.a, to_alpha, t);
col.a = alpha;
this.pic_comp.color = col;
yield return null;
}
if(call_back != null) call_back();
}
IEnumerator MoveTo(Vector2 to_pos, float time, Action call_back)
{
float timer = 0;
Vector2 start_pos = this.transform.localPosition;
while(timer <= time)
{
timer += Time.deltaTime;
float t = timer / time;
Vector2 pos = Vector2.Lerp(this.transform.localPosition, to_pos, t);
this.transform.localPosition = pos;
yield return null;
}
if (call_back != null) call_back();
}
IEnumerator ScaleTo(Vector3 to_scale, float time, Action call_back)
{
float timer = 0;
Vector3 start_scale = this.transform.localScale;
while (timer <= time)
{
timer += Time.deltaTime;
float t = timer / time;
Vector3 scale = Vector3.Lerp(this.transform.localScale, to_scale, t);
this.transform.localScale = scale;
yield return null;
}
if (call_back != null) call_back();
}
IEnumerator RotateTo(float to_angle, float time, Action call_back)
{
Quaternion start_rotation = this.transform.localRotation;
Quaternion to_rotation = Quaternion.AngleAxis(to_angle, new Vector3(0, 1, 0));
float timer = 0;
while (timer <= time)
{
timer += Time.deltaTime;
float t = timer / time;
Quaternion rotation = Quaternion.Lerp(this.transform.localRotation, to_rotation, t);
this.transform.localRotation = rotation;
yield return null;
}
if (call_back != null) call_back();
}
// -------------------------------- 循环 ---------------------------------------------//
IEnumerator ScaleLoop(Vector3[] to_scale, float[] time, bool is_loop, Action call_back)
{
while (true)
{
for (int i = 0; i < time.Length; i++)
{
yield return StartCoroutine(ScaleTo(to_scale[i], time[i], null));
}
if (!is_loop) break;
}
if (call_back != null) call_back();
}
IEnumerator RotateLoop(float[] to_angle, float[] time, bool is_loop, Action call_back)
{
while (true)
{
for (int i = 0; i < time.Length; i++)
{
yield return StartCoroutine(RotateTo(to_angle[i], time[i], null));
}
if (!is_loop) break;
}
if (call_back != null) call_back();
}
IEnumerator FadeLoop(float[] to_alpha, float[] time, bool is_loop, Action call_back)
{
while (true)
{
for (int i = 0; i < time.Length; i++)
{
yield return StartCoroutine(FadeTo(to_alpha[i], time[i], null));
}
if (!is_loop) break;
}
if (call_back != null) call_back();
}
IEnumerator MoveLoop(Vector3[] to_pos, float[] time, bool is_loop, Action call_back)
{
while (true)
{
for (int i = 0; i < time.Length; i++)
{
yield return StartCoroutine(MoveTo(to_pos[i], time[i], null));
}
if (!is_loop) break;
}
if (call_back != null) call_back();
}
//------------------------------- 回调方法 ------------------------------//
public void Close()
{
this.gameObject.SetActive(false);
}
}