Skip to content

Instantly share code, notes, and snippets.

@geniikw
Created December 31, 2019 02:29
Show Gist options
  • Select an option

  • Save geniikw/1131f099801d77cd8a3c54a6090742d4 to your computer and use it in GitHub Desktop.

Select an option

Save geniikw/1131f099801d77cd8a3c54a6090742d4 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
using UnityEngine.UI;
public static class CC
{
/// <typeparam name="TOwner">보간을 값을 가지고 있는 대상</typeparam>
/// <typeparam name="TValue">보간할 값의 타입</typeparam>
/// <returns></returns>
public static IEnumerator TweenRoutine<TOwner, TValue>(
TOwner target,
Func<TValue> end,
Func<TOwner, TValue> startGetter,
Action<TOwner, TValue> setter,
Func<TValue, TValue, float, TValue> lerpFunc,
TimeContainer tc,
AnimationCurve curve = null)
{
if (curve == null)
curve = AnimationCurve.Linear(0, 0, 1, 1);
TValue start = default(TValue);
if (startGetter != null)
start = startGetter(target);
while (tc.t < 1f)
{
tc.t += Time.deltaTime / tc.time;
if (setter != null && lerpFunc != null)
setter(target, lerpFunc(start, end(), curve.Evaluate(tc.t)));
yield return null;
}
if (setter != null && lerpFunc != null)
setter(target, end());
}
public static Coroutine Scale(this MonoBehaviour runner, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => t.localScale, (t, e) => t.localScale = e, Vector3.LerpUnclamped, time, curve));
}
public static Coroutine Scale(this MonoBehaviour runner, Vector3 start, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => start, (t, e) => t.localScale = e, Vector3.LerpUnclamped, time, curve));
}
public static Coroutine Move(this MonoBehaviour runner, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => t.position, (t, e) => t.position = e, Vector3.Lerp, time, curve));
}
public static Coroutine Move(this MonoBehaviour runner, Transform end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end.position, t => t.position, (t, e) => t.position = e, Vector3.Lerp, time, curve));
}
public static Coroutine Stay(this MonoBehaviour runner, Transform end, TimeContainer time)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end.position, t => t.position, (t, e) => t.position = e, (a,b,t)=>b, time));
}
public static Coroutine Move(this MonoBehaviour runner, Vector3 start, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => start, (t, e) => t.position = e, Vector3.Lerp, time, curve));
}
public static Coroutine MoveUI(this MonoBehaviour runner, Vector2 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.GetComponent<RectTransform>(), ()=>end, t => t.anchoredPosition, (t, e) => t.anchoredPosition = e, Vector2.Lerp, time, curve));
}
public static Coroutine ChangeUISize(this MonoBehaviour runner, Vector2 end, TimeContainer time, AnimationCurve curve =null){
return runner.StartCoroutine(TweenRoutine(runner.GetComponent<RectTransform>(), ()=>end, t => t.sizeDelta, (t, e) => t.sizeDelta = e, Vector2.Lerp, time, curve));
}
public static Coroutine MoveLocal(this MonoBehaviour runner, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => t.localPosition, (t, e) => t.localPosition = e, Vector3.Lerp, time, curve));
}
public static Coroutine MoveLocal(this MonoBehaviour runner, Vector3 start, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => start, (t, e) => t.localPosition = e, Vector3.Lerp, time, curve));
}
public static Coroutine MoveRelatively(this MonoBehaviour runner, Vector3 dis, TimeContainer time, AnimationCurve curve = null)
{
var end = runner.transform.position + dis;
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => t.position, (t, e) => t.position = e, Vector3.Lerp, time, curve));
}
public static Coroutine Rotation(this MonoBehaviour runner, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => t.eulerAngles, (t, e) => t.eulerAngles = e, Vector3.Lerp, time, curve));
}
public static Coroutine Rotation(this MonoBehaviour runner, Vector3 start, Vector3 end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner.transform, ()=>end, t => start, (t, e) => t.eulerAngles = e, Vector3.Lerp, time, curve));
}
public static Coroutine Wait(this MonoBehaviour runner, TimeContainer wait, AnimationCurve curve = null)
{
//2번째 인자와 템플린 인자는 필요없긴함.
return runner.StartCoroutine(TweenRoutine<object, object>(null, null, null, null, null, wait));
}
public static Coroutine AlphaTween(this SkeletonAnimation runner, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => sp.skeleton.a, (sp, a) => sp.skeleton.a = a, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this SkeletonAnimation runner, float start, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => start, (sp, a) => sp.skeleton.a = a, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this UIWidget runner, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => sp.alpha, (sp, a) => sp.alpha = a, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this UIWidget runner, float start, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => start, (sp, a) => sp.alpha = a, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this UIPanel runner, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => sp.alpha, (sp, a) => sp.alpha = a, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this UIPanel runner, float start, float end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => start, (sp, a) => sp.alpha = a, Mathf.Lerp, time, curve));
}
public static Coroutine ColorTween(this UISprite runner, Color start, Color end, TimeContainer time, AnimationCurve curve = null)
{
return runner.StartCoroutine(TweenRoutine(runner, () => end, sp => start, (sp, a) => sp.color = a, Color.Lerp, time, curve));
}
public static Coroutine AlphaTween(this Graphic runner, float end, TimeContainer time,bool containChild =false, AnimationCurve curve = null)
{
if(containChild){
var child = runner.transform.GetComponentsInChildren<Graphic>();
foreach(var c in child)
c.AlphaTween(end,time,false,curve);
}
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => sp.color.a, UGUIUtil.GraphicAlphaSetter, Mathf.Lerp, time, curve));
}
public static Coroutine AlphaTween(this Graphic runner, float start, float end, TimeContainer time,bool containChild=false, AnimationCurve curve = null)
{
if(containChild){
var child = runner.transform.GetComponentsInChildren<Graphic>();
foreach(var c in child)
c.AlphaTween(start,end,time,false,curve);
}
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, sp => start, UGUIUtil.GraphicAlphaSetter, Mathf.Lerp, time, curve));
}
public static Coroutine MoveWithSpeed(this MonoBehaviour runner, Vector3 end, TimeContainer time, AnimationCurve speedCurve, float speed = 1f, Action onUpdate = null)
{
return runner.StartCoroutine(SpeedRoutine(runner.transform, end, time, speedCurve, speed, onUpdate));
}
public static Coroutine FillTween(this Image runner, float start, float end, TimeContainer time, AnimationCurve curve =null){
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, r=>start,(p,i)=>p.fillAmount=i,Mathf.Lerp,time,curve));
}
public static Coroutine FillTween(this Image runner, float end, TimeContainer time, AnimationCurve curve =null){
return runner.StartCoroutine(TweenRoutine(runner, ()=>end, r=>r.fillAmount,(p,i)=>p.fillAmount=i,Mathf.Lerp,time,curve));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment