Reputation: 683
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.
Upvotes: 1
Views: 2638
Reputation: 125245
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.
Upvotes: 1