Search Unity

  1. 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


    Aug 28, 2018
    class in the
    namespace has a single method that is used for checking internet connection, namely
    . 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

    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


    Apr 10, 2014
    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:

    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):

    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:

    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(""))
    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 happens to be offline. :D
    Last edited: Aug 6, 2022
    PeachyPixels likes this.
  3. MiTschMR


    Aug 28, 2018
    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


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


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

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