Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity > Php > MySQL > Xml > Unity ?

Discussion in 'Scripting' started by craigerz, Jan 12, 2010.

  1. craigerz

    craigerz

    Joined:
    Mar 30, 2009
    Posts:
    19
    Someone please tell me this is serious overkill. All I am trying to do is pull mySQL data into Unity. Not a single value, but rather multiple named values, commonly referred to as associative arrays.

    This is possible in Flash, by using Php as a middleman, Flash will recognize variable names:values similar to a URL quesrystring as if using the $_GET[] method. Simply echoing the format "var1=value+var2=value" in a Php page will import into Flash as two distinct named variables for use in AS.

    I would have thought Unity's WWW class (www.data) would be able to parse variables such as this, but from scouring the Unity forums it seems it can only dump the entire returned echo - useful as only a single string in Unity or variable. To return multiple variables, I would have to do multiple Php calls.

    I can appreciate this topic has been talked to death, but the only solution I can find is in this thread http://forum.unity3d.com/viewtopic.php?t=26903 which creates associative arrays using Xml. While this DOES work, I am to understand that I must go Unity to Php to MySQL to output Xml to only then have Unity import the Xml file. This is like going all the way around the world just to get next door.

    Is there not a direct Unity-to-MySQL solution, or per chance a WWW-like class than can allow for Unity to parse associative arrays - or at the very least multiple variables in a single call?

    I'm confident many have asked for this, I know I've read days of topics on the subject, but I can't find any thread offering the resolve. If its out there, I'd be grateful for a link.
     
  2. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    The return is a single string, but you can take the result, and in Unity, parse it. This will allow a single WWW-PHP call to give you multiple variables of different types, not just strings.

    There are unity-mySQL solutions, they involve having the proper dll and mySQL Lite (I believe), so the database can run locally on the machine. For a web game, or at least a web database, I don't know of a direct app-database solution. Many have pointed out that it would be a gerat security risk to put such information directly into the source of your hackable app, but instead, use the WWW class to at last limit potential abuse.
     
  3. craigerz

    craigerz

    Joined:
    Mar 30, 2009
    Posts:
    19
    Thank you Tempest. I couldn't find any documentation in Unity's scripting reference regarding the proper syntax for parsing out the www.data. Can someone provide a sample parsing statement to head me in the right direction?
     
  4. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    WWW.data parsing is just string parsing. You can find examples in both JavaScript and C# on the web.

    In the below example, I have my php page echo a line of text where each value is delimited by a semi-colon.

    Code (csharp):
    1.  
    2. string[] info = data.Split(';');
    3.                
    4. for( int x = 1; x < info.Length; x++ )
    5. {
    6.     User kUser = new User();
    7.         kUser.name = info[x];
    8.         x++;
    9.     kUser.id   = int.Parse(info[x]);
    10. }
    11.  
    The string looks like this:

    ;Bob;1;Frank;2;Jimmy;6;Tom;12

    I start with a semi-colon, just because that's how my php page formats this string, and it's why x starts at 1, instead of 0, so I skip over info[0] which is null.
     
  5. craigerz

    craigerz

    Joined:
    Mar 30, 2009
    Posts:
    19
    Ah straight JS, thought it was a builtin Unity thing. Thank you again Tempest!
     
  6. Flimgoblin

    Flimgoblin

    Joined:
    Nov 25, 2009
    Posts:
    89
    Might be of use to you: I've written a basic JSON parser as well as a lightweight XML parser.

    http://www.roguishness.com/unity/

    If you've got a fairly basic/streamlined structure something custom may serve you better, or if you're not fussy about adding 1Meg to your executable then you can use the full range of System.XML stuff which is waaay more flexible.

    Can't just "exec" the JSON response as you can with browser javascript (though that's just asking for some nasty cross-site scripting anyway) hence the JSON interpreter.

    Let me know if you find these of use/find any bugs/whatever.