playMaker

Author Topic: Check for Internet  (Read 20030 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Check for Internet
« Reply #15 on: October 20, 2020, 05:42:38 AM »
Hi.

There is an action on the Ecosystem :



I have not use this yet, but it probably works :)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Check for Internet
« Reply #16 on: October 20, 2020, 05:32:35 PM »
Yeah thanks Djaydino, but I looked at that one but couldn't install it (never appeared in the list). So not sure what's wrong with it.

I also was looking for something much simpler like the one posted here, just that Unity changed all the network stuff and I couldn't find out how to update it with my limited knowledge.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Check for Internet
« Reply #17 on: December 10, 2020, 04:55:43 PM »
Bumped for the greater good. Is this action reliable for iOS/Android?
I wonder what would be the simplest and surest method to check on the availability of a connection.

Edit.
https://hutonggames.com/playmakerforum/index.php?topic=22980.0
Has there been an update on this lately?
« Last Edit: December 10, 2020, 04:58:00 PM by Broken Stylus »

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Check for Internet
« Reply #18 on: October 09, 2022, 05:19:45 AM »

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Check for Internet
« Reply #19 on: October 17, 2022, 11:56:00 PM »
This is what I use nowadays. Seems to work the best:

Code: [Select]
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Device)]
[Tooltip("Check for internet connection.")]

public class CheckForInternetConnection : FsmStateAction
{
[Tooltip("Send event if internet is available.")]
public FsmEvent internetIsOn;

[Tooltip("Send event if internet is not available.")]
public FsmEvent internetIsOff;

[Tooltip("Sets a bool to true or false depending on the state of the Internet")]
public FsmBool isInternetOn;


public override void Reset()
{
internetIsOn = null;
internetIsOff = null;
isInternetOn = true;
}

public override void OnEnter()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
isInternetOn.Value = false;
Fsm.Event(internetIsOff);
}
else
{
isInternetOn.Value = true;
Fsm.Event(internetIsOn);
}
}
}
}

amdev

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Check for Internet
« Reply #20 on: December 25, 2022, 09:00:52 AM »
Code: [Select]

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Custom")]
    [Tooltip("Checks if there is an internet connection on the Android device.")]
    public class CheckInternetConnection : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        [Tooltip("Store the result of the check in a boolean variable.")]
        public FsmBool internetAvailable;

        public override void OnEnter()
        {
            // Check for internet connection
            CheckConnection();
        }

        void CheckConnection()
        {
            // Set up a UnityWebRequest to check for internet connection
            UnityWebRequest www = UnityWebRequest.Get("http://google.com");
            www.SendWebRequest();

            // Check for internet connection
            if (www.isNetworkError || www.isHttpError)
            {
                // There is no internet connection
                internetAvailable.Value = false;
            }
            else
            {
                // There is an internet connection
                internetAvailable.Value = true;
            }
        }
    }
}