Unity3d read modify JSON data on the Android side

Source: Internet
Author: User

First we need to download a file LitJson.dll (download link PS: is to use their own Baidu cloud disk download If the link has been received, please leave a message or download it yourself, password: 5FOA)

In addition, because we want to publish to the Android phone, so we need to configure the jar and SDK, because this is mainly about the JSON in the Android modification and reading, here is not elaborate, of course, if you do not publish the phone end, the computer can also be used normally

(1) Let's start by simply setting up a test environment ()

From top to bottom, the text box, button, input box, button, function display (you can enlarge the UI if needed to avoid being too small on the phone, or adjust the resolution in the upper left corner of the game). After the build, save the scene (menu to File--save scene)

(2) Now we need to write a script, build a csharp script named Jsontest, as follows:

1 usingUnityengine;2 usingSystem.Collections;3 usingLitjson;//Import JSON4 usingUnityengine.ui;//Import UI5 usingSystem.Text; Using StringBuilder6 usingSystem.IO; Working with file streams7 8 /// <summary>9 ///JSON phone-side read TestTen /// </summary> One  Public classJsontest:monobehaviour A { -      PublicText Jsontext;//text boxes that display JSON -      PublicInputfield input;//Modify the input text box for JSON data the      Public stringName//test if the value inside the JSON has been replaced -  -     voidAwake () -     { +Name =" Person";//write a character's attributes - savejsonstring (Getjson ()); After getting a JSON data, store the data. +     } A  at      Public voidDidreadjsonbutton_click ()//button event to read JSON text -     { -Jsontext.text =getjsonstring (); Read JSON data and display it in a text box -     } -  -      Public voidDidconfirminputbutton_click ()//Reading button Events that determine the contents of an input box in     { -name = Input.text;//get the data inside the text box to savejsonstring (Getjson ()); Store it up +     } -  the      Public stringGetjson ()//Since this is a test, I'll simply write a JSON data here *{//get the JSON format string $StringBuilder SB =NewStringBuilder ();Panax NotoginsengJsonwriter writer =NewJsonwriter (SB); -Writer. Writeobjectstart ();//The dictionary begins theWriter. Writepropertyname (name);//Key values (character properties are printed by changing the name to see the value) + writer. Writeobjectstart (); AWriter. Writepropertyname ("Hp");//There are these attributes theWriter. Write (" -"); +Writer. Writepropertyname ("Mp");  -Writer. Write (" -"); $Writer. Writepropertyname ("Attack"); $Writer. Write (" -"); -Writer. Writepropertyname ("Exp"); -Writer. Write (" -"); the writer. Writeobjectend (); -Writer. Writeobjectend ();//End of DictionaryWuyi         returnSb. ToString ();//returns a JSON-formatted string the     } -  Wu      Public voidSavejsonstring (stringjsonstring)//save JSON format string -     { About        FileInfo file = new FileInfo (Application.persistentdatapath + "jsondata.json"   ); Here is the point, will be in detail below, here just need to know it is just a path  $StreamWriter writer = file. CreateText ();//how to write with text -Writer. Write (jsonstring);//Write Data -Writer. Close ();//turn off the write pointer -Writer. Dispose ();//destroying the Write pointer A     } +  the      Public stringGetjsonstring ()//read JSON data from a file -{//Since this is just a test, we don't write specific parsing data. $         StreamReader reader = new StreamReader (Application.persistentdatapath + "Jsondata.json "); the         stringJsondata =Reader. ReadToEnd (); the Reader. Close (); the Reader. Dispose (); the         returnJsondata; -     } in}

(3) The script we have finished, now we need to mount the script, I built an empty object mount script, and then need to drag the corresponding object (do not forget the Drag button event OH) Run the result is as follows:

Through the results can be seen, on the computer side has been successful

(4) Next is the package release, the menu bar (File)--build settings--platform (Android)-Add current (or drag the scene just before, try to save the scene before packaging, and then add it), Here you need to click on the player Settings to the right of a small Android icon, click, to Ohter Settings inside the BA Bundle Identifier inside the COM. Company. After the change, if you need to rotate the screen can be in resolution and preadentaion inside the default orientaion inside to adjust the effect you want

Above is the phone above the effect, start page, display, modify the page

(5) If you want to publish to the computer side, also can oh, just need to put the fourth step in the release of the release platform (Platform) to replace the PC can be, the same can be used

(6) Now let's say application.persistentdatapath this path

This path only after the application installed on the mobile phone after the path, so the file can not be stored directly in the path, the resources under this path can be modified and read, for we need to modify the data can be stored in here, take the resources stored in the Resources folder inside, The resources in this can be obtained through the resources.load<>, but can not be modified, this comparison pit, but it is possible to put some precast in this folder is more convenient.

Unity3d read modify JSON data on the Android side

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.