【Unity】工具类

发表于2017-08-29
评论0 4k浏览

Unity作为游戏开发引擎,其中的开发工具有很多,是作为开发人员必须要掌握的,当然也有些人对Unity的工具类并不是很了解,为此,下面就给大家介绍介绍。


基本工具:

  1. using UnityEngine;  
  2.   
  3. public class XgControlTools : MonoBehaviour  
  4. {  
  5.   
  6.     private static iTween.EaseType posEaseType = iTween.EaseType.easeOutQuad;  
  7.     private static iTween.EaseType rotEaseType = iTween.EaseType.easeOutQuad;  
  8.   
  9.     public static void SetActive(GameObject[] gos, bool isActive)  
  10.     {  
  11.         for (int i = 0; i < gos.Length; i )  
  12.             SetActive(gos[i], isActive);  
  13.     }  
  14.   
  15.     public static void SetActive(GameObject[] gos, int index)  
  16.     {  
  17.         for (int i = 0; i < gos.Length; i )  
  18.             SetActive(gos[i], i == index);  
  19.     }  
  20.   
  21.     public static void SetActive(GameObject go, bool isActive)  
  22.     {  
  23.         if (go != null)  
  24.             go.SetActive(isActive);  
  25.     }  
  26.   
  27.     public static void PlayTween(GameObject go, bool forward)  
  28.     {  
  29.         UIPlayTween[] uPT = go.GetComponents();  
  30.         for (int i = 0; i < uPT.Length; i )  
  31.             uPT[i].Play(forward);  
  32.     }  
  33.   
  34.     public static void PlayTween(GameObject[] gos, int index)  
  35.     {  
  36.         for (int i = 0; i < gos.Length; i )  
  37.         {  
  38.             UIPlayTween[] uPT = gos[i].GetComponents();  
  39.             for (int j = 0; j < uPT.Length; j )  
  40.                 uPT[j].Play(index == i);  
  41.         }  
  42.     }  
  43.   
  44.     public static void PlayTween(GameObject[] gos, bool isActive)  
  45.     {  
  46.         for (int i = 0; i < gos.Length; i )  
  47.         {  
  48.             UIPlayTween[] uPT = gos[i].GetComponents();  
  49.             for (int j = 0; j < uPT.Length; j )  
  50.                 uPT[j].Play(isActive);  
  51.         }  
  52.     }  
  53.   
  54.     public static void AlphaTween(GameObject go, bool forward, EventDelegate ed)  
  55.     {  
  56.         TweenAlpha[] uPT = go.GetComponents();  
  57.         for (int i = 0; i < uPT.Length; i )  
  58.         {  
  59.             uPT[i].Play(forward);  
  60.             if (ed != null)  
  61.                 uPT[i].onFinished.Add(ed);  
  62.         }  
  63.     }  
  64.   
  65.     public static void ScaleTween(GameObject go, bool forward)  
  66.     {  
  67.         TweenScale[] uPT = go.GetComponents();  
  68.         for (int i = 0; i < uPT.Length; i )  
  69.         {  
  70.             uPT[i].Play(forward);  
  71.         }  
  72.     }  
  73.   
  74.     public static void PositionTween(GameObject go, bool forward)  
  75.     {  
  76.         TweenPosition[] uPT = go.GetComponents();  
  77.         for (int i = 0; i < uPT.Length; i )  
  78.             uPT[i].Play(forward);  
  79.     }  
  80.   
  81.     public static void Move(Transform cam, Vector3 pos, float time)  
  82.     {  
  83.         iTween.MoveTo(cam.gameObject, iTween.Hash("position", pos, "time", time, "easeType", posEaseType, "complete""OnComplete"));  
  84.     }  
  85.   
  86.     public static void Rotate(Transform cam, Vector3 rot, float time)  
  87.     {  
  88.         iTween.RotateTo(cam.gameObject, iTween.Hash("rotation", rot, "time", time, "easeType", rotEaseType));  
  89.     }  
  90.     public static void Scale(Transform cam, Vector3 rot, float time)  
  91.     {  
  92.         iTween.ScaleTo(cam.gameObject, iTween.Hash("scale", rot, "time", time, "easeType", rotEaseType));  
  93.     }  
  94. }  
背景图:

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class XgTextureSize : MonoBehaviour  
  5. {  
  6.     public GUITexture[] uiTextures;  
  7.   
  8.     void Awake()  
  9.     {  
  10.         if (uiTextures.Length > 0)  
  11.             for (int i = 0; i < uiTextures.Length; i )   
  12.                 //uiTextures [i].pixelInset = new Rect (new Vector2 (Screen.width / 2, Screen.height / 2), new Vector2 (1, 1));  
  13.                 uiTextures [i].pixelInset = new Rect (new Vector2 (0, 0), new Vector2 (Screen.width, Screen.height));     
  14.     }  
  15. }  
动画:

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class GTAnimation : MonoBehaviour  
  5. {  
  6.   
  7.   
  8.     ///   
  9.     /// Play default clip  
  10.     ///   
  11.     /// Animation.  
  12.     /// Clip.  
  13.     public static void PlayAnimation(Animation anim, AnimationClip clip)  
  14.     {  
  15.   
  16.         anim.clip = clip;  
  17.   
  18.         anim[clip.name].normalizedTime = 0.0f;  
  19.         anim[clip.name].speed = 1.0f;  
  20.   
  21.         anim.Play();  
  22.     }  
  23.   
  24.     public static void PlayAnimation(Animation anim, AnimationClip clip, bool expend, float speed = 1f, float progress = -1f)  
  25.     {  
  26.         if (progress != -1f)  
  27.             anim[clip.name].normalizedTime = progress;  
  28.         else  
  29.             anim[clip.name].normalizedTime = anim[clip.name].normalizedTime == 0f ? (expend ? 0f : 1f) : anim[clip.name].normalizedTime;  
  30.         anim[clip.name].speed = expend ? speed : -speed;  
  31.         anim.clip = clip;  
  32.         anim.Play();  
  33.     }  
  34.   
  35.     ///   
  36.     /// Plaies the animation.  
  37.     ///   
  38.     /// Animation.  
  39.     /// Clip.  
  40.     /// Speed with 100 faster.  
  41.     public static void PlayAnimation(float speed, Animation anim, AnimationClip clip)  
  42.     {  
  43.   
  44.         anim.clip = clip;  
  45.   
  46.         anim[clip.name].normalizedTime = 0.0f;  
  47.         anim[clip.name].speed = speed;  
  48.   
  49.         anim.Play();  
  50.     }  
  51.   
  52.   
  53.     public static void PlayAnimation(Animation anim, AnimationClip clip, float progress)  
  54.     {  
  55.   
  56.         anim.clip = clip;  
  57.   
  58.         anim[clip.name].normalizedTime = progress;  
  59.         anim[clip.name].speed = 1.0f;  
  60.   
  61.         anim.Play();  
  62.     }  
  63.   
  64.   
  65.   
  66.     ///   
  67.     /// animtion with pre-saved progress  
  68.     ///   
  69.     /// Animation.  
  70.     /// Clip.  
  71.     /// Progress.  
  72.     public static void ReverseAnimation(Animation anim, AnimationClip clip, float progress)  
  73.     {  
  74.   
  75.         anim.clip = clip;  
  76.   
  77.         anim[clip.name].normalizedTime = progress;  
  78.         anim[clip.name].speed = -1.0f;  
  79.   
  80.         anim.Play();  
  81.     }  
  82.   
  83.     ///   
  84.     /// pause default clip  
  85.     ///   
  86.     /// Animation.  
  87.     /// Clip.  
  88.     public static void PauseAnimation(Animation anim, AnimationClip clip)  
  89.     {  
  90.   
  91.         anim[clip.name].speed = 0.0f;  
  92.     }  
  93.   
  94.     ///   
  95.     /// pause default clip  
  96.     ///   
  97.     /// Animation.  
  98.     /// Clip.  
  99.     public static void ResumeAnimation(float speed, Animation anim, AnimationClip clip)  
  100.     {  
  101.   
  102.         anim[clip.name].speed = speed;  
  103.     }  
  104.   
  105.   
  106.     ///   
  107.     /// skip the animation to the last frame  
  108.     ///   
  109.     /// Animation.  
  110.     /// Clip.  
  111.     public static void SkipAnimtion(Animation anim, AnimationClip clip)  
  112.     {  
  113.   
  114.         anim.clip = clip;  
  115.   
  116.         anim[clip.name].normalizedTime = 1.0f;  
  117.         anim[clip.name].speed = 0.0f;  
  118.   
  119.         anim.CrossFade(clip.name);  
  120.         //anim.Sample ();  
  121.   
  122.     }  
  123.   
  124.   
  125.     ///   
  126.     /// Resets the animation.  
  127.     ///   
  128.     /// Animation.  
  129.     /// Clip.  
  130.     public static void ResetAnimation(Animation anim, AnimationClip clip)  
  131.     {  
  132.         switch (anim[clip.name].wrapMode)  
  133.         {  
  134.             case WrapMode.Loop:  
  135.   
  136.                 anim.clip = clip;  
  137.                 anim[clip.name].normalizedTime = 0.0f;  
  138.                 anim[clip.name].speed = 0.0f;  
  139.                 anim.CrossFade(clip.name);  
  140.   
  141.                 break;  
  142.             case WrapMode.Default:  
  143.                 anim.clip = clip;  
  144.                 anim[clip.name].normalizedTime = 0f;  
  145.                 anim[clip.name].speed = 0;  
  146.                 //anim.Play(clip.name);  
  147.                 anim.CrossFade(clip.name);  
  148.                 break;  
  149.             case WrapMode.Once:  
  150.                 anim.clip = clip;  
  151.                 anim[clip.name].normalizedTime = 0f;  
  152.                 anim[clip.name].speed = 0f;  
  153.   
  154.                 anim.CrossFade(clip.name);  
  155.                 //anim.Play(clip.name);  
  156.                 break;  
  157.         }  
  158.         //anim.Sample ();  
  159.     }  
  160.   
  161.   
  162.     ///   
  163.     /// get a progress of the entered clip on the anim obj  
  164.     ///   
  165.     /// The progress.  
  166.     public static float ClipProgress(Animation anim, AnimationClip clip)  
  167.     {  
  168.   
  169.         float p;  
  170.   
  171.         p = anim[clip.name].normalizedTime;  
  172.   
  173.         //Debug.Log("anim progress: "   p);  
  174.         return p;  
  175.     }  
  176.   
  177.   
  178.     ///   
  179.     /// Samples the animation.  
  180.     /// used for pausing animation or expend slider animation  
  181.     ///   
  182.     /// Animation.  
  183.     /// Clip.  
  184.     /// Progress.  
  185.     public static void SampleAnimation(Animation anim, AnimationClip clip, float progress)  
  186.     {  
  187.   
  188.         anim.clip = clip;  
  189.   
  190.         anim[clip.name].speed = 0.0f;  
  191.         anim[clip.name].normalizedTime = progress;  
  192.   
  193.         anim.Sample();  
  194.         anim.Play();  
  195.     }  
  196.     //因为项目需求所以speed 为2 默认:float speed = 1f  
  197.     public static float GetAnimmationClipLength(Animation anim, AnimationClip clip, float speed = 1f)  
  198.     {  
  199.         return anim[clip.name].length / speed;  
  200.   
  201.     }  
  202. }  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: