- Home /
Check if connected to internet or not
I had a small study, in which I took a picture from the internet url and then convert into an image file. The picture attached to a plane as a texture. If I have a connection to the internet, all went smoothly, but when I do not have an internet connection, there is an error that the data retrieval error. I want to ask is how to check whether I am connected to the internet or not, if yes I took the url on the internet and turn it into a texture, if not, I take a byte of data from local and insert it into a texture. I will attach a simple code ..
using UnityEngine;
using System.Collections;
using System.IO;
public class RetrieveURL : MonoBehaviour
{
string urlGold1 = "http://commondatastorage.googleapis.com/ingress.com/img/Ingress_logo_512px.png";
GameObject[] Premium;
string Filename;
StreamReader reader;
StreamWriter writer;
void Start()
{
Load();
try
{
StartCoroutine(LoadImage());
}
catch
{
}
Premium = GameObject.FindGameObjectsWithTag("Premium");
}
void Update()
{
}
IEnumerator LoadImage()
{
byte[] bytes;
WWW gd2 = new WWW(urlGold1);
yield return gd2;
Texture2D gld2 = new Texture2D(256, 256, TextureFormat.DXT1, false);
bytes = gd2.bytes;
gld2.LoadImage(bytes);
renderer.material.mainTexture = gld2;
}
public void Load()
{
Filename = Application.persistentDataPath + "/SaveURLGold1.txt";
if (!File.Exists(Filename))
{
Filename = Application.persistentDataPath + "/SaveURLGold1.txt";
writer = File.CreateText(Filename);
writer.Write("URL Gold1 |" + urlGold1); writer.WriteLine("");
writer.Close();
}
else
{
reader = File.OpenText(Filename);
string buffer;
buffer = reader.ReadLine();
string[] token = buffer.Split('|');
urlGold1 = token[1];
}
}
}
Here Image if I have connection to internet
and here is image if i don't have connection to internet
I'm sorry for my bad english, I'm from Indonesia.. thanks :)
Answer by kumarc123 · Aug 26, 2013 at 10:04 AM
You can use Application.internetReachability() method to check is network is reachable via carrier, LAN, or unreachable.
Answer by tonic · Aug 20, 2014 at 12:49 PM
To truly know internet can be reached, there needs to be "captive portal detection". It's a technique also used by operating systems, where you simply do a request to an url which gives a reply with known contents. That helps to know if you're e.g. hitting a public WiFi login page. Just checking Application.internetReachability or doing a Ping doesn't guarantee that internet connection works.
I have made an easy asset called Internet Reachability Verifier and it allows you to use one of the captive portal detection methods maintained by big OS vendors, or your own custom url. It keeps you up-to-date whether you have verified internet access and it works with both desktop and mobile platforms (self-hosted custom method can be used with the web player plugin if you think that's necessary).
More info here: http://j.mp/IRVUN
Answer by niall1111 · Aug 20, 2014 at 12:59 PM
IEnumerator LoadImage()
{
byte[] bytes;
WWW gd2 = new WWW(urlGold1);
yield return gd2;
if ( gd2.error != null ) {
//There was an error downloading the file, do what you like from here.
}
else{
Texture2D gld2 = new Texture2D(256, 256, TextureFormat.DXT1, false);
bytes = gd2.bytes;
gld2.LoadImage(bytes);
renderer.material.mainTexture = gld2;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613112959im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to assign texture from url 1 Answer
Assigning UV Map to model at runtime 0 Answers
Best way to determine WWW timeout/404/etc.? 2 Answers
Load texture on disabled GameObject 1 Answer