josemorval

LoadingUIElement

May 8th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. #To run this you need to attach to a gameobject with four children (in my case with a circle sprite). Adjust some scale and position in code...and voilà!
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class LoadingAnim : MonoBehaviour {
  7.  
  8.     public AnimationCurve animCurve;
  9.     public AnimationCurve animPointAlongRadius;
  10.     public AnimationCurve animTranslation;
  11.  
  12.     public float timeToRotate;
  13.     public float timeToTranslate;
  14.  
  15.     //Put the children in local pos (1,1,0), (-1,1,0), (-1,-1,0) and (1,-1,0)
  16.     Transform[] transformBalls;
  17.    
  18.     Vector3[] basePosBalls;
  19.  
  20.     void Start () {
  21.  
  22.         transformBalls = new Transform[transform.childCount];
  23.         basePosBalls = new Vector3[transform.childCount];
  24.  
  25.         for(int i=0;i<transformBalls.Length;i++){
  26.             transformBalls[i] = transform.GetChild(i);
  27.             basePosBalls[i] = transformBalls[i].localPosition.normalized;
  28.         }
  29.  
  30.         StartCoroutine("TranslationAnimBegin");
  31.         StartCoroutine("RotationAnim");
  32.     }
  33.  
  34.     IEnumerator TranslationAnimBegin(){
  35.  
  36.         yield return new WaitForSeconds(1f);
  37.  
  38.         Vector3 v;
  39.         Color c;
  40.         float time = 0;
  41.  
  42.         while(time<timeToTranslate){
  43.  
  44.             v = transform.position;
  45.             v.y = Mathf.Lerp(-3f,-1.5f,animTranslation.Evaluate(time/timeToTranslate));
  46.             transform.position = v;
  47.            
  48.             for(int i=0;i<transformBalls.Length;i++){
  49.                 c=transformBalls[i].GetComponent<SpriteRenderer>().color;
  50.                 c.a = Mathf.Lerp(0f,1f,time/timeToTranslate);
  51.                 transformBalls[i].GetComponent<SpriteRenderer>().color=c;
  52.             }
  53.  
  54.  
  55.             time+=Time.deltaTime;
  56.             yield return null;
  57.         }
  58.  
  59.  
  60.         for(int i=0;i<transformBalls.Length;i++){
  61.             c=transformBalls[i].GetComponent<SpriteRenderer>().color;
  62.             c.a = 1f;
  63.             transformBalls[i].GetComponent<SpriteRenderer>().color=c;
  64.         }
  65.  
  66.         yield return new WaitForSeconds(4f);
  67.  
  68.         StartCoroutine("TranslationAnimEnd");
  69.  
  70.     }
  71.  
  72.     IEnumerator TranslationAnimEnd(){
  73.  
  74.         Vector3 v;
  75.         Color c;
  76.         float time = 0;
  77.  
  78.         while(time<timeToTranslate){
  79.  
  80.             v = transform.position;
  81.             v.y = Mathf.Lerp(-1.5f,-3f,animTranslation.Evaluate(time/timeToTranslate));
  82.             transform.position = v;
  83.  
  84.             for(int i=0;i<transformBalls.Length;i++){
  85.                 c=transformBalls[i].GetComponent<SpriteRenderer>().color;
  86.                 c.a = Mathf.Lerp(1f,0f,time/timeToTranslate);
  87.                 transformBalls[i].GetComponent<SpriteRenderer>().color=c;
  88.             }
  89.  
  90.  
  91.             time+=Time.deltaTime;
  92.             yield return null;
  93.         }
  94.  
  95.  
  96.         for(int i=0;i<transformBalls.Length;i++){
  97.             c=transformBalls[i].GetComponent<SpriteRenderer>().color;
  98.             c.a = 0f;
  99.             transformBalls[i].GetComponent<SpriteRenderer>().color=c;
  100.         }
  101.  
  102.     }
  103.  
  104.  
  105.     IEnumerator RotationAnim(){
  106.  
  107.  
  108.         float time;
  109.         float rot = 0;
  110.  
  111.         while(true){
  112.  
  113.             Quaternion q1 = Quaternion.Euler(0f,0f,rot);
  114.             Quaternion q2 = Quaternion.Euler(0f,0f,rot+90f);
  115.  
  116.             time = 0f;
  117.  
  118.             while(time<timeToRotate){
  119.  
  120.                 Quaternion q = Quaternion.Lerp(q1,q2,animCurve.Evaluate(time/timeToRotate));
  121.                 transform.rotation = q;
  122.  
  123.                 for(int i=0;i<transformBalls.Length;i++){
  124.                     transformBalls[i].localPosition = Vector3.Lerp(0.2f*basePosBalls[i],0.15f*basePosBalls[i],animPointAlongRadius.Evaluate(time/timeToRotate));
  125.                 }
  126.  
  127.  
  128.                 time+=Time.deltaTime;
  129.                 yield return null;
  130.             }
  131.  
  132.             transform.rotation = q2;
  133.  
  134.             for(int i=0;i<transformBalls.Length;i++){
  135.                 transformBalls[i].localPosition = 0.2f*basePosBalls[i];
  136.             }
  137.  
  138.             rot += 90f;
  139.             rot%=360f;
  140.  
  141.         }
  142.  
  143.     }
  144.  
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment