PlayerManager.cs 4.51 KB
//Log Dir         http://docs.unity3d.com/Manual/LogFiles.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine.UI;


public class PlayerManager : MonoBehaviour {

	public InputField INFIELD;

	public static float hSliderValue = 1.1f;

	public static string alphabet = "0123456789,ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static String[] stringPos = {"_default"};//vetor que sera usado para quebrar a glosa

	// Guarda os nomes das palavras ja carregadas
	HashSet<string> loadedAssetBundles = new HashSet<string>();
	// Guarda os nomes das palavras que nao tem assetbundle
	HashSet<string> nonexistentAssetBundles = new HashSet<string>();


	// Contem todos os nomes das anims na ordem que serao reproduzidas
	public static Queue<string> playList = new Queue<string>();

	AnimationClip aniClip;
	public string glosa = " _default ";


	GameObject AVATAR;
	Animation COMPONENT_ANIMATION;

	//Primeiro metodo que o player executa
	void Start( ){

		Application.ExternalCall("onLoadPlayer");//var onLoadPlayer = function(){}

		AVATAR = GameObject.FindGameObjectWithTag("avatar");//referencia para o avatar
		COMPONENT_ANIMATION = AVATAR.GetComponent<Animation>();//referencia para o componente animador do avatar
		addAlpha( );//executa o metodo que adiciona alfabeto e numerais

	}

	void Update(){
		if(COMPONENT_ANIMATION.isPlaying)
			foreach(AnimationState anim in COMPONENT_ANIMATION)
				anim.speed = hSliderValue;
	}

	//define a velocidade das animacoes com base no slider da GUI
	public void setSlider( float x ){
		hSliderValue = x;

	}

	//adiciona alfabeto e numerais ao componente animador do avatar
	void addAlpha( ){



		foreach( char letter in alphabet ){

			aniClip = Resources.Load<AnimationClip> ("ANIMS/"+letter);

			if( aniClip ) COMPONENT_ANIMATION.AddClip(aniClip, ""+letter);
			else Debug.Log("Anim "+letter+" not found");

		}


	}//addAlpha

	public void stop_animations( ){

		COMPONENT_ANIMATION.Stop();
		aniClip = Resources.Load<AnimationClip>("ANIMS/_default");
		COMPONENT_ANIMATION.CrossFade("_default", 0.6F, PlayMode.StopAll );

	}

	public void catchGlosa(){

		this.glosa = " _default "+glosa+" _default ";

	}



	public void start_inputfield_web_play( ){	
		int version = 1;
		StartCoroutine(webPlay(INFIELD.text.ToUpper(), version));
	}

	public void start_web_play( ){

		catchGlosa();
		int version = 1;
		StartCoroutine(webPlay(INFIELD.text.ToUpper(), version));

	}

	IEnumerator webPlay(String glosa, int version) {



		stop_animations( );

		Subtitles.WORDS.Clear();
		foreach( char letter in alphabet ) Subtitles.WORDS.Add(""+letter);


		stringPos = glosa.Split(' ');
		String BaseURL = "http://150.165.205.9/anims/WEBGL/";

		//aniClip = www.assetBundle.mainAsset as AnimationClip;

		COMPONENT_ANIMATION.CrossFadeQueued("_default", 0.6F, QueueMode.CompleteOthers);

		foreach(string aniName in stringPos){

			try{ 

				if (String.IsNullOrEmpty(aniName)) continue;

			}catch(Exception e){ Debug.Log(e+" :: NotNullNotEmpty"); } 

			// Se ja estiver carregado: reproduz animaçao
			if (loadedAssetBundles.Contains(aniName)){

				Subtitles.WORDS.Add(aniName);
				COMPONENT_ANIMATION.CrossFadeQueued(aniName, 0.6F, QueueMode.CompleteOthers);

			}else if (nonexistentAssetBundles.Contains(aniName)){//se nao estiver, soletra
				foreach (char letter in aniName)
					COMPONENT_ANIMATION.CrossFadeQueued(""+letter, 0.6F, QueueMode.CompleteOthers);

			}else{

				WWW www = WWW.LoadFromCacheOrDownload(BaseURL+aniName, version);
				yield return www;

				if (www.error != null){//soletra se o ww inexistir
					//throw new Exception("Erro no WWW: " + www.error);

					Debug.Log("Sinal nao disponivel em banco de sinais. SOLETRANDO "+aniName);

					foreach (char letter in aniName)
						COMPONENT_ANIMATION.CrossFadeQueued(""+letter, 0.6F, QueueMode.CompleteOthers);

					nonexistentAssetBundles.Add(aniName);
					continue;

				}else{

					//Debug.Log (www.assetBundle.name);
					aniClip = www.assetBundle.mainAsset as AnimationClip;

					if (aniClip){

						COMPONENT_ANIMATION.AddClip(aniClip, aniName);
						COMPONENT_ANIMATION.CrossFadeQueued(aniName, 0.6F, QueueMode.CompleteOthers); // 0.4


						Subtitles.WORDS.Add(aniName); loadedAssetBundles.Add(aniName);

					}else{
						foreach (char letter in aniName)
							COMPONENT_ANIMATION.CrossFadeQueued("" + letter, 0.6F, QueueMode.CompleteOthers);

					}

				}

			}//else

		}//foreach

		COMPONENT_ANIMATION.CrossFadeQueued("_default", 0.6F, QueueMode.CompleteOthers);

	}

}