Unity3d reads and modifies Json data on Android, unity3djson

Source: Internet
Author: User

Unity3d reads and modifies Json data on Android, unity3djson

First, we need to download a file LitJson. dll (download linkPs: It is downloaded from your Baidu cloud disk. If the link is out of date, leave a message or clickCarrier, password: 5foa)

In addition, we need to configure the Jar and SDK because we want to release it to the Android mobile phone. Here we will not elaborate on the modification and reading of Json on the android terminal, of course, if the mobile phone is not released, the computer can also be used normally.

(1) first, we will build a simple test environment ()

 

The text box, button, input box, And button are displayed in sequence from top to bottom. (If necessary, you can enlarge the UI to avoid being too small on the phone or adjust the resolution in the upper left corner of the Game ),. after the setup, Save the scenario (File -- Save Scene from the menu)

(2) Now we need to write the script. Create a Csharp script named JsonTest, as shown below:

1 using UnityEngine; 2 using System. collections; 3 using LitJson; // import Json 4 using UnityEngine. UI; // import UI 5 using System. text; // use StringBuilder 6 using System. IO; // use the file stream 7 8 /// <summary> 9 /// test the reading of the Json mobile phone 10 /// </summary> 11 public class JsonTest: monoBehaviour 12 {13 public Text jsonText; // display the Json Text box 14 public InputField input; // modify the Json data input Text box 15 public string name; // test whether the Json value 16 17 void Awake () 18 {19 name = "Person"; // write a character's attribute 20 SaveJsonString (GetJson ()); // after obtaining a Json data, store the data 21} 22 23 public void DidReadJsonButton_Click () // The button event 24 {25 jsonText for reading Json text. text = GetJsonString (); // read json data and display it in the text box 26} 27 28 public void DidConfirmInputButton_Click () // read the button event 29 {30 name = input. text; // get the data in the text box 31 SaveJsonString (GetJson (); // store it 32} 33 34 public string GetJson () // as tested here, here I will simply write a Json data 35 {// get the Json Format String 36 StringBuilder sb = new StringBuilder (); 37 JsonWriter writer = new JsonWriter (sb); 38 writer. writeObjectStart (); // The dictionary starts 39 writer. writePropertyName (name); // key value (the character attribute is printed out by changing the name) 40 writer. writeObjectStart (); 41 writer. writePropertyName ("Hp"); // contains these attributes: 42 writer. write ("20"); 43 writer. writePropertyName ("Mp"); 44 writer. write ("60"); 45 writer. writePropertyName ("Attack"); 46 writer. write ("30"); 47 writer. writePropertyName ("Exp"); 48 writer. write ("100"); 49 writer. writeObjectEnd (); 50 writer. writeObjectEnd (); // The End Of The dictionary 51 return sb. toString (); // return a Json string 52} 53 54 public void SaveJsonString (string JsonString) // save a Json string 55 {56FileInfo file = new FileInfo (Application. persistentDataPath + "JsonData. Json"); // This is the focus and will be detailed below. Here, you only need to know that it is just a path.57 StreamWriter writer = file. createText (); // method of text writing 58 writer. write (JsonString); // Write data 59 writer. close (); // Close the write pointer 60 writer. dispose (); // destroy write pointer 61} 62 63 public string GetJsonString () // read json data from the file 64 {// as this is only a test, so no specific parsing data is written. 65StreamReader reader = new StreamReader (Application. persistentDataPath + "JsonData. Json ");66 string jsonData = reader. ReadToEnd (); 67 reader. Close (); 68 reader. Dispose (); 69 return jsonData; 70} 71}

(3) We have finished writing the script. Now we need to mount the script. I have created an empty object mounting script, then you need to drag the corresponding object (do not forget to drag the button event). The running result is as follows:

The result shows that the computer has succeeded.

(4) The next step is to package and release it. In the menu bar (File) -- Build Settings -- Platform (Android) -- Add Current (or drag the previous scenario into it, save the scenario as much as possible before packaging, and then add it.) Click the icon on the Right of Player Settings and click here, to the com. company. to rotate the screen, you can adjust the desired effect in Default Orientaion in Resolution and Preadentaion.

The above are the effects on the phone, the start page, the display, and the modification page.

(5) If you want to publish data to a computer, you can do the same. You just need to replace the Platform released in Step 4 with the PC, can also be used

(6) Now let's talk aboutApplication. persistentDataPath

This path is only available after the application is installed on the mobile phone end. Therefore, you cannot directly store files in this path. Resources in this path can be modified and read, the data to be modified can be stored here. For the Resources in the Resources resource folder, the Resources here can be modified through Resources. load <>.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.