1

So I'm trying to write a Unity3d Project that can restrain my access to certain URL. For example, if I want to block Youtube.com, it will block my access to youtube.com from all my browsers.

I see that in C# program you can use System.Windows.Form class to do it. But Unity3D don't have access to use System.Windows.Form. Is there any built-in method that I can use to block URL using unity3d?

Any suggestions would be appreciated. Thank you.

libra
  • 673
  • 1
  • 10
  • 30
  • What, you are trying unity3d to block URL on web. Is this right approach? Unity3d is not made for this . You should make desktop application or browser extension or utility to do this job. I guess. – Muhammad Faizan Khan Feb 20 '17 at 04:42
  • @MohammadFaizanKhan But the thing is the project I'm working on needs a lot of 3D graphics. And I'm quite familiar with Unity3d. So yeaaaa – libra Feb 20 '17 at 04:44
  • 3
    It is difficult for me to identitfy the relationship b/w 3d Graphics and restricting url on browser. anyhow best of luck – Muhammad Faizan Khan Feb 20 '17 at 04:45

1 Answers1

1

Is there any built-in method that I can use to block URL using unity3d?

No

There is no built in method to do this. You have to create one yourself.

On way to do this is through the hosts file located at

C:\Windows\System32\drivers\etc

Example of adding websites to block in this file:

To block stackoverflow:

127.0.0.1 www.stackoverflow.com

To block Google:

127.0.0.1 www.google.com

Putting it in Code:

1.Read the hosts file from C:\Windows\System32\drivers\etc line by line with File.ReadAllLines:

string[] hostData = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"));

2.Convert the hostData array to List with:

List<string> hostDataList = new List<string>(hostData );

3.To add url, simply make a function that takes string(url) as a parameter. Remove http from that url if there is one.

Loop through the hostDataList List and make sure that the url you want to add does not exist by checking it with String.Contains. If it does not exist, add it to the List:

string urlToBlock = "127.0.0.1" + " "+ url;
hostDataList.Add(urlToBlock);

4.To remove url, simply make a function that takes string(url) as a parameter. Remove http from that url if there is one.

Loop through the hostDataList List and make sure that the url you want to add exist. If it does not exist, return. If it exist, find the index then remove it from the List:

int index;
//Make sure that the Url exist before removing it
if (containsUrl(url, out index))
{
    hostDataList.RemoveAt(index);
}

Note:

The containsUrl function is simply a function that loops through the hostDataList List then checks if the provided string is found. If it is found return true and store the index in which the string is found to the out index parameter.

This is the prototype:

bool containsUrl(string url, out int index){...}

5.When you are done, save it by converting the hostDataList List back to array then saving it with File.WriteAllLines.

string savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
string[] hostDataArray = hostDataList.ToArray();
File.WriteAllLines(savePath, hostDataArray);

I know this works because I have done it before. With answer this, you should be able to get this done on Windows.

Notice:

For this to work, your application must be running with an administrator privileges. There are many ways to do this.

If testing from Unity's Editor, right click on Unity logo and then choose Run as administrator.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank You very much for this very detailed answer, I will test this in a few days ! – libra Feb 21 '17 at 05:00
  • 1
    Sure. Update me how it went. Happy coding! – Programmer Feb 21 '17 at 05:01
  • I finally got time to implement this in Unity, it's almost done. But there's one quick question, Is creating a proxy in Unity a better way of doing it? Using Network class. Since I'm also going to use my app cross-platform . e.g. The only way to block URL on android without root is to create a proxy – libra Feb 24 '17 at 14:43
  • 1
    I did not consider Android while answering this since it was not tagged. My solution is tested on Windows and works. The-same thing can be done on Mac and Linux so it should also work there. For Android, you have to use another method which is proxy. You can write a Java code then call if from C# as a plugin. – Programmer Feb 24 '17 at 15:57
  • 1
    You can also use Unity's [Platform dependent compilation](https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) to decide if you should call the Android or Windows/Linux/Mac version of the code. – Programmer Feb 24 '17 at 15:58
  • Gotcha! So you mean while on mac/pc, I can call Java code from c# and on android just call the android SDK, am I getting you right? Thanks for the link – libra Feb 24 '17 at 16:02
  • And also, It seems to have problem working on Mac. It doesn't have privileges to access hosts. Is there any way I can request user authorization from Unity3d? I did some google but not much results. (FYI i also posted the question [Here](http://stackoverflow.com/questions/42443185/unity3d-request-user-authorisation-osx)) – libra Feb 24 '17 at 16:05
  • 1
    While on Windows/Mac/Linux, you can use the method in my answer. While on Android, you have to make a Java plugin that you call from C#. The examples for Android/Java are really **hard** to find on the Internet but [this](https://gist.github.com/madeye/4352485) and [this](https://gist.github.com/madeye/2297083) may be useful to you. You can Google Unity Java/C# plugin for how to call Java from C#. – Programmer Feb 24 '17 at 16:10
  • 1
    As for the Mac problem you are having, I don't know. Maybe you should create a new question for that. There are good Mac users that can help. – Programmer Feb 24 '17 at 16:10
  • Thank you very much!, I will look into it! – libra Feb 24 '17 at 16:12