PlayerManager.cs 2.6 KB
//Log Dir         http://docs.unity3d.com/Manual/LogFiles.html

// requisiçao http get no unity
// vlibras.lavid.ufpb.br:5000/glosa?texto=CASA%LAVID%123	
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using System.Threading;

public class PlayerManager : GenericPlayerManager {

	private const string BASE_URL = "http://150.165.205.9/anims/ANDROID/";
	private const string SERVER_URL = "http://vlibras.lavid.ufpb.br:5000/glosa?texto=";

	public GameObject loading_snippet;

	//private const string BASE_URL = "http://150.165.205.9/anims/AssetBundles/0_DefaultBundles/";
	public InputField INFIELD;
	private int version = 1;

	public void catchGlosa(String glosa)
	{
		base.glosa = glosa;
		start_web_play();
	}

	public void start_web_play()
	{
		try {
			base.play();
		} catch(Exception e) {
			e.ToString();//Debug.Log(e+" :: Ops!");
		}
	}

	protected override WWW loadAssetBundle(string aniName)
	{
		return WWW.LoadFromCacheOrDownload(BASE_URL + aniName, version);
	}

	protected override void onPlayingStateChange() {}

	IEnumerator translate()
	{
		WWW glosaRequest = new WWW(SERVER_URL + WWW.EscapeURL(base.glosa));

		try {
			waitForRequest(glosaRequest);
		} catch (Exception e) { e.ToString(); }

		if (glosaRequest != null)
		{
			loading_snippet.SetActive(true);
			yield return glosaRequest;
			loading_snippet.SetActive(false);

			if ( ! String.IsNullOrEmpty(glosaRequest.text))
			{
				base.glosa = glosaRequest.text;
				Debug.Log("Server answer: " + glosa);
			}
			else
			{
				base.glosa = base.glosa.ToUpper();
				Debug.Log("No answer: " + glosa);
			}
		}
		else Debug.Log ("eita");

		base.play();
	}

	public void start_inputfield_web_play()
	{
		try {
			base.glosa = INFIELD.text;
			StartCoroutine("translate");
		}
		catch (Exception e) {
			Debug.Log (e+" :: Exception StartCourr");
		}
	}

	protected string getGlosaFromServer(string str)
	{
		string strToServer = "";
		
		foreach (char c in str)
			if (c.Equals(' '))
				strToServer += "%20";
			else
				strToServer += c;
		
		Debug.Log("str to server -> " + strToServer);

		string urlWithText = SERVER_URL + strToServer;
		
		WWW www = new WWW(urlWithText);
		StartCoroutine(waitForRequest(www));

		// while(!www.isDone) Thread.Sleep(250);

		Debug.Log("returned from server -> " + www.text);
		return www.text;
	}

	protected IEnumerator waitForRequest(WWW www)
	{
		yield return www.isDone;

		// check for errors
		if (www.error == null)
			Debug.Log("WWW Ok!: " + www.text);
		else
			Debug.Log("WWW Error: "+ www.error);
	}
}