大家好,我是笨笨,笨笨的笨,笨笨的笨,谢谢!
让动画师调好曲线,程序哥可以直接拿来用了。
代码:https://code.csdn.net/snippets/1034693
/*
----------- 动画曲线转换成C#脚本 ----------
------------By JerryJin 20150905----------
大家好,我是笨笨,笨笨的笨,笨笨的笨,谢谢!
------------------------------------------
NGUI的动画只带了几个缓动函数严重短缺。不过网上已经看到有人扩展过了。
但我还想更自由些,喜欢自已来调曲线更直观。
但最终的效果还是交给程序用代码创建。
又不能污染项目里的东西,于是有了这个脚本。
------------------------------------------
此脚本会在:TweenAlpha,TweenPosition,TweenRotation,TweenScale 中挂一个【复制动画曲线】按钮。
本来想写在 UITweener 就不用这么坑了。代码写的很有乱。U3D不熟好多东西都不会。将就实现效果罢了。
但是不知道怎么实现只找到下面这个方法,所以只改了这四个基础的。其实如果这四个实现不了,我就考虑直接用U3D的动画系统了。
[CustomEditor(typeof(TweenAlpha), true)]
------------------------------------------
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
//获取动画曲线
public static class ConvertAnimationCurveToCode
{
//复制动画曲线参数
public static void ToClipboard(AnimationCurve curve)
{
//Debug.Log("复制动画曲线: \n" + AnimationCurveToCode(curve));
TextEditor te = new TextEditor();
te.content = new GUIContent(AnimationCurveToCode(curve));
te.SelectAll();
te.Copy();
}
//将动画曲线的参数转换成 C# 代码。
private static string AnimationCurveToCode(AnimationCurve curve)
{
string createKeyCode =
"//---------- Create AnimationCurve Begin----------\n" +
"//创建动画曲线,关键帧数量:"+ curve.keys.Length +" 帧\n" +
"AnimationCurve animCurve = new AnimationCurve();\n" +
"Keyframe[] ks = new Keyframe["+ curve.keys.Length +"];\n";
for (int i = 0; i < curve.keys.Length; i++) {
createKeyCode +=
"ks["+i+"] = new Keyframe("+curve.keys[i].time+", "+curve.keys[i].value+");\n" +
"ks["+i+"].inTangent = "+curve.keys[i].inTangent+";\n" +
"ks["+i+"].outTangent = "+curve.keys[i].outTangent+";\n" ;
}
createKeyCode +=
"//animCurve 赋值给NGUI动画的 animationCurve 属性。\n" +
"animCurve = new AnimationCurve(ks); \n" +
"//---------- Create AnimationCurve End ----------\n" ;
//"animCurve.postWrapMode = WrapMode.ClampForever;//循环方式";
return createKeyCode;
}
}
[CustomEditor(typeof(TweenAlpha), true)]
public class TweenAlphaExtension : UITweenerEditor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
if(GUILayout.Button("复制动画曲线"))
{//Debug.Log("输出测试");
UITweener tw =(UITweener)target;
ConvertAnimationCurveToCode.ToClipboard(tw.animationCurve);
}
}
}
[CustomEditor(typeof(TweenPosition), true)]
public class TweenPositionExtension : UITweenerEditor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
if(GUILayout.Button("复制动画曲线"))
{//Debug.Log("输出测试");
UITweener tw =(UITweener)target;
ConvertAnimationCurveToCode.ToClipboard(tw.animationCurve);
}
}
}
[CustomEditor(typeof(TweenRotation), true)]
public class TweenRotationExtension : UITweenerEditor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
if(GUILayout.Button("复制动画曲线"))
{//Debug.Log("输出测试");
UITweener tw =(UITweener)target;
ConvertAnimationCurveToCode.ToClipboard(tw.animationCurve);
}
}
}
[CustomEditor(typeof(TweenScale), true)]
public class TweenScaleExtension : UITweenerEditor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
if(GUILayout.Button("复制动画曲线"))
{//Debug.Log("输出测试");
UITweener tw =(UITweener)target;
ConvertAnimationCurveToCode.ToClipboard(tw.animationCurve);
}
}
}
将 ConvertAnimationCurveToCode.cs 放到:项目\Assets\Editor \ (只要在Assets下,名字为Editor,U3D就能找到。注意美观哦)
调好曲线后点击【复制动画曲线】
生成的效果自动放入剪切板:
//---------- Create AnimationCurve Begin----------
//创建动画曲线,关键帧数量:13 帧
AnimationCurve animCurve = new AnimationCurve();
Keyframe[] ks = new Keyframe[13];
ks[0] = new Keyframe(0, 0);
ks[0].inTangent = -0.02673043;
ks[0].outTangent = -0.02673043;
ks[1] = new Keyframe(0.2698695, 0.07689273);
ks[1].inTangent = -1.281696;
ks[1].outTangent = -1.281696;
ks[2] = new Keyframe(0.3741068, -0.06480181);
ks[2].inTangent = 0.3571016;
ks[2].outTangent = 0.3571016;
ks[3] = new Keyframe(0.4299066, 0.3068273);
ks[3].inTangent = 0;
ks[3].outTangent = 0;
ks[4] = new Keyframe(0.5082815, -0.1418224);
ks[4].inTangent = 0;
ks[4].outTangent = 0;
ks[5] = new Keyframe(0.5784958, 0.6303786);
ks[5].inTangent = -0.7605511;
ks[5].outTangent = -0.7605511;
ks[6] = new Keyframe(0.6428112, -0.2857635);
ks[6].inTangent = 0;
ks[6].outTangent = 0;
ks[7] = new Keyframe(0.7142559, 0.4632396);
ks[7].inTangent = -1.02695;
ks[7].outTangent = -1.02695;
ks[8] = new Keyframe(0.7725148, -0.1664435);
ks[8].inTangent = 0;
ks[8].outTangent = 0;
ks[9] = new Keyframe(0.855494, 0.3427125);
ks[9].inTangent = -0.9688854;
ks[9].outTangent = -0.9688854;
ks[10] = new Keyframe(0.8931202, -0.1386863);
ks[10].inTangent = -7.83197;
ks[10].outTangent = -7.83197;
ks[11] = new Keyframe(0.9551187, 0.1843302);
ks[11].inTangent = 0;
ks[11].outTangent = 0;
ks[12] = new Keyframe(0.990999, 0.007004678);
ks[12].inTangent = 1;
ks[12].outTangent = 0;
//animCurve 赋值给NGUI动画的 animationCurve 属性。
animCurve = new AnimationCurve(ks);
//---------- Create AnimationCurve End ----------