x


Unity project and 3rd Party apps

Hi

I recently discovered Unity and it seems pretty cool. I was wondering if there was any chance to add functionalities to a unity project through a 3rd party application. For instance, should I have an hardware that produces data, or should I want to deal with an advanced database system within my game, is there an option to develop an asset (or I don t know what) in order to have my unity application interacting with the program or server I would have created myself (if yes, are there any computer language limitations? can I use Java for my own application for instance?)

Thanks

Manu

more ▼

asked Apr 18 '10 at 08:29 PM

Manu gravatar image

Manu
41 1 1 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

.NET Sockets are mostly just TCP/IP Sockets, nothing unusual. .NET also has a set of classes such as TCPClient and TCPListener (for building a server), that contain a Socket, and supply additional functionality - and are easier to use than straight Sockets.

I have started playing with networking, and written a TCP script, which I attached to a GUI script. The GUI script has a Text Box and a Send Button, which sends the contents of the Text box via the WriteSocket() function I wrote. In the GUI script, there is also a function that reads (once a second) the ReadSocket() function for incoming data. If it gets data, it then displays it in a second Text box.

The server part of my test software isn't .NET, I wrote a Python/Twisted-based server. Doesn't do much, basically echoes lines back. But, this is a working example of creating a TCP client in Unity

A list of the C# TCP code I wrote, in a file called 's_TCP.cs', is:

using UnityEngine;
using System.Collections;
using System;
using http://System.IO;
using System.Net.Sockets;

public class s_TCP : MonoBehaviour {
    internal Boolean socketReady = false;

    TcpClient mySocket;
    NetworkStream theStream;
    StreamWriter theWriter;
    StreamReader theReader;
    String Host = "localhost";
    Int32 Port = 5111;

    void Start () {
    }
    void Update () {
    }
// **********************************************
    public void setupSocket() {
        try {
            mySocket = new TcpClient(Host, Port);
            theStream = mySocket.GetStream();
            theWriter = new StreamWriter(theStream);
            theReader = new StreamReader(theStream);
            socketReady = true;
        }
        catch (Exception e) {
            Debug.Log("Socket error: " + e);
        }
    }
    public void writeSocket(string theLine) {
        if (!socketReady)
            return;
        String foo = theLine + "\r\n";
        theWriter.Write(foo);
        theWriter.Flush();
    }
    public String readSocket() {
        if (!socketReady)
            return "";
        if (theStream.DataAvailable)
            return theReader.ReadLine();
        return "";
    }
    public void closeSocket() {
        if (!socketReady)
            return;
        theWriter.Close();
        theReader.Close();
        mySocket.Close();
        socketReady = false;
    }
} // end class s_TCP

Hope that helps.

more ▼

answered Apr 19 '10 at 01:16 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

This is cool. Other posts I've seen say that this type of stuff won't really work unless you do a bunch of thread stuff. Does you code 'just work' or should it really involve threading?

Apr 01 '11 at 07:14 AM DaveA

@DaveA, yes, that code works as-is, no threading required or anything else (just attach it to an Object). Sockets are a built-in part of .Net/Mono, which is included with Unity.

Apr 01 '11 at 01:14 PM Cyclops

It's great. Thanks. But I'm having the compilation error on the line of using http://System.IO; in my C# source. It seems to me "//" was interpreted by the unity C# compiler as the beginning of comment. Can you please shed some light on that?

Jul 03 '12 at 03:06 PM FingerFighter
(comments are locked)
10|3000 characters needed characters left

You can use standard .net sockets to communicate with any other socket-based application. You also have access to most of the .net 2.0 API, and with unity pro, you can write c++ DLLs for your project.

So, there are many ways for your projects to interact with 3rd party applications. You can't use java directly with the Unity engine, although there's nothing stopping you from communicating with Java applications.

One common example of this is Unity clients communicating with Smartfox Server (a socket-based multiuser server application written in Java)

more ▼

answered Apr 18 '10 at 09:15 PM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

Great. that s what I was expecting :). Any chance to find a code example of .NET socket (I m not yet familiar with the .NET paradigm) working with a unity App, especially a C# example? Technically speaking, How will I contact external socket from a unity app... Don't think I have to do that from a script???

Thanks again for the quick help.

more ▼

answered Apr 19 '10 at 04:34 AM

Manu gravatar image

Manu
41 1 1 4

Please don't post comments as answers.

Apr 19 '10 at 04:49 AM Eric5h5

Follow the first link in my answer for lots of socket example code.

Apr 20 '10 at 09:46 AM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x60
x46
x31
x27

asked: Apr 18 '10 at 08:29 PM

Seen: 6055 times

Last Updated: Jul 03 '12 at 03:06 PM