Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Driving Unity From Other App

Discussion in 'Multiplayer' started by Jehsup, May 22, 2008.

  1. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Hello,

    I'm kicking tires here for an upcoming project with a very short turn around and Unity has peaked my interest. A lot of my questions I have been able to answer myself, but for one particular requirement I need some guidance on how I would go about "driving" Unity from an external application over a network, and have the communication be two way (IP\port). I need to be able to add and remove entities, manipulate camera position\focus\speed, attach camera to paths, control object animation states, etc.

    Is this feasible? Have any of you done anything similar? If so any help you could offer, or a poke in the right direction (links, docs) would be greatly appreciated.
    --J
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You have access to the .net library from scripts in unity so you could easily set up some socket networking. Also spawning and deleting objects, manipulating properties in response to incoming packets should be fairly simple.

    Sockets in C# .net: http://channel9.msdn.com/ShowPost.aspx?PostID=224635
     
  3. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Nice. Thanks for reply. That is exactly what I needed to hear. I appreciate your help.

    -J
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
  5. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Hello again.

    Using bits and peices of information and resources submitted by community members,etc. I've attempted to create a very simple C# Server app running on one box (works fine).

    I've created a C# Client DLL as described and placed it in my Assets\Plugins directory. I created a C# script in unity that I attached to my main camera.

    I'm basically going for the simple functionality of the "Simple TCP/IP Server Client" example on wiki. When I add my C# script to unity, I get the following error.... type or namespace name SocketConnections could not be found.

    Here is unity script attached to my camera
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Runtime.InteropServices;
    5. using SocketConnections;
    6. using System.Security.Permissions;
    7.  
    8. public class SocketConnector : MonoBehaviour
    9. {
    10.     public SocketClient test;
    11.    
    12.     void Start ()
    13.     {
    14.         // create our client
    15.         test = new SocketClient();
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         if (Input.GetKeyDown ("space"))
    21.         {
    22.             Debug.Log ("Attempting To Open Socket");
    23.             test.ConnectClientSocket();
    24.         }
    25.        
    26.         if (Input.GetKeyDown ("escape"))
    27.         {
    28.             Debug.Log ("Attempting to Close Socket");
    29.             test.CloseClientSocket();
    30.         }
    31.        
    32.     }
    33.    
    34.     void OnApplicationQuit ()
    35.     {
    36.         try{test.CloseClientSocket();}
    37.         catch{}
    38.     }
    39. }
    40.  
    Here is the simple client DLL code.
    Code (csharp):
    1.  
    2. using System;
    3. using System.Windows.Forms;
    4. using System.Net;
    5. using System.Net.Sockets;
    6.  
    7.  
    8. namespace SocketConnections
    9. {
    10.     public class SocketClient
    11.     {
    12.    
    13.         byte[] m_dataBuffer = new byte [10];
    14.         IAsyncResult m_result;
    15.         public AsyncCallback m_pfnCallBack ;
    16.         public Socket m_clientSocket;
    17.        
    18.         public SocketClient()
    19.         {
    20.             // Create our client socket instance
    21.         }
    22.        
    23.         public void CloseClientSocket()
    24.         {
    25.             if ( m_clientSocket != null )
    26.             {
    27.                 m_clientSocket.Close ();
    28.                 m_clientSocket = null;
    29.             }      
    30.            
    31.         }
    32.  
    33.         public void ConnectClientSocket()
    34.         {
    35.             // create our new socket
    36.             m_clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
    37.  
    38.             // Set the IPAdress
    39.             IPAddress ip = IPAddress.Parse ("192.168.1.218");
    40.            
    41.             int iPortNo = 8000;// hard coded for now
    42.  
    43.             // Create the end point
    44.             IPEndPoint ipEnd = new IPEndPoint (ip,iPortNo);
    45.             // Connect to the remote host
    46.             m_clientSocket.Connect ( ipEnd );
    47.             if(m_clientSocket.Connected)
    48.             {
    49.                    
    50.                 //Wait for data asynchronously
    51.                 WaitForData();
    52.             }
    53.         }
    54.  
    55.         public void WaitForData()
    56.         {
    57.             try
    58.             {
    59.                 if  ( m_pfnCallBack == null )
    60.                 {
    61.                     m_pfnCallBack = new AsyncCallback (OnDataReceived);
    62.                 }
    63.                 SocketPacket theSocPkt = new SocketPacket ();
    64.                 theSocPkt.thisSocket = m_clientSocket;
    65.                 // Start listening to the data asynchronously
    66.                 m_result = m_clientSocket.BeginReceive (theSocPkt.dataBuffer,
    67.                                                         0, theSocPkt.dataBuffer.Length,
    68.                                                         SocketFlags.None,
    69.                                                         m_pfnCallBack,
    70.                                                         theSocPkt);
    71.             }
    72.             catch(SocketException se)
    73.             {
    74.                 MessageBox.Show (se.Message );
    75.             }
    76.  
    77.         }
    78.         public class SocketPacket
    79.         {
    80.             public System.Net.Sockets.Socket thisSocket;
    81.             public byte[] dataBuffer = new byte[1024];
    82.         }
    83.  
    84.         public  void OnDataReceived(IAsyncResult asyn)
    85.         {
    86.             try
    87.             {
    88.                 SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
    89.                 int iRx  = theSockId.thisSocket.EndReceive (asyn);
    90.                 char[] chars = new char[iRx +  1];
    91.                 System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    92.                 int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
    93.                 System.String szData = new System.String(chars);
    94.                 //richTextRxMessage.Text = richTextRxMessage.Text + szData;
    95.                 WaitForData();
    96.             }
    97.             catch (ObjectDisposedException )
    98.             {
    99.                 System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
    100.             }
    101.             catch(SocketException se)
    102.             {
    103.                 MessageBox.Show (se.Message );
    104.             }
    105.         }  
    106.  
    107.         }
    108. }
    109.  
    I know I am doing something really stupid, or omitting something, but I've looked at it so long that I can't see it. Anyone else see what the heck is going on?

    I compiled my DLL on windows box, not sure if that would make any difference or not.
     
  6. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I don't think you need the using SocketConnectons line at all. I think for DLLs Unity automatically imports the namespace.
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    C# libraries are different. They need to be in your assets along with your scripts and everything else. And yes you do need to be using the namespace in the scripts in which you're gonna access its contents. (Unless ofcourse you're using MyNameSpace.MyClass but thats usually just a pain if you ask me).
     
  8. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Bummer, so nothing obviously wrong then? I have everything where the very poor documentation on the subject says it should be and it just doesnt work. I would think that this attractive "feature" of the engine would deserve a much better run at documentation and/or example code.
     
  9. Jehsup

    Jehsup

    Joined:
    May 22, 2008
    Posts:
    166
    Got it working. Made new project for the 3rd time and "whammo magico" it works.