0

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

stylobic
  • 1
  • 3
  • if its opening a text file, chances are thats what you're calling.. – BugFinder Jul 25 '23 at 07:07
  • from your screenshot it looks like you are linking to some raw text file hosted on github .... Have you tried simply putting the link directly into that `linkURL` ..? – derHugo Jul 25 '23 at 10:45
  • derHugo As you see from screenshot i put a raw link from github to be enable to change it later from host without need of update my app. – stylobic Jul 25 '23 at 12:35
  • i also try to put link inside script like this : Application.OpenURL("https://raw.githubusercontent.com/xxxxxx/xxxxxxr/main/example.txt"); but still open it as a txt – stylobic Jul 25 '23 at 14:28

1 Answers1

0

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);
    }
}
stylobic
  • 1
  • 3