Search Unity

Question A Way to configure Economy Resources trough code (automation)

Discussion in 'Economy' started by Atlas-terrain, Mar 12, 2023.

  1. Atlas-terrain

    Atlas-terrain

    Joined:
    Jul 11, 2015
    Posts:
    115
    Hi, is there a way to configure your economy resources (items) trough code or via uploading json?
    using the UI to add/configure every resources is not something I look forward to :D

    I found there are editor tools but they don't seem to help this issue.
     
  2. Shaun-Peoples

    Shaun-Peoples

    Joined:
    Feb 26, 2013
    Posts:
    24
    I've done this by storing design-side resources in ScriptableObjects that are then serialized to JSON and uploaded via the Economy REST API

    https://services.docs.unity.com/economy-admin/v2/index.html#tag/Economy-Admin

    Sample code for uploading to Economy.

    Code (CSharp):
    1. public IEnumerator SubmitResources(List<string> jsonDataList, ServerOpResult success = null, ServerOpResult failure = null)
    2.     {
    3.         var url = $"https://services.api.unity.com/economy/v2/projects/{projectId}/environments/{environmentId}/configs/draft/resources";
    4.      
    5.         foreach(var jsonData in jsonDataList)
    6.         {
    7.             using UnityWebRequest www = UnityWebRequest.Post(new Uri(url), jsonData, "application/json");
    8.             www.SetRequestHeader("Authorization", $"Basic {loginId}:{pass}");
    9.             www.SetRequestHeader("Content-Type", "application/json; charset=utf-8");
    10.              
    11.             yield return www.SendWebRequest();
    12.  
    13.             if (www.result != UnityWebRequest.Result.Success)
    14.             {
    15.                 Debug.Log(www.error + " " + www.result);
    16.                 failure?.Invoke();
    17.             }
    18.             else
    19.             {
    20.                 Debug.Log("Form upload complete!");
    21.                 success?.Invoke();
    22.             }
    23.         }
    24.     }
     
    Last edited: Apr 11, 2023
    Ohilo and Atlas-terrain like this.
  3. Atlas-terrain

    Atlas-terrain

    Joined:
    Jul 11, 2015
    Posts:
    115
    ooh I see, thanks Shaun! :)
     
    Shaun-Peoples likes this.