Issue with Unity cross-promotion button opening raw text file instead of redirecting to Google Play link

39 Views Asked by At

I'm currently working on a Unity project and have implemented a cross-promotion button that should open a Google Play link for my app. However, when I click the button, it opens a raw text file hosted on GitHub instead of redirecting to the Google Play link. I've tried hosting the Google Play link in a text file on different platforms like GitHub and Google Drive, but the issue persists. I've also tried using URL shortening services and redirection techniques, but none of them seem to work. Here's a simplified version of the script I'm using:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class crospromo : MonoBehaviour
{
    public string imageURL;
    public string linkURL;
    private Image image;

    private void Start()
    {
        image = GetComponent<Image>();
        StartCoroutine(LoadImage());
    }

    private IEnumerator LoadImage()
    {
        using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(imageURL))
        {
            yield return www.SendWebRequest();

            if (!www.isNetworkError && !www.isHttpError)
            {
                Texture2D texture = DownloadHandlerTexture.GetContent(www);
                if (texture != null)
                {
                    Sprite sprite = Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
                    image.sprite = sprite;
                }
            }
        }
    }

    public void OnClick()
    {
        Application.OpenURL(linkURL);
    }
}

unity inspector screenshot

Screen from browser

1

There are 1 best solutions below

0
On

I get help from AI generator :

it update my script like this :

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class crospromo : MonoBehaviour
{
    public string imageURL;
    public string linkURLFileURL; // URL of the text file containing the Google Play link
    private Image image;
    private string linkURL; // Declare linkURL as a class-level variable

    private void Start()
    {
        image = GetComponent<Image>();
        StartCoroutine(LoadImage());
        StartCoroutine(LoadLinkURL());
    }

    private IEnumerator LoadImage()
    {
        using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(imageURL))
        {
            yield return www.SendWebRequest();

            if (!www.isNetworkError && !www.isHttpError)
            {
                Texture2D texture = DownloadHandlerTexture.GetContent(www);
                if (texture != null)
                {
                    Sprite sprite = Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
                    image.sprite = sprite;
                }
            }
        }
    }

    private IEnumerator LoadLinkURL()
    {
        using (UnityWebRequest www = UnityWebRequest.Get(linkURLFileURL))
        {
            yield return www.SendWebRequest();

            if (!www.isNetworkError && !www.isHttpError)
            {
                linkURL = www.downloadHandler.text.Trim();
            }
        }
    }

    public void OnClick()
    {
        Application.OpenURL(linkURL);
    }
}