Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Last active June 1, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anzfactory/13a23b00fd84b218e5c11dce6925c243 to your computer and use it in GitHub Desktop.
Save anzfactory/13a23b00fd84b218e5c11dce6925c243 to your computer and use it in GitHub Desktop.
画像を裏返すようなアニメーション
/*********************************
* Flipしたときに画像差し替えたいな
* with DOTween
*********************************/
using System;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class FlipImage : Image
{
private Sprite sourceImage;
private bool isFlipped = false;
public bool IsFlipped {
get { return this.isFlipped; }
}
/// <summary>
/// 裏返し
/// </summary>
/// <param name="duration">アニメーション秒</param>
/// <param name="flipImage">裏返した時にセットする画像</param>
public void Flip(float duration, Sprite flipImage)
{
Flip(duration, flipImage, null);
}
/// <summary>
/// 裏返し
/// </summary>
/// <param name="duration">アニメーション秒</param>
/// <param name="flipImage">裏返した時にセットする画像</param>
/// <param name="callback">裏返し完了時によぶコールバック</param>
public void Flip(float duration, Sprite flipImage, Action callback)
{
if (this.isFlipped) {
return;
}
this.isFlipped = true;
this.sourceImage = this.sprite;
var seq = DOTween.Sequence();
var rotation = this.rectTransform.rotation;
var halfDuration = duration / 2f;
seq.Append(
this.rectTransform
.DORotate(new Vector3(rotation.x, 90, rotation.z), halfDuration, RotateMode.FastBeyond360)
.OnComplete(() => {
if (flipImage != null) {
this.sprite = flipImage;
}
})
);
seq.Append(
this.rectTransform
.DORotate(new Vector3(rotation.x, -90, rotation.z), halfDuration, RotateMode.FastBeyond360)
.SetRelative(true)
);
seq.OnComplete(() => {
if (callback != null) {
callback();
}
});
}
/// <summary>
/// 戻す
/// </summary>
/// <param name="duration">アニメーション秒</param>
public void Reverse(float duration)
{
Reverse(duration, null);
}
/// <summary>
/// 戻す
/// </summary>
/// <param name="duration">アニメーション秒</param>
/// <param name="callback">戻し完了時によぶコールバック</param>
public void Reverse(float duration, Action callback)
{
if (!this.isFlipped) {
return;
}
this.isFlipped = false;
var seq = DOTween.Sequence();
var rotation = this.rectTransform.rotation;
var halfDuration = duration / 2f;
seq.Append(
this.rectTransform
.DORotate(new Vector3(rotation.x, 90, rotation.z), halfDuration, RotateMode.FastBeyond360)
.OnComplete(() => {
if (this.sourceImage != null) {
this.sprite = this.sourceImage;
}
})
);
seq.Append(
this.rectTransform
.DORotate(new Vector3(rotation.x, -90, rotation.z), halfDuration, RotateMode.FastBeyond360)
.SetRelative(true)
.OnComplete(() => {
rotation.y = 0;
this.rectTransform.rotation = rotation;
})
);
seq.OnComplete(() => {
this.sourceImage = null;
if (callback != null) {
callback();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment