纳金网

标题: 开发技巧-携程方法库 [打印本页]

作者: may    时间: 2018-8-23 21:14
标题: 开发技巧-携程方法库

来自:UnityTerminator
  1. using UnityEngine;
  2. using System.Collections;

  3. public static class FunctionLibrary : object
  4. {
  5.     public delegate void myDelegate();
  6.     public delegate void myDelegateInt(int i);

  7.     //Moves a menu element by the received ammount in time
  8.     //通过接收到的文件来移动菜单元素
  9.     public static IEnumerator MoveElementBy(Transform element, Vector2 ammount, float time)
  10.     {
  11.         float i = 0.0f;
  12.         float rate = 1.0f / time;

  13.         Vector2 startPos = element.position;
  14.         Vector2 endPos = element.position;
  15.         endPos += ammount;

  16.         while (i < 1.0)
  17.         {
  18.             i += Time.deltaTime * rate;
  19.             element.localPosition = Vector3.Lerp(startPos, endPos, i);

  20.             yield return 0;
  21.         }
  22.     }
  23.     //Rescales the given element to the given scale in time
  24.     //将给定的元素重新调整到给定的时间尺度
  25.     public static IEnumerator ScaleTo(Transform element, Vector2 endScale, float time)
  26.     {
  27.         float i = 0.0f;
  28.         float rate = 1.0f / time;

  29.         Vector2 startScale = element.localScale;

  30.         while (i < 1.0)
  31.         {
  32.             i += Time.deltaTime * rate;
  33.             element.localScale = Vector3.Lerp(startScale, endScale, i);

  34.             yield return 0;
  35.         }
  36.     }
  37.     //Sets the active state of the go to state, after time
  38.     //将状态的活动状态设置为状态
  39.     public static IEnumerator ChangeEnabledState(GameObject go, bool state, float time)
  40.     {
  41.         float i = 0.0f;
  42.         float rate = 1.0f / time;

  43.         while (i < 1.0)
  44.         {
  45.             i += Time.deltaTime * rate;
  46.             yield return 0;
  47.         }

  48.         go.SetActive(state);
  49.     }
  50.     //Calls the passed void function with no arguments after delay
  51.     //调用传递的void函数,在延迟之后没有参数
  52.     public static IEnumerator CallWithDelay(myDelegate del, float delay)
  53.     {
  54.         yield return new WaitForSeconds(delay);
  55.         del();
  56.     }
  57.     //Calls the passed void function with  arguments after delay
  58.     //在延迟之后调用传递的void函数
  59.     public static IEnumerator CallWithDelay(myDelegateInt del, int num, float delay)
  60.     {
  61.         yield return new WaitForSeconds(delay);
  62.         del(num);
  63.     }
  64.     //Fade overlay opacity
  65.     //褪色覆盖不透明度
  66.     public static IEnumerator FadeScreen(SpriteRenderer overlay, float time, float to)
  67.     {
  68.         //Set the screen fade's color to end in time
  69.         float i = 0.0f;
  70.         float rate = 1.0f / time;

  71.         Color start = overlay.color;
  72.         Color end = new Color(start.r, start.g, start.b, to);

  73.         while (i < 1.0)
  74.         {
  75.             i += Time.deltaTime * rate;
  76.             overlay.color = Color.Lerp(start, end, i);
  77.             yield return 0;
  78.         }
  79.     }
  80. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5