Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Created January 29, 2015 13:15
Show Gist options
  • Save Buravo46/65e930085d42f68e6c7b to your computer and use it in GitHub Desktop.
Save Buravo46/65e930085d42f68e6c7b to your computer and use it in GitHub Desktop.
【Unity】スクリプトでAnimationClipの実装をする3種類のサンプルスクリプト
using UnityEngine;
using System.Collections;
/*===============================================================*/
/**
* EaseInOutによる実装サンプル
* 2015年1月28日 Buravo
*/
public class EaseInOutExample : MonoBehaviour
{
#region メンバ変数
/*===============================================================*/
/**
* @brief 開始時間
*/
private float m_start_time = 0.0f;
/**
* @brief 開始値
*/
private float m_start_value = 0.0f;
/**
* @brief 終了時間
*/
private float m_end_time = 5.0f;
/**
* @brief 終了値
*/
private float m_end_value = 10.0f;
/*===============================================================*/
#endregion
/*===============================================================*/
/**
* @brief 最初に一度だけ実行されるメソッド
*/
void Start ()
{
// AnimationClipの生成.
AnimationClip clip = new AnimationClip();
// AnimationCurveの生成.
AnimationCurve curve = AnimationCurve.EaseInOut(m_start_time, m_start_value, m_end_time, m_end_value);
// AnimationCurveの追加.
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
// AnimationClipの追加.
animation.AddClip(clip, "Move");
// AnimationClipの再生.
animation.Play("Move");
}
/*===============================================================*/
}
/*===============================================================*/
using UnityEngine;
using System.Collections;
/*===============================================================*/
/**
* Keyframeによる実装サンプル
* 2015年1月29日 Buravo
*/
public class KeyframeExample : MonoBehaviour
{
#region パブリック変数
/*===============================================================*/
/**
* @brief AnimationCurveの実装方法を切り替える値.
*/
public bool isSwitchCreation = false;
/*===============================================================*/
#endregion
#region メンバ変数
/*===============================================================*/
/**
* @brief 開始時間
*/
private float m_start_time = 0.0f;
/**
* @brief 開始値
*/
private float m_start_value = 0.0f;
/**
* @brief 終了時間
*/
private float m_end_time = 5.0f;
/**
* @brief 終了値
*/
private float m_end_value = 10.0f;
/*===============================================================*/
#endregion
/*===============================================================*/
/**
* @brief 最初に一度だけ実行されるメソッド
*/
void Start ()
{
// もしもアニメーションコンポーネントがなければ追加する.
if (!animation)
{
gameObject.AddComponent<Animation>();
}
// AnimationClipの生成.
AnimationClip clip = new AnimationClip();
// AnimationCurveの宣言.
AnimationCurve curve;
// Keyframeの宣言.
Keyframe startKeyframe;
Keyframe endKeyframe;
// 実装手法の切り替え.
if (isSwitchCreation)
{
// Keyframeの生成.
startKeyframe = new Keyframe(m_start_time, m_start_value);
endKeyframe = new Keyframe(m_end_time, m_end_value);
// AnimationCurveの生成.
curve = new AnimationCurve(startKeyframe, endKeyframe);
}
else
{
// AnimationCurveの生成.
curve = new AnimationCurve();
// Keyframeの生成.
startKeyframe = new Keyframe(m_start_time, m_start_value);
endKeyframe = new Keyframe(m_end_time, m_end_value);
// Keyframeの追加.
curve.AddKey(startKeyframe);
curve.AddKey(endKeyframe);
}
// AnimationCurveの追加.
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
// AnimationClipの追加.
animation.AddClip(clip, "Move");
// AnimationClipの再生.
animation.Play("Move");
}
/*===============================================================*/
}
/*===============================================================*/
using UnityEngine;
using System.Collections;
/*===============================================================*/
/**
* Linearによる実装サンプル
* 2015年1月28日 Buravo
*/
public class LinearExample : MonoBehaviour
{
#region メンバ変数
/*===============================================================*/
/**
* @brief 開始時間
*/
private float m_start_time = 0.0f;
/**
* @brief 開始値
*/
private float m_start_value = 0.0f;
/**
* @brief 終了時間
*/
private float m_end_time = 5.0f;
/**
* @brief 終了値
*/
private float m_end_value = 10.0f;
/*===============================================================*/
#endregion
/*===============================================================*/
/**
* @brief 最初に一度だけ実行されるメソッド
*/
void Start ()
{
// AnimationClipの生成.
AnimationClip clip = new AnimationClip();
// AnimationCurveの生成.
AnimationCurve curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
// AnimationCurveの追加.
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
// AnimationClipの追加.
animation.AddClip(clip, "Move");
// AnimationClipの再生.
animation.Play("Move");
}
/*===============================================================*/
}
/*===============================================================*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment