drupal


Browser to unity3d communication and back again

I have been doing a fair bit of unity3d recently again. In particular I have been looking at the ways it can take data and parameters. It’s all in the unity documentation but there were a few subtle things I missed first time around that I thought it worth sharing, as much for my memory as anything.
The first useful thing is the unity plugin is able to simply talk back to the browser and call javascript functions in the page. So in C# for example I am able to do this.

void Awake ()
{
Application.ExternalCall( "callbackunity", "The game says hello!" );
}

Where the page that the unity3d is served from has

<script type=”text/javascript”>
<!–
function callbackunity(arg)
{
alert(arg);
}
–>


Obviously you can do more than just an alert so I looked at what I can send back into unity3d from the page and started to do this in the calling page.

<script type=”text/javascript”>
<!–
function callbackunity(arg)
{
alert(arg);
var unity = unityObject.getObjectById(“unityPlayer”);
unity.SendMessage(“Rezzer”, “inboundfunction”, “item1~item2~item3”);
}
–>
</script>

This gets the unity plugin module and calls a function called inboundfunction that is attached to a unity game object called Rezzer. It is only able to pass a single value so I send a string with a known seperator ~ to be able to send more than one piece of data.
So the flow is that unity loads, awakes and then makes a callback to the page which then injects the data into unity.
On the unity side I have this sort of function

void inboundfunction(string indata)
{

string[] words = indata.Split('~');

data1 = words[0];
moredata2 = words[1];
anotherpiece3 = words[2];

}

At first this all did not work (of course). I thought I had not put the right script on the right object as I was getting a callback to the page but not injecting the data into the unity object.
This turned out to be something quite simple in the end. The unity docs example shows var unity = unityObject.getObjectById(“UnityContent”); However the page I got generated out of unity3d that I used to then add my code to used a different label for the Unity plugin in the setup. It called it “unityPlayer”. So my failing code was because the js in the webpage was not picking up the right object. As we know computers need to have everything exact.
This was almost code blindness. I was thinking it was getting the unity object, of course it was how could it do anything other, but its an obvious school boy error “UnityContent” <> “unityPlayer” 🙂
Once that little bug was sorted out it was all plain sailing. The parameters I pass as item 1,2 3 etc are generated by PHP from Drupal that embeds the unity3d. So I can send anything to the unity based on which page is being served and by whom.
One of the other things I do though is use direct WWW communication to a server from inside the unity3d. This initial set up code is to establish some start parameters, once running communication is not via the browser, but a hotline to server instead.
That all just works as documented, though you have to make sure you are serving from and talking to the same server or dealing with the cross domain policy documents that help protect web apps from rogue applications in browsers.
This is all very basic code really, but if you are not from a web world it can seem a little unusual.
e.g. in .cs in unity3d

IEnumerator sendIt ()
{
// Create a form object
WWWForm form = new WWWForm ();
// set up fields
form.AddField ("data1", "some data");
form.AddField ("userscore", score );

// Create a download object
WWW download = new WWW ("http://someurltohandlethings", form);

// Wait until the download is done
yield return download;

if (download.error != null) {
Debug.Log (download.error);
print ("Error downloading: " + download.error);

} else {
print ("Sent");

}

}

Due to .cs needing to multi thread this is an enumerated function which means you have to call it like this when you want to send anything

StartCoroutine (sendIt ());

As most of my quick unity3d pieces had been in .js this StartCoroutine was not as obvious, though it is in the Unity docs.
The URL I call has some PHP that gets at the form elements, but we are in normal web territory then.

$somedata = $_POST["data1"];
$somescore = $_POST["userscore"];

Of course all the error handling and data handling can (and is) much more elegant but this all seems to work very nicely and the core of the communication I am able to drop into anywhere.

Open source software really has come of age

The past few weeks I have had my head in an installation of Drupal 6.16 the content management system that sits very nicely on the LAMP stack. So I got to spark up another Ubuntu linux server off in a cloud somewhere,using the great apt packager on ubuntu/linux to go and fetch the extras I needed AND their dependencies. All the extra instructions I needed were on slicehosts forums for various pieces of config. So a full OS, all the extras I needed without having to trawl trough patches and dependency trees manually. In the good old days the stuff was pretty disorganised. If you came to it all fresh, or had been away for a while the “obviously you would have x, y or z” would be quite a pain.

The whole apache, mysql, php installation and config is also very straightforward. Yes you still need to know a few of the more arcane system command lines, or be able to look them up and editing even with nano or vi on a terminal is still rather annoying (terminals being a hangover from before we had more complex machines on the front end of servers). However, doing what would be the simplest task in a drag and drop world in a command line typing all the paths correctly etc makes you feel you are in charge of the whole thing. Which is why sysops are usually quite stroppy 🙂
Drupal’s install goes like a dream to. Again a whole set of extra modules are contributed to the let you do other things with your content, thinks like the CCK (content creation kit) which layers some new fields to be able to add to the page creation forms you make.
Back in 1998 I wrote content management systems, mainly in Lotus Domino, I know the problems and also the sort of things you need to do in the systems. So for me Drupal was great, once you get used to the naming convention of the template overrides and the ability to use views (SQL selects) on the data its all pretty slick. It is of course a long while since 2008, and it is interesting that whilst there were lots of commercial CMS packages that attempted to emerge none of them seem a patch on Drupal and even on WordPress. That is for most things, most web applications it is a pretty good fit.
Of course with any themeable template based system with a multitude of user contributed modules and gadgets there are going to be times when things just are not where you need them to be for your particular layout or information design. I spent a fair bit of time with one piece of data and layout trying to do it “properly” in the end I just changed the module, which let me put the class id’s in that I needed to make more sense of the display for the CSS. Not ideal but the point was it was there to do.
Commercial software has all to often been put in place to keep you away from the engine. Opensource be it opensim, drupal, wordpress, linux, freeswitch etc really do let you be the mechanic on the engine if you want, but you clearly don’t have to as the slickness of the design ethics in these applications through crowdsourced cooperation is quite stunning.
To do the full thing from commissioning a server to creating data structures in Drupal and then adjusting templates and style sheets is still a great swathe of skills needed, but when you have been in this for so long you know the patterns and roughly how things need to work. The ability to look up and search for problems, similar situations and generally fix on the fly though really helps to and can’t be understated. Much better than routing through a cupboard of manuals as we had to back in 1990!
Gotta love the web