Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Firebase Issue in Unity Android Build

Discussion in 'Package Manager' started by aazed, Aug 9, 2023.

  1. aazed

    aazed

    Joined:
    Jun 20, 2023
    Posts:
    2
    I've created an app that dynamically generates data from Firebase, and while it works perfectly fine in the Unity Player, its not while testing it on an Android device.
    I suspect the root of the issue might be within the Gradle setup, an area I'm not very experienced with.
    I am using unity 2022, Gradle 7.2, all the necessary firebase sdks are imported.

    This is the script to initialize firebase. Please advise.

    using UnityEngine;
    using UnityEngine.UI;
    using Firebase;
    using Firebase.Database;
    using Firebase.Extensions;
    using UnityEngine.Networking;
    using System.Collections;
    using UnityEngine.SceneManagement;

    public class StoreButtonManager : MonoBehaviour
    {
    public GameObject storeButtonPrefab;
    public Transform buttonParent;
    private DatabaseReference databaseReference;

    private void Awake()
    {
    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
    {
    if (task.Exception != null)
    {
    Debug.LogError($"Failed to initialize Firebase with {task.Exception}");
    return;
    }

    databaseReference = FirebaseDatabase.DefaultInstance.RootReference;

    FetchStoreData();
    });
    }

    private void FetchStoreData()
    {
    databaseReference.Child("Stores").GetValueAsync().ContinueWithOnMainThread(task =>
    {
    if (task.Exception != null)
    {
    Debug.LogError($"Failed to fetch store data with {task.Exception}");
    return;
    }

    foreach (var storeSnapshot in task.Result.Children)
    {
    CreateStoreButton(storeSnapshot);
    }
    });
    }

    private void CreateStoreButton(DataSnapshot storeSnapshot)
    {
    GameObject newButton = Instantiate(storeButtonPrefab, buttonParent);
    Text buttonText = newButton.GetComponentInChildren<Text>();
    buttonText.text = storeSnapshot.Child("StoreName").Value.ToString();

    Button buttonComponent = newButton.GetComponent<Button>();
    buttonComponent.onClick.AddListener(() => LoadStoreLandingPage(storeSnapshot));

    // Fetch and set the store button sprite
    string storeImageURL = storeSnapshot.Child("StoreImage").Value.ToString();
    StartCoroutine(LoadImage(storeImageURL, newButton.GetComponent<Image>()));
    }

    private IEnumerator LoadImage(string url, Image targetImage)
    {
    using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
    {
    yield return request.SendWebRequest();

    if (request.result == UnityWebRequest.Result.Success)
    {
    Texture2D texture = DownloadHandlerTexture.GetContent(request);
    targetImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
    }
    }
    }

    private void LoadStoreLandingPage(DataSnapshot storeSnapshot)
    {
    string landingPageUrl = storeSnapshot.Child("StoreLandingPage").Value.ToString();

    int targetSceneIndex = 2; // Assuming "02_StoreLandingPage" has a build index of 2

    if (targetSceneIndex >= 0 && targetSceneIndex < SceneManager.sceneCountInBuildSettings)
    {
    PlayerPrefs.SetString("LandingPageURL", landingPageUrl); // Pass the landing page URL

    // Set the selected store's key for the next scene
    PlayerPrefs.SetString("SelectedStoreKey", storeSnapshot.Key);

    SceneManager.LoadScene(targetSceneIndex);
    }
    else
    {
    Debug.LogError("Scene index " + targetSceneIndex + " is out of range.");
    }
    }
    }
     
  2. joefspiro

    joefspiro

    Joined:
    Jan 6, 2022
    Posts:
    16
    Please describe how the behaviors differ in Unity and Android in terms of what occurs and what is logged.
     
  3. aazed

    aazed

    Joined:
    Jun 20, 2023
    Posts:
    2
    The app is working fine in the unity player and its fetching the data dynamically from firebase without any error. On android it does not, just a blank screen. I am getting an "SMPTE ST 2094-40" error from the logcat