Hi all,
I create one Thread where I perform some socket.receive operation. From this Thread I want call another class (Jump) for perform an animation on my Humanoid. When I call this class my Thread seems stop working? How can I resolve this problem? I show you my code:
Connect class (where I create Thread):
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Net;
public class Connect : MonoBehaviour {
Jump jump;
// Use this for initialization
void Start () {
Thread connectThread = new Thread (connection);
connectThread.Start ();
}
// Update is called once per frame
void Update () {
}
void connection(){
StringBuilder sb = new StringBuilder ();
byte[] buffer_controllo = new byte[1024];
byte[] buffer_controllo2 = new byte[1024];
byte[] buffer_dati = new byte[1024];
int count_bytes;
//Esegue connessione
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect ("1.2.3.4", 2000);
// socket.ReceiveTimeout = 10;
Debug.Log ("connesso");
string element;
string element2;
string data;
count_bytes = socket.Receive(buffer_controllo);
element = (System.Text.Encoding.ASCII.GetString(buffer_controllo));
element = element.Substring (0, count_bytes);
sb.Append (element);
int count_bytes2 = socket.Receive (buffer_controllo2);
element2 = System.Text.Encoding.ASCII.GetString (buffer_controllo2);
element2 = element2.Substring (0, count_bytes2);
sb.Append (element2);
Debug.Log (sb.ToString());
if (sb.ToString().Equals ("*HELLO*READY")) {
//Abilita trasmissione dati
Debug.Log("sto inviando");
byte[] start_byte = Encoding.ASCII.GetBytes ("s");
socket.Send (start_byte);
}
else {
return;
}
string dati;
int Pressure1;
int Pressure2;
int soglia = 120;
// socket.ReceiveTimeout = 10;
while ((count_bytes = socket.Receive(buffer_dati,0,19,SocketFlags.None))>0) {
dati = System.Text.Encoding.ASCII.GetString (buffer_dati);
Debug.Log(dati);
if(dati.Length>=19){
Pressure1 = int.Parse(dati.Substring(10, 3), System.Globalization.NumberStyles.HexNumber);
Pressure2 = int.Parse(dati.Substring(13, 3), System.Globalization.NumberStyles.HexNumber);
if(Pressure1>soglia && Pressure2>soglia){
Debug.Log("salto");
jump.performJump("a");
}
}
}
}
}
andthis is my jump class code:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
AndroidJavaClass jc;
// Use this for initialization
void Start () {
// AndroidJNIHelper.debug = true;
// jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
// AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity");
// AndroidJNI.AttachCurrentThread ();
// androidClass = new AndroidJavaClass("com.example.footm.Avatar");
}
// Update is called once per frame
void Update () {
}
public void performJump(string message){
//Void esecuzione salto
GameObject go2 = GameObject.Find ("myHumanoid");
Animator anim = go2.GetComponent<Animator> ();
anim.Play ("jump_1");
}
}