- Home /
How to get multiple variables from page on server?
I'm looking for a way to get more than one variable from a page that's located in my server. At the moment the page has this text:
Critical Chance: 20
Enemy Health Min: 20
Enemy Health Max: 100
Gold Drop Min: 10
Gold Drop Max: 200
Random Chance Gold Multiplier: 10
Gold Multiplier Min: 2
Gold Multiplier Max: 5
I want to get certain numbers from that page and use it in my C# script for certain variables. I understand I could use WWW, but It grabs all the text from that page instead of each one. What's a smarter way of doing this? ?
Answer by Bilelmnasser · Oct 09, 2017 at 09:19 AM
hi, make your webpage generate json text response like this :
{
"Critical_Chance": 20,
"Enemy_Health_Min": 20,
"Enemy_Health_Max": 100,
"Gold_Drop_Min": 10,
"Gold_Drop_Max": 200,
"Random_Chance_Gold_Multiplier": 10,
"Gold_Multiplier_Min": 2,
"Gold_Multiplier_Max": 5
}
in unity use a class like :
[Serializable]
public class MyData
{
public int Critical_Chance ;
public int Enemy_Health_Min ;
public int Enemy_Health_Max ;
public int Gold_Drop_Min ;
public int Gold_Drop_Max ;
public int Random_Chance_Gold_Multiplier ;
public int Gold_Multiplier_Min;
public int Gold_Multiplier_Max ;
}
use JsonUtility to Deserialize JSON into a Mydata Object :
//get the string from my server
string myServerResponse=getStringFromServer();
// deserialize json text into Object
MyData obj=JsonUtility.FromJson<MyData >(myServerResponse);
Wow thank you! I just have a few questions if you don't $$anonymous$$d.
Can I manually type that json code into the page and it will still grab it? $$anonymous$$y aim is to be able to edit certain game variables on the fly from a webpage in server, so for example, once I build it will still grab those variables from the web page and I can just change multipliers and enemy healths on the page without having to rebuild the game.
Can I make that $$anonymous$$yData class to an array and have it automatically add variables to it that I add/remove from the webpage?
How do I classify each variable as int, string, float, bool , in json and have the $$anonymous$$yData class set that for me?
Sorry if I'm asking so many questions, I'm new to working with json and unity and it seems way better than the way I'm doing it now :D
yes you can change all the json entries values with any type :) it's will automatically Deserialize it , just chnage the type also in the class :) yes you can make it an array just make Serializable class like :
[Serializable]
public class $$anonymous$$yDataArray
{
public $$anonymous$$yData[] myArray;
}