【Unity】工具类
发表于2017-08-29
Unity作为游戏开发引擎,其中的开发工具有很多,是作为开发人员必须要掌握的,当然也有些人对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
(); - 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
(); - 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
(); - 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
(); - 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
(); - for (int i = 0; i < uPT.Length; i )
- {
- uPT[i].Play(forward);
- }
- }
- public static void PositionTween(GameObject go, bool forward)
- {
- TweenPosition[] uPT = go.GetComponents
(); - 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
- {
- ///
- /// Play default clip
- ///
- /// Animation.
- /// Clip.
- 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();
- }
- ///
- /// Plaies the animation.
- ///
- /// Animation.
- /// Clip.
- /// Speed with 100 faster.
- 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();
- }
- ///
- /// animtion with pre-saved progress
- ///
- /// Animation.
- /// Clip.
- /// Progress.
- 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();
- }
- ///
- /// pause default clip
- ///
- /// Animation.
- /// Clip.
- public static void PauseAnimation(Animation anim, AnimationClip clip)
- {
- anim[clip.name].speed = 0.0f;
- }
- ///
- /// pause default clip
- ///
- /// Animation.
- /// Clip.
- public static void ResumeAnimation(float speed, Animation anim, AnimationClip clip)
- {
- anim[clip.name].speed = speed;
- }
- ///
- /// skip the animation to the last frame
- ///
- /// Animation.
- /// Clip.
- 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 ();
- }
- ///
- /// Resets the animation.
- ///
- /// Animation.
- /// Clip.
- 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 ();
- }
- ///
- /// get a progress of the entered clip on the anim obj
- ///
- ///
The progress. - public static float ClipProgress(Animation anim, AnimationClip clip)
- {
- float p;
- p = anim[clip.name].normalizedTime;
- //Debug.Log("anim progress: " p);
- return p;
- }
- ///
- /// Samples the animation.
- /// used for pausing animation or expend slider animation
- ///
- /// Animation.
- /// Clip.
- /// Progress.
- 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;
- }
- }