【Unity】工具类

基本工具:

 

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;

    }
}

 


 

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity_阿黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值