-3

I had created a small android game that had some character names and some values, I want to control the names and value so that I change value whenever I need. I searched online but I didn't find any quick solution for that. Is there any way to control the values online through some web service so that I can change value online and it will create changes in my game?

Rubick
  • 296
  • 3
  • 22
  • use this [What is firebase and how to use it in Android?](https://stackoverflow.com/questions/33720614/what-is-firebase-and-how-to-use-it-in-android) – Mojtaba jalambadani Sep 03 '18 at 08:24
  • Instead of databases you can simply read a text from a file in the [Persitent Data Path](https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html) which you still can replace after a build. – derHugo Sep 05 '18 at 13:20

2 Answers2

1

You'd need a server to store your data, even GitHub would do and it has free plan. Then your code needs to be designed so it takes those.

Here could be a short example. This is what you have on github:

{
    "characterName" : "Buldor",
    "strength" : 10
}

You'd need a C# version of that:

[Serializable]
public class Response
{
   public string characterName = null;
   public int strength = 0;
}

Then you'd need a loading screen on your game where you fetch the info from the server and assign them. It means your game is no longer valid offline (or you'd used previous loaded or default values).

void Start()
    {
        StartCoroutine(GetText());
    }

    IEnumerator GetText()
    {
        using (UnityWebRequest www = UnityWebRequest.Get("github_url"))
        {
            yield return www.Send();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                // Show results as text
                Debug.Log(www.downloadHandler.text);

                Response res = JsonUtility.FromJson<Response>(www.downloadHandler.text);
                PlayerPrefs.SetString("characterName", res.characterName);
                PlayerPrefs.SetInt("strength", res.strength);
            }
        }
    }
}

And finally, you have the character class:

public class Character : MonoBehaviour
{
     void Start(){
         string characterName= PlayerPrefs.GetString("characterName", "OldName");
         int strength = PlayerPrefs.GetInt("strength", 0);      
     }
}

So to sum it up, first create a json to store on a server, get the url of the location. Then download the file, it can be json, xml, txt or anything, but make sure you know how to parse it and json already have it all so more simple. Then parse the file and store the values, either in PlayerPrefs like I did or directly into the player.

Then extend the json file to store more info.

Everts
  • 10,408
  • 2
  • 34
  • 45
  • thats great but letme tell whats I want to get , i am creating app for food ordering everthing is done but the problem is I am unable to give customers freedom to edit price and item names ,in future if they want to update names or prices then they have to contact me and then i have to edit everytime they ask me to do – harsh gupta Sep 03 '18 at 09:51
  • Then in this case you need way bigger installation. First you need user management, could use Firebase. Second, you need to GET/POST/PATCH/DELETE (CRUD) on the server so any registered user can request his info, add new ones, and modifies existing ones. This is not related to Unity or Android but about taking are of a database. – Everts Sep 03 '18 at 10:19
  • that will help , i dont know how to write code for your above codes file for json,if you can just write that code that i hade to put on server then please put it – harsh gupta Sep 03 '18 at 10:44
  • This I cannot do because I do not know what are the information you are willing to store. Json is quite simple. I suggest you take a look at it, the main idea is a key/value concept. Then once you have the json you can convert it to c# with json2csharp.com – Everts Sep 03 '18 at 11:28
  • I think I had to research On firebase – harsh gupta Sep 03 '18 at 12:24
0

Yes this is possible! You can use the database like Mongo, Redis or MySQL, it depends on your value! And work with the POST or GET HTTP request to get the value! And for the back-end, you can use Golang, PHP, or anything you can! If you have a game with many users my offer is Golang! And you can use these link to work with and POST Get in unity:

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html

Good luck! If you have a question about Golang or PHP code you can ask me! :))

  • Just for info, a post request would require work to be done on the backend. Just sending a POST will not do, it needs to be taken care since the server has no idea what you meant to do with the sent data. GET is more straight forward. – Everts Sep 03 '18 at 09:09