Search Unity

mini Lib for communication with Android

Discussion in 'Android' started by tato469, Jul 26, 2012.

  1. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Finally I build a mini library for easy communication between Android and Unity, is a class that runs an UDP server in a thread and you can send strings from Android to this port. Is very simple but I think that can be usefull for people that are looking for it.

    I tested it with a subView following the tutorial: http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View and it works fine. I hope to update it for send and receive more type of classes.

    Note: It probably works for the communication with other platforms, while you communicate with UDP datagrams, but ever in localhost.

    Example of a call:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Net;
    6. using System.Net.Sockets;
    7. using System.Text;
    8. using System.Threading;
    9.  
    10.  
    11.  
    12. public class cameraScript : MonoBehaviour {
    13.  
    14.     CommunicationLib myServer;
    15.    
    16.     string messageClient;
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.        
    21.         myServer = new CommunicationLib();
    22.         myServer.start();
    23.        
    24.     }
    25.    
    26.         // Update is called once per frame
    27.     void Update () {
    28.         GameObject.Find("text_output").guiText.text = myServer.readMessage();
    29.     }
    30.  
    31.     void OnApplicationQuit(){
    32.     myServer.stop();
    33.     }
    34.    
    35.    
    36.    
    37.  
    38. }
    39.  
    40.  
     

    Attached Files:

    Last edited: Jul 26, 2012