php

More Design Patterns

24 Nov 2010
Posted by xeophin

Writing Serialised Data to a String Instead of a File in C#

Instead of writing the serialised data to a file, which can be done using using (Stream s = File.Create("foo.xml")), you might want to have just the string – maybe because you want to send it to a server? You can use the StringWriter class to do so:

string data;
using (StringWriter stream = new StringWriter()) {
    xs.Serialize (stream, foo);
    data = stream.ToString();
}

Uploading Data to a Server in Unity 3D

Unfortunately, you can't directly upload data to server in Unity 3D. Instead, you have to write some sort of wrapper script that resides on the server.

Since you can create POST variables in Unity, you can mainly work with those in your script.

A very simple, preliminary implementation could look like this:

concierge.php

<?php 
//concierge.php resides on the server – and might not be written so nicely,
// my PHP skills are rusty.
 
if ($_POST['action'] == 'save') {
    // Needs to be rewritten to add folders when they don't exist 
    // – otherwise, fileputcontents will silently fail.
    file_put_contents('./'. $_POST['type'] . '/save.xml', stripslashes($_POST['data']));
 
} else if ($_POST['action'] == 'load') {
    $data = file_get_contents($_POST['type'].


Navigation



Languages


Syndicate

Syndicate content