using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class AnimLoop : MonoBehaviour
{
public enum AnimLoopType
{
MoveX, //水平
MoveY, //垂直
Fade, //格子
RotateZ, //Z轴旋转
Scale, //尺寸
}
private Tweener tweener = null;
private Transform animGo;
public AnimLoopType m_AnimLoopType = AnimLoopType.MoveX;
public float offset = -6.0f;
public Color end_color = Color.white;
public float time = 0.8f;
public Ease easeType = Ease.Linear;
public bool automatic = true;
public Transform AnimGo {
get {
if (this.animGo == null) {
this.animGo = this.transform;
}
return this.animGo;
}
}
private Image animImage;
public Image AnimImage
{
get
{
if (this.animImage == null)
{
this.animImage = this.GetComponent<Image>();
}
return this.animImage;
}
}
private Vector3 old_scale = Vector3.one;
private void Start()
{
old_scale = AnimGo.localScale;
}
private void OnEnable()
{
if(automatic) onStart();
}
public void onStart()
{
if (tweener == null)
{
if (m_AnimLoopType == AnimLoopType.MoveX) { tweener = AnimGo.DOLocalMoveX(AnimGo.localPosition.x + offset, time); }
else if (m_AnimLoopType == AnimLoopType.MoveY) { tweener = AnimGo.DOLocalMoveY(AnimGo.localPosition.y + offset, time); }
else if (m_AnimLoopType == AnimLoopType.RotateZ) { tweener = AnimGo.DOLocalRotateZ(AnimGo.localEulerAngles.z + offset, time); }
else if (m_AnimLoopType == AnimLoopType.Scale) { tweener = AnimGo.DOScale(new Vector3(offset, offset, offset), time); }
else
{
tweener = AnimImage.DOColor(end_color, time);
}
tweener.SetLoops(-1, LoopType.Yoyo).SetEase(easeType);
tweener.Pause();
}
if (tweener != null)
{
tweener.Play();
}
}
public void onStop()
{
if (tweener != null)
{
tweener.Pause();
}
if (m_AnimLoopType == AnimLoopType.Scale)
{
AnimGo.localScale = old_scale;
}
}
private void OnDisable()
{
onStop();
}
private void OnDestroy()
{
if (tweener != null) {
tweener.Kill();
tweener = null;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
namespace XGUI
{
public class XEffectTween : MonoBehaviour
{
public enum EaseType
{
dotweenEase = 1,
animCurve = 2,
}
public enum EffectType
{
rock, //摇摆
translate, //平移
shake,//震动
}
public EffectType effectType;
public EaseType easeType = EaseType.dotweenEase;
[SerializeField]
private AnimationCurve animCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0f, 0f), new Keyframe(3f, 40f) });
public Ease ease = Ease.OutQuad;
private RectTransform effectRect;
public float duration = 1.0f;
public float delay = 0.0f;
public int loops = 0;
public LoopType loopType;
public Vector3 startVect = Vector3.zero;
public Vector3 endVect = Vector3.zero;
public bool playAutomatically = false;
private void Awake()
{
if (effectRect == null)
{
effectRect = gameObject.GetComponent<RectTransform>();
}
}
void OnEnable()
{
if (playAutomatically)
{
Play(null);
}
}
private void OnDisable()
{
Stop();
}
public void Play(TweenCallback onCompleFun)
{
if (effectRect == null) return;
if (delay > 0)
StartCoroutine(DoWait(onCompleFun));
else
DoPlay(onCompleFun);
}
private IEnumerator DoWait(TweenCallback onCompleFun)
{
yield return new WaitForSeconds(delay);
DoPlay(onCompleFun);
}
private void DoPlay(TweenCallback onCompleFun)
{
Tweener tweener = GetEffect();
if (tweener!=null)
{
tweener.SetLoops(loops, loopType);
if (easeType == EaseType.animCurve)
tweener.SetEase(animCurve);
else
tweener.SetEase(ease);
if (onCompleFun != null)
{
tweener.OnComplete(delegate () {
onCompleFun();
});
}
}
}
public Tweener GetEffect()
{
Tweener tweener = null;
effectRect.DOKill(false);
switch (effectType)
{
case EffectType.rock:
tweener = RockEffect();
break;
case EffectType.translate:
tweener = TranslateEffect();
break;
case EffectType.shake:
tweener = ShakeEffect();
break;
}
return tweener;
}
//摇摆特效
private Tweener RockEffect()
{
effectRect.localRotation = Quaternion.Euler(startVect);
Tweener tweener = effectRect.DOLocalRotate(endVect, duration);
return tweener;
}
//平移特效
private Tweener TranslateEffect()
{
effectRect.anchoredPosition = startVect;
Tweener tweener = effectRect.DOAnchorPos(endVect, duration);
return tweener;
}
//平移特效
private Tweener ShakeEffect()
{
effectRect.anchoredPosition = startVect;
Tweener tweener = effectRect.DOShakeAnchorPos(duration, endVect);
return tweener;
}
public void Stop()
{
switch (effectType)
{
case EffectType.rock:
effectRect.localRotation = Quaternion.Euler(endVect);
break;
case EffectType.translate:
effectRect.anchoredPosition = endVect;
break;
}
effectRect.DOKill(false);
}
private void OnDestroy()
{
effectRect.DOKill(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace XGUI
{
public class XFloatTexts:MonoBehaviour
{
public enum FloatDir
{
UP,
DOWN
}
class FloatData
{
public Text text;
public RectTransform rect;
public Vector2 position;
public float alpha = 1.0f;
public CanvasGroup canvsgroup;
public float birthTime;
public void Draw()
{
rect.anchoredPosition = position;
canvsgroup.alpha = alpha;
}
}
[SerializeField]
private bool showEditor;
[SerializeField]
private Text template;
[SerializeField]
private FloatDir direction = FloatDir.UP;
public float interval = 0.2f; //间隔多久飘一个
public float speed = 2.0f; //m/s
public float fadeSpeed = 1.0f; // 0 - 1
public float fadeDelay = 0.1f;
private RectTransform m_transform;
private float markInterval = 0;
private List<FloatData> loops = new List<FloatData>();
private List<FloatData> floats = new List<FloatData>();
private List<string> waits = new List<string>();
private void Awake()
{
m_transform = gameObject.GetComponent<RectTransform>();
if (template != null) template.SetActive(false);
}
public void Show(string str)
{
if (string.IsNullOrEmpty(str)) return;
waits.Add(str);
}
private FloatData GetItem(string str)
{
FloatData fdata = null;
if (loops.Count > 0)
{
fdata = loops[0];
loops.RemoveAt(0);
}
else
{
Text text = GameObject.Instantiate<Text>(template);
text.SetActive(true);
text.transform.SetParentOEx(m_transform);
CanvasGroup canvsgroup = text.gameObject.GetComponent<CanvasGroup>();
if(canvsgroup == null) canvsgroup = text.gameObject.AddComponent<CanvasGroup>();
fdata = new FloatData();
fdata.text = text;
fdata.rect = text.rectTransform;
fdata.canvsgroup = canvsgroup;
}
fdata.text.text = str;
fdata.alpha = 1;
fdata.position = Vector2.zero;
fdata.birthTime = Time.unscaledTime + fadeDelay;
return fdata;
}
private void PushItem(FloatData fdata)
{
fdata.rect.anchoredPosition = new Vector2(0, 10000);
loops.Add(fdata);
}
private void Update()
{
if(showEditor)
{
if (Time.frameCount % 20 == 0) Show("+300");
}
float deltaTime = Time.deltaTime;
float unscaledTime = Time.unscaledTime;
if (waits.Count > 0)
{
if (markInterval == 0 || unscaledTime - markInterval > interval)
{
markInterval = unscaledTime;
floats.Add(GetItem(waits[0]));
waits.RemoveAt(0);
}
}
if(floats.Count > 0)
{
for(int i = 0;i< floats.Count;i++)
{
FloatData fData = floats[i];
if(unscaledTime > fData.birthTime)
{
fData.alpha -= this.fadeSpeed * deltaTime;
if (fData.alpha <= 0)
{
//移除
PushItem(fData);
fData.Draw();
floats.RemoveAt(i);
i--;
continue;
}
}
float dx = fData.position.x;
float dy = fData.position.y;
if(direction == FloatDir.UP)
{
dy += speed * deltaTime;
}
else if (direction == FloatDir.DOWN)
{
dy -= speed * deltaTime;
}
fData.position.x = dx;
fData.position.y = dy;
fData.Draw();
}
}
}
public void Clear()
{
waits.Clear();
}
// 内部对象随主体一起毁灭
//private void OnDestroy()
//{
//}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
public class XImageFrameAnimator : MonoBehaviour {
private Image ImageSource;
private int mCurFrame = 0;
private float mDelta = 0;
public float FPS = 5;
public List<Sprite> SpriteFrames;
public bool IsPlaying = false;
public bool Inversion = false;
public bool Loop = false;
public int FrameCount
{
get
{
return SpriteFrames.Count;
}
}
void Awake()
{
ImageSource = GetComponent<Image>();
}
private void SetSprite(int idx)
{
ImageSource.sprite = SpriteFrames[idx];
//ImageSource.SetNativeSize();
}
public void Play()
{
IsPlaying = true;
Inversion = true;
}
public void PlayReverse()
{
IsPlaying = true;
Inversion = false;
}
void Update()
{
if (!IsPlaying || 0 == FrameCount)
{
return;
}
mDelta += Time.deltaTime;
if (mDelta > 1 / FPS)
{
mDelta = 0;
if(Inversion)
{
mCurFrame--;
}
else
{
mCurFrame++;
}
if (mCurFrame >= FrameCount)
{
if (Loop)
{
mCurFrame = 0;
}
else
{
IsPlaying = false;
return;
}
}
else if (mCurFrame<0)
{
if (Loop)
{
mCurFrame = FrameCount-1;
}
else
{
IsPlaying = false;
return;
}
}
SetSprite(mCurFrame);
}
}
public void Pause()
{
IsPlaying = false;
}
public void Resume()
{
if (!IsPlaying)
{
IsPlaying = true;
}
}
public void Stop()
{
mCurFrame = 0;
SetSprite(mCurFrame);
IsPlaying = false;
}
public void Rewind()
{
mCurFrame = 0;
SetSprite(mCurFrame);
Play();
}
}