- Home /
Question by
Nimuin · Jun 04, 2015 at 01:22 PM ·
threadingudpmain thread
SetBlendShapeWeight can only be called from main thread
I'm currently struggling with controlling the blendshapes in my script.
Some background: I've written an external application to go from text to phonemes. These phonemes will be used to drive the mouthshapes of my character in Unity.
My current problem is that I'm running my UPDRead() method in a seperate thread (from which I am trying to call SetBlendShapeWeight). However, as the title states, SetBlendShapeWeight can only be called from the main thread. When I try to parse the data from UDPRead() to the main thread (update()), it only reads the final two bytes in the string.
My question is how can I fix this? Do I need multithreading for this? Or is there some way to use SetBlendShapeWeight from this seperate thread?
Below is my code:
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
public class UDPClient : MonoBehaviour {
public string a;
public int port = 11000;
public int port2 = 6100;
private UdpClient client;
private UdpClient client2;
private IPEndPoint remoteIPEndPoint;
public static byte []receive_byte_array;
public static string b;
public byte []receive_time_array;
private Regex regexParse;
private Thread t_udp;
public static bool receive = false;
public SkinnedMeshRenderer skinnedMeshRenderer;
public Mesh skinnedMesh;
void awake(){
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
skinnedMesh = GetComponent<SkinnedMeshRenderer> ().sharedMesh;
}
// Use this for initialization
void Start () {
client = new UdpClient (port);
client2= new UdpClient(port2);
remoteIPEndPoint = new IPEndPoint (IPAddress.Any, 0);
regexParse = new Regex (@"\d*$");
t_udp = new Thread (new ThreadStart (UDPRead));
t_udp.Name = "Mindtuner UDP thread";
t_udp.Start ();
}
public void UDPRead(){
while (true) {
try {
receive = true;
receive_byte_array = client.Receive(ref remoteIPEndPoint);
receive_time_array = client2.Receive(ref remoteIPEndPoint);
string returnData = Encoding.ASCII.GetString(receive_time_array);
double time = Convert.ToDouble(returnData);
b = MouthShapes(receive_byte_array[0]);
//Debug.Log(b + " " + returnData);
}
catch (Exception e) {
Debug.Log ("Not so good " + e.ToString ());
}
Thread.Sleep (20);
}
}
// Update is called once per frame
void Update ()
{
Debug.Log (b);
}
private string MouthShapes(byte letter)
{
switch(letter)
{
case 10:
//Debug.Log("aa");
a ="aa";
skinnedMeshRenderer.SetBlendShapeWeight(5,70);
break;
}
}
}
Comment