【Unity】工具类
2015-12-28 23:00
465 查看
基本工具:
using UnityEngine;
public class XgControlTools : MonoBehaviour
{
private static iTween.EaseType posEaseType = iTween.EaseType.easeOutQuad;
private static iTween.EaseType rotEaseType = iTween.EaseType.easeOutQuad;
public static void SetActive(GameObject[] gos, bool isActive)
{
for (int i = 0; i < gos.Length; i++)
SetActive(gos[i], isActive);
}
public static void SetActive(GameObject[] gos, int index)
{
for (int i = 0; i < gos.Length; i++)
SetActive(gos[i], i == index);
}
public static void SetActive(GameObject go, bool isActive)
{
if (go != null)
go.SetActive(isActive);
}
public static void PlayTween(GameObject go, bool forward)
{
UIPlayTween[] uPT = go.GetComponents<UIPlayTween>();
for (int i = 0; i < uPT.Length; i++)
uPT[i].Play(forward);
}
public static void PlayTween(GameObject[] gos, int index)
{
for (int i = 0; i < gos.Length; i++)
{
UIPlayTween[] uPT = gos[i].GetComponents<UIPlayTween>();
for (int j = 0; j < uPT.Length; j++)
uPT[j].Play(index == i);
}
}
public static void PlayTween(GameObject[] gos, bool isActive)
{
for (int i = 0; i < gos.Length; i++)
{
UIPlayTween[] uPT = gos[i].GetComponents<UIPlayTween>();
for (int j = 0; j < uPT.Length; j++)
uPT[j].Play(isActive);
}
}
public static void AlphaTween(GameObject go, bool forward, EventDelegate ed)
{
TweenAlpha[] uPT = go.GetComponents<TweenAlpha>();
for (int i = 0; i < uPT.Length; i++)
{
uPT[i].Play(forward);
if (ed != null)
uPT[i].onFinished.Add(ed);
}
}
public static void ScaleTween(GameObject go, bool forward)
{
TweenScale[] uPT = go.GetComponents<TweenScale>();
for (int i = 0; i < uPT.Length; i++)
{
uPT[i].Play(forward);
}
}
public static void PositionTween(GameObject go, bool forward)
{
TweenPosition[] uPT = go.GetComponents<TweenPosition>();
for (int i = 0; i < uPT.Length; i++)
uPT[i].Play(forward);
}
public static void Move(Transform cam, Vector3 pos, float time)
{
iTween.MoveTo(cam.gameObject, iTween.Hash("position", pos, "time", time, "easeType", posEaseType, "complete", "OnComplete"));
}
public static void Rotate(Transform cam, Vector3 rot, float time)
{
iTween.RotateTo(cam.gameObject, iTween.Hash("rotation", rot, "time", time, "easeType", rotEaseType));
}
public static void Scale(Transform cam, Vector3 rot, float time)
{
iTween.ScaleTo(cam.gameObject, iTween.Hash("scale", rot, "time", time, "easeType", rotEaseType));
}
}
背景图:
using UnityEngine;
using System.Collections;
public class XgTextureSize : MonoBehaviour
{
public GUITexture[] uiTextures;
void Awake()
{
if (uiTextures.Length > 0)
for (int i = 0; i < uiTextures.Length; i++)
//uiTextures [i].pixelInset = new Rect (new Vector2 (Screen.width / 2, Screen.height / 2), new Vector2 (1, 1));
uiTextures [i].pixelInset = new Rect (new Vector2 (0, 0), new Vector2 (Screen.width, Screen.height));
}
}
动画:
using UnityEngine;
using System.Collections;
public class GTAnimation : MonoBehaviour
{
/// <summary>
/// Play default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void PlayAnimation(Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = 1.0f;
anim.Play();
}
public static void PlayAnimation(Animation anim, AnimationClip clip, bool expend, float speed = 1f, float progress = -1f)
{
if (progress != -1f)
anim[clip.name].normalizedTime = progress;
else
anim[clip.name].normalizedTime = anim[clip.name].normalizedTime == 0f ? (expend ? 0f : 1f) : anim[clip.name].normalizedTime;
anim[clip.name].speed = expend ? speed : -speed;
anim.clip = clip;
anim.Play();
}
/// <summary>
/// Plaies the animation.
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="speed">Speed with 100 faster.</param>
public static void PlayAnimation(float speed, Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = speed;
anim.Play();
}
public static void PlayAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].normalizedTime = progress;
anim[clip.name].speed = 1.0f;
anim.Play();
}
/// <summary>
/// animtion with pre-saved progress
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="progress">Progress.</param>
public static void ReverseAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].normalizedTime = progress;
anim[clip.name].speed = -1.0f;
anim.Play();
}
/// <summary>
/// pause default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void PauseAnimation(Animation anim, AnimationClip clip)
{
anim[clip.name].speed = 0.0f;
}
/// <summary>
/// pause default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void ResumeAnimation(float speed, Animation anim, AnimationClip clip)
{
anim[clip.name].speed = speed;
}
/// <summary>
/// skip the animation to the last frame
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void SkipAnimtion(Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 1.0f;
anim[clip.name].speed = 0.0f;
anim.CrossFade(clip.name);
//anim.Sample ();
}
/// <summary>
/// Resets the animation.
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void ResetAnimation(Animation anim, AnimationClip clip)
{
switch (anim[clip.name].wrapMode)
{
case WrapMode.Loop:
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = 0.0f;
anim.CrossFade(clip.name);
break;
case WrapMode.Default:
anim.clip = clip;
anim[clip.name].normalizedTime = 0f;
anim[clip.name].speed = 0;
//anim.Play(clip.name);
anim.CrossFade(clip.name);
break;
case WrapMode.Once:
anim.clip = clip;
anim[clip.name].normalizedTime = 0f;
anim[clip.name].speed = 0f;
anim.CrossFade(clip.name);
//anim.Play(clip.name);
break;
}
//anim.Sample ();
}
/// <summary>
/// get a progress of the entered clip on the anim obj
/// </summary>
/// <returns>The progress.</returns>
public static float ClipProgress(Animation anim, AnimationClip clip)
{
float p;
p = anim[clip.name].normalizedTime;
//Debug.Log("anim progress: " + p);
return p;
}
/// <summary>
/// Samples the animation.
/// used for pausing animation or expend slider animation
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="progress">Progress.</param>
public static void SampleAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].speed = 0.0f;
anim[clip.name].normalizedTime = progress;
anim.Sample();
anim.Play();
}
//因为项目需求所以speed 为2 默认:float speed = 1f
public static float GetAnimmationClipLength(Animation anim, AnimationClip clip, float speed = 1f)
{
return anim[clip.name].length / speed;
}
}
using UnityEngine;
public class XgControlTools : MonoBehaviour
{
private static iTween.EaseType posEaseType = iTween.EaseType.easeOutQuad;
private static iTween.EaseType rotEaseType = iTween.EaseType.easeOutQuad;
public static void SetActive(GameObject[] gos, bool isActive)
{
for (int i = 0; i < gos.Length; i++)
SetActive(gos[i], isActive);
}
public static void SetActive(GameObject[] gos, int index)
{
for (int i = 0; i < gos.Length; i++)
SetActive(gos[i], i == index);
}
public static void SetActive(GameObject go, bool isActive)
{
if (go != null)
go.SetActive(isActive);
}
public static void PlayTween(GameObject go, bool forward)
{
UIPlayTween[] uPT = go.GetComponents<UIPlayTween>();
for (int i = 0; i < uPT.Length; i++)
uPT[i].Play(forward);
}
public static void PlayTween(GameObject[] gos, int index)
{
for (int i = 0; i < gos.Length; i++)
{
UIPlayTween[] uPT = gos[i].GetComponents<UIPlayTween>();
for (int j = 0; j < uPT.Length; j++)
uPT[j].Play(index == i);
}
}
public static void PlayTween(GameObject[] gos, bool isActive)
{
for (int i = 0; i < gos.Length; i++)
{
UIPlayTween[] uPT = gos[i].GetComponents<UIPlayTween>();
for (int j = 0; j < uPT.Length; j++)
uPT[j].Play(isActive);
}
}
public static void AlphaTween(GameObject go, bool forward, EventDelegate ed)
{
TweenAlpha[] uPT = go.GetComponents<TweenAlpha>();
for (int i = 0; i < uPT.Length; i++)
{
uPT[i].Play(forward);
if (ed != null)
uPT[i].onFinished.Add(ed);
}
}
public static void ScaleTween(GameObject go, bool forward)
{
TweenScale[] uPT = go.GetComponents<TweenScale>();
for (int i = 0; i < uPT.Length; i++)
{
uPT[i].Play(forward);
}
}
public static void PositionTween(GameObject go, bool forward)
{
TweenPosition[] uPT = go.GetComponents<TweenPosition>();
for (int i = 0; i < uPT.Length; i++)
uPT[i].Play(forward);
}
public static void Move(Transform cam, Vector3 pos, float time)
{
iTween.MoveTo(cam.gameObject, iTween.Hash("position", pos, "time", time, "easeType", posEaseType, "complete", "OnComplete"));
}
public static void Rotate(Transform cam, Vector3 rot, float time)
{
iTween.RotateTo(cam.gameObject, iTween.Hash("rotation", rot, "time", time, "easeType", rotEaseType));
}
public static void Scale(Transform cam, Vector3 rot, float time)
{
iTween.ScaleTo(cam.gameObject, iTween.Hash("scale", rot, "time", time, "easeType", rotEaseType));
}
}
背景图:
using UnityEngine;
using System.Collections;
public class XgTextureSize : MonoBehaviour
{
public GUITexture[] uiTextures;
void Awake()
{
if (uiTextures.Length > 0)
for (int i = 0; i < uiTextures.Length; i++)
//uiTextures [i].pixelInset = new Rect (new Vector2 (Screen.width / 2, Screen.height / 2), new Vector2 (1, 1));
uiTextures [i].pixelInset = new Rect (new Vector2 (0, 0), new Vector2 (Screen.width, Screen.height));
}
}
动画:
using UnityEngine;
using System.Collections;
public class GTAnimation : MonoBehaviour
{
/// <summary>
/// Play default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void PlayAnimation(Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = 1.0f;
anim.Play();
}
public static void PlayAnimation(Animation anim, AnimationClip clip, bool expend, float speed = 1f, float progress = -1f)
{
if (progress != -1f)
anim[clip.name].normalizedTime = progress;
else
anim[clip.name].normalizedTime = anim[clip.name].normalizedTime == 0f ? (expend ? 0f : 1f) : anim[clip.name].normalizedTime;
anim[clip.name].speed = expend ? speed : -speed;
anim.clip = clip;
anim.Play();
}
/// <summary>
/// Plaies the animation.
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="speed">Speed with 100 faster.</param>
public static void PlayAnimation(float speed, Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = speed;
anim.Play();
}
public static void PlayAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].normalizedTime = progress;
anim[clip.name].speed = 1.0f;
anim.Play();
}
/// <summary>
/// animtion with pre-saved progress
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="progress">Progress.</param>
public static void ReverseAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].normalizedTime = progress;
anim[clip.name].speed = -1.0f;
anim.Play();
}
/// <summary>
/// pause default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void PauseAnimation(Animation anim, AnimationClip clip)
{
anim[clip.name].speed = 0.0f;
}
/// <summary>
/// pause default clip
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void ResumeAnimation(float speed, Animation anim, AnimationClip clip)
{
anim[clip.name].speed = speed;
}
/// <summary>
/// skip the animation to the last frame
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void SkipAnimtion(Animation anim, AnimationClip clip)
{
anim.clip = clip;
anim[clip.name].normalizedTime = 1.0f;
anim[clip.name].speed = 0.0f;
anim.CrossFade(clip.name);
//anim.Sample ();
}
/// <summary>
/// Resets the animation.
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
public static void ResetAnimation(Animation anim, AnimationClip clip)
{
switch (anim[clip.name].wrapMode)
{
case WrapMode.Loop:
anim.clip = clip;
anim[clip.name].normalizedTime = 0.0f;
anim[clip.name].speed = 0.0f;
anim.CrossFade(clip.name);
break;
case WrapMode.Default:
anim.clip = clip;
anim[clip.name].normalizedTime = 0f;
anim[clip.name].speed = 0;
//anim.Play(clip.name);
anim.CrossFade(clip.name);
break;
case WrapMode.Once:
anim.clip = clip;
anim[clip.name].normalizedTime = 0f;
anim[clip.name].speed = 0f;
anim.CrossFade(clip.name);
//anim.Play(clip.name);
break;
}
//anim.Sample ();
}
/// <summary>
/// get a progress of the entered clip on the anim obj
/// </summary>
/// <returns>The progress.</returns>
public static float ClipProgress(Animation anim, AnimationClip clip)
{
float p;
p = anim[clip.name].normalizedTime;
//Debug.Log("anim progress: " + p);
return p;
}
/// <summary>
/// Samples the animation.
/// used for pausing animation or expend slider animation
/// </summary>
/// <param name="anim">Animation.</param>
/// <param name="clip">Clip.</param>
/// <param name="progress">Progress.</param>
public static void SampleAnimation(Animation anim, AnimationClip clip, float progress)
{
anim.clip = clip;
anim[clip.name].speed = 0.0f;
anim[clip.name].normalizedTime = progress;
anim.Sample();
anim.Play();
}
//因为项目需求所以speed 为2 默认:float speed = 1f
public static float GetAnimmationClipLength(Animation anim, AnimationClip clip, float speed = 1f)
{
return anim[clip.name].length / speed;
}
}
相关文章推荐
- unity 5.3 的 JSON Serialization
- unity 2D 透明遮罩 简单实现物体一半透明
- 【Unity 3D】学习笔记三十五:游戏实例——摄像机切换镜头
- 【Unity】游戏体销毁重新实例化GameObject后相同结构动画的衔接
- 一个示例性的Unity Shader例子
- Unity3D 获取主窗口句柄
- Unity3d ulua c#与lua交互+wrap文件理解
- Unity性能优化
- Unity各种路径
- Unity 的OCulus VR开发遇到的坑---OC版本差异
- Unity 的OCulus VR开发遇到的坑---OC版本差异
- Unity树木生成器
- Unity Shader 学习笔记(九) UV动画Shader实例
- 【Unity编辑器】面板中显示Private修饰的变量
- Unity学习过程(二)——创建第一个工程
- unity kinect2 怎么使用姿势或者使用你自己的How to Use Gestures or Create Your Own Ones
- 今天开始进驻CSDN博客,记录自己unity3d学习的每一步。
- Unity水效果
- Unity3D 自定义地形插件(支持FBX导入模型的编辑)
- Unity 自定义地形(可刷地形、材质和种树)(一)