Open external files

I want to open the file in external mode, not into a game object.

I tried a script that is working pretty well. I tryed with JPG, PDF, DOCX and anothers with excelent results.

i used, in this case:

function OnGUI () {
   if (GUI.Button (Rect (100,10,100,30), "Open File")) {
      Application.OpenURL ((Application.dataPath) + "/Folder/File.Type");
   }
}

for the time is working in a GUI button, but it can be made for a trigger or anything in the game.

It’s working on Windows Standalone Player…
Can anyone helpme to make this working for Windows and Mac at the same time?

It’s a great code to view Readme.rtf files or Purchaseorder.html from the hard disk

To make it works for different platform it is only necessary to make something like:

			if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) {
				// TODO - write your code for Windows
			} else {
				// TODO - write your code for Mac
			} // if-else

You can also specify others platform like WindowsWebPlayer, etc.

Pleas enote that the code shown works for Windows executable but also in Unity editor (running on Windows platform).

I hope this can help… if not try to be more specific on your questions !! :wink:

thanks a lot man!

:wink:

you’re welcome !!!

Radiolobito, I’ve tried opening an .html file using your code but I must be doing something wrong as the file doesn’t open. I’ve imported the html file into Unity3d’s Asset folder and I have the following code:

if (GUI.Button (Rect(522,710,85,40),"Help"")){
Application.OpenURL ((Application.dataPath)+ "Users/... /Assets/Assets/Help.html");
}

I’m assuming that one has to include the entire path with folder and all the sub-folders (I’ve omitted some of the sub-folders for brevity in the code above) to get it to work. Can you please tell me what I’m doing wrong?

Are you working at MAC, right?

In PC i used to make the folder and contents after publish the game…

In MAC i dont know how to make this, because the folder is an application folder… in PC or Linux you can make that folder, but i dont know the internal folder structure to do this…

sorry but i can’t help you…

if you try to use this command

function OnGUI () {
if (GUI.Button (Rect (250,10,100,30), "Show path")) {
//this can show the path in the Safari, Firefox
Application.OpenURL ("index.html");
//this can show the path in the internal console
print (Application.dataPath);
}
}

hope this can help you… if its working please say us… to apply this in our MAC games

Thanks Radiolobito. Checked the path and it was OK but Safari just doesn’t open. I also checked and popup blocking isn’t on. I’ll just put in a url of an on-line webpage.

remember… if you’re trying to open an URL, put the complete direction; like

http://www.yoursite.com

I want to open a local file in runtime from a Unity-Standalone (.app) on a Mac.
I use something like:

path= Application.dataPath+"Image.jpg"; //(The Result is: "Applications/MyGame.app/Image.jpg")
Application.OpenURL (path);

I can read or save textfiles or even load textures located at path but OpenUrl simply does not open the file, no matter if it´s a jpg or html-file.

Any ideas?
Possible that opening local files with OpenUrl() is not working for MacOS?

EDIT: Solved
Adding “File:” to path makes it work

Hi Cascho01,

What exactly was the code that got this to work? Can’t seem to have any luck my end!

Thanks

You need a slash between Application.dataPath and the added String, like this:
String path = Application.dataPath + “/openThis.txt”;

Not sure, but try:

“File://” + Application.dataPath + …

its not working in the pdf format …can u plz suggest me some solution

How to Read In Android Device, It works fine in PC

To anyone trying to open external files from a Unity application on Android, I made this plugin that may help: GitHub - Andy-Roger/UnityAndroidNativeFileOpener: Open files from a Unity app natively on an Android device.

If you are trying to open any document using Application.OpenURL then it won’t work. Unity removed that support in latest versions. You can use AndroidOpenUrl.OpenUrl(documentUrl, dataType) for these cases.

public void Example()
{  
     string dataType = "application/pdf";
     string documentUrl = "/storage/emulated/0/Downloads/template.pdf";
     AndroidOpenUrl.OpenUrl(documentUrl, dataType); // you can specify any MIME type when opening a file by explicitly specifying the dataType parameter
}

You will get a demo and installation steps of the package from the following repository.

1 Like