0

I am new in Unity and C# and I had search all over to find the equivalent of dictionaryWithContentsOfURL (Objective C) in Unity (C#).

Or please at least give me an idea on how to retrieve contents from given URL, and put it on dictionary or in an array so that I can instantiate it to a variable and use it in the entire scripts. Thank you very much.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
LouieJe
  • 1
  • 3
  • Need to see URL. Every URL is different and scraping the URL may or may not be easy. – jdweng Jul 20 '15 at 12:42
  • You'll probably need an property list / xml parser for that. – d4Rk Jul 20 '15 at 13:04
  • possible duplicate of [Alternative to HttpUtility.ParseQueryString without System.Web dependency?](http://stackoverflow.com/questions/27442985/alternative-to-httputility-parsequerystring-without-system-web-dependency) – Matías Fidemraizer Jul 20 '15 at 13:06
  • Hey d4Rk, can you tell me how to parse property list / xml? or any link to for me to know how to, because my mate told me thats what I need, then I have to learn how to handle coroutine.... – LouieJe Jul 20 '15 at 13:56

1 Answers1

0

For fetching content from a given URL, you can check the WWW class, here is a simple example in your case.

IEnumerator ProcessContentFromInternet(string url) {
    WWW www = new WWW(url);
    yield return www; // waiting the request to finish

    if (www.error == null) { // if not error
        string response = www.text; // here is raw data

        // call another method for parsing the response text
        // will discuss it later
        ParseContentFromText(response);
    } else {
        Debug.LogError(www.error);
        // there is an error when access the url
        // need do something here
    }
}

You can call StartCoroutine(ParseContentFrom(url)); to start it, it probably will take a while base on your internet and the size of the content. You can use www.process and www.isDone to view the process/state, check the official documentation for more details.

I cannot give a detailed solution for parsing the raw content to dictionary/array, since I don't know what format you will use (I personally would recommend JSON since it's probably the easiest one to deal with, check this: SimpleJSON for Unity).

For parsing XML, you can find plenty of tutorials by Googling, Most things that say C# will work in Unity3D as well. Here is a good one: Loading data from a xml file.

I am not sure about the format of your "property list", but for what I have seen, the .plist files on my Mac are all XML. There is no different for parsing the both.

Lin
  • 620
  • 7
  • 11
  • Thanks for the idea, it might solve my problem once I read the links you posted, I will not forget to hit vote for this answer if ever. – LouieJe Jul 21 '15 at 00:52