Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Utilities class - Expand to whole Unity please

Discussion in 'Unity Remote Config' started by MiTschMR, Aug 5, 2022.

  1. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    482
    The
    Utilities
    class in the
    Unity.Services.RemoteConfig
    namespace has a single method that is used for checking internet connection, namely
    CheckForInternetConnection
    . Is there any particular reason why this is only available in this namespace and i.e. not in the other UGS namespaces or the whole Unity namespace
    UnityEngine
    ?

    It would be great to have this simple check available everywhere, otherwise people will always look for another option to check internet connection. Some are easier to implement than others, but don't handle errors well etc.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    It's available anywhere as long as Unity services package is installed. Namespaces are not about "discovery". In fact, a great IDE makes discovery a non-issue!

    You can see I do not have Unity.Services usings on top, but I do have RemoteConfig package installed. Now I can just start typing "Utilities" and the IDE makes good suggestions which I can easily follow along:
    upload_2022-8-6_10-32-10.png

    And there we have it (the using statement is added automatically, I never manually edit using statements anymore, since unused will be removed automatically as well in my setup):
    upload_2022-8-6_10-33-12.png

    I only had to know that this method is in a "Utilities" namespace. If I don't know about that, but know there is a method called "CheckForInternetConnection" I'll search for it:
    upload_2022-8-6_10-38-42.png

    Of course, this isn't Microsoft Visual Studio as it is merely a decent IDE. :p

    Btw, this particular method is trivial enough to copy it into your own global namespace if you find it more convenient. I've seen this code snippet on the forums before.
    Code (CSharp):
    1. namespace Unity.Services.RemoteConfig
    2. {
    3.     public static class Utilities
    4.     {
    5.         public static bool CheckForInternetConnection()
    6.         {
    7.             try
    8.             {
    9.                 using (var client = new WebClient())
    10.                     using (client.OpenRead("http://unity3d.com"))
    11.                         return true;
    12.             }
    13.             catch
    14.             {
    15.                 return false;
    16.             }
    17.         }
    18.     }
    19. }
    It's slightly pretentious that the entire Internet must be unavailable if unity3d.com happens to be offline. :D
     
    Last edited: Aug 6, 2022
    PeachyPixels likes this.
  3. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    482
    I know it’s a simple as installing the package or copy/pasting the code, but I guess you didn’t get what I meant in general. Why is it only there? Was there any discussion internally about putting it to a global namespace so it’s available by default without knowing that it’s available in a specific package?
     
  4. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    I can't believe Unity allowed that to pass any sort of review process :(

    @MiTschMR

    I suggest you ignore that method. The correct Unity approach is to use the following property...

    https://docs.unity3d.com/ScriptReference/Application-internetReachability.html

    It's not perfect, but it's better than the utility method above imo.
     
    MiTschMR likes this.