您的位置:首页 > 移动开发 > Unity3D

【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;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: