problem with jason reading in Android app
I'm able to fetch data from json file in applicationPath and show the array of user in it, but it works only in dev enviroment. I use a 2d Panel with a template that gets cloned for each item in users.json
everything fine in the editor nothing happens in android apk.
here is the script attached to the panel
using UnityEngine; using UnityEngine.UI; using System; using System.Collections; using UnityEngine.Networking;
public static class ButtonExtension { public static void AddEventListener(this Button button, T param, Action OnClick) { button.onClick.AddListener(delegate () { OnClick(param); }); } }
public class ShowUsers : MonoBehaviour {
[Serializable]
public struct Operatori
{
public string id; // il campo deve essere uguale "caseSensitive" al campo del tracciato JSON
public string nome;
public string password;
public string stampaScontrino;
public string stampaProforma;
public string storna;
public string modificaPrezzo;
public Sprite Icon;
public string IconUrl;
}
Operatori[] allUsers;
[SerializeField] Sprite defaultIcon;
void Start()
{
//fetch data from Json
//StartCoroutine(GetOperatori());
DrawOperatori();
}
void DrawUI()
{
//prende il template modello child(0) del go a cui è attaccata questo script ovvero il GO "Panel" in alternattiva instantiate a prefab
GameObject buttonTemplate = transform.GetChild(0).gameObject;
GameObject g;
int N = allUsers.Length;
for (int i = 0; i < N; i++)
{
g = Instantiate(buttonTemplate, transform);
g.transform.GetChild(0).GetComponent<Text>().text = allUsers[i].nome;
g.transform.GetChild(1).GetComponent<Text>().text = allUsers[i].password;
g.transform.GetChild(2).GetComponent<Text>().text = allUsers[i].stampaScontrino;
g.transform.GetChild(3).GetComponent<Text>().text = allUsers[i].stampaProforma;
g.transform.GetChild(4).GetComponent<Text>().text = allUsers[i].storna;
g.transform.GetChild(5).GetComponent<Text>().text = allUsers[i].modificaPrezzo;
g.transform.GetChild(6).GetComponent<Image>().sprite = allUsers[i].Icon;
g.GetComponent<Button>().AddEventListener(i, ItemClicked);
}
Destroy(buttonTemplate);//distrugge il template originale pe rlasciare in griglia solo i cloni
Debug.Log("operatori shown");
}
void ItemClicked(int itemIndex)
{
Debug.Log("name " + allUsers[itemIndex].nome);
}
public void DrawOperatori()
{
StartCoroutine(GetOperatori());
}
//***************************************************
IEnumerator GetOperatori()
{
yield return new WaitForSeconds(1);
string url = (Application.persistentDataPath + "/users.json");
//attenzione che non serve chiamare il web per caricare un file locale
UnityWebRequest request = UnityWebRequest.Get(url);
request.chunkedTransfer = false;
yield return request.Send();
if (request.isNetworkError)
{
//show message "no internet "
}
else
{
if (request.isDone)
{
allUsers = JsonHelperArray.GetArray<Operatori>(request.downloadHandler.text);
//StartCoroutine(GetOperatoriIcons());
}
}
DrawUI(); // non caricando la coroutine getoperatoriicons inserisco qui la drawui()
}
//IEnumerator GetOperatoriIcons()
//{
// for (int i = 0; i < allUsers.Length; i++)
// {
// WWW w = new WWW(allUsers[i].IconUrl);
// yield return w;
// if (w.error != null)
// {
// //error
// //show default image
// allUsers[i].Icon = defaultIcon;
// }
// else
// {
// if (w.isDone)
// {
// Texture2D tx = w.texture;
// allUsers[i].Icon = Sprite.Create(tx, new Rect(0f, 0f, tx.width, tx.height), Vector2.zero, 10f);
// }
// }
// }
// DrawUI();
}
}
Comment
Your answer
