Skip to content

Instantly share code, notes, and snippets.

@rondagdag
Created February 5, 2016 21:25
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rondagdag/ee821452fad215a3c07c to your computer and use it in GitHub Desktop.
Here's what I used to receive UDP Packets from Photon
using UnityEngine;
using System.Collections;
using System;
using System.Security.Policy;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Text;
public class PhotonWS : MonoBehaviour {
// receiving Thread
Thread receiveThread;
UdpClient client;
int port = 33333;
public float Value;
public GameObject Light;
string lastReceivedUDPPacket;
// Use this for initialization
void Start () {
init ();
}
private List<float> initialSizes = new List<float>();
void Awake() {
// Save off all the initial scale values at start.
ParticleSystem[] particles = Light.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem particle in particles) {
initialSizes.Add(particle.startSize);
ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
if (renderer) {
initialSizes.Add(renderer.lengthScale);
initialSizes.Add(renderer.velocityScale);
}
}
}
public void Update() {
UpdateScale ();
}
public void UpdateScale() {
// Scale all the particle components based on parent.
int arrayIndex = 0;
ParticleSystem[] particles = Light.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem particle in particles) {
particle.startSize = initialSizes[arrayIndex++] * (value / 10);
ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
if (renderer) {
renderer.lengthScale = initialSizes[arrayIndex++] *
Light.transform.localScale.magnitude;
renderer.velocityScale = initialSizes[arrayIndex++] *
Light.transform.localScale.magnitude;
}
}
}
private void init()
{
// ----------------------------
// Abhören
// ----------------------------
// Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
// Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
private void ReceiveData()
{
client = new UdpClient(port);
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
string text = Encoding.UTF8.GetString(data);
//var n = text.Split('A');
//print(">> " + n[1]);
// latest UDPpacket
lastReceivedUDPPacket=text;
var rot = lastReceivedUDPPacket.Split(',');
Value = Convert.ToSingle (rot [6]);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment