Read external files from resources Unity - unity3d

I'm developing an application with Unity in VR, I need to read a filename, which inserts the user, to be uploaded within the application and other 2-3 options that always the user could choose. I had thought of an initial menu in which to put that data but it is not practical for a VR solution. So I had thought of creating a ' setting ' file where the user, before starting the application, inserts the options, the name and the path of the file from which to load the data. But I do not know how to read a file that is outside the resources of the Unity project. Also how can I create this file setting so that it is always in the same location after I Build the project and is visible within the EXE folder? Would anyone know how to help me? Or maybe give me some advice on how to proceed? Thanks in advance.

Welcome to the stack!
I suppose you are creating a desktop-based game? For your case I suggest using the StreamingAsset folder to store the config file. When you build the game it will be placed in <BuildFolder>/<GameName>_Data/StreamingAssets.
For loading a file, you can use any of C#'s many different System.IO classes, a simple way to read file's content as text could be something like this:
var path = Application.dataPath + "/StreamingAssets/settings.txt";
using (StreamReader reader = new StreamReader(path))
{
try
{
var fileContent = reader.ReadToEnd();
/* do something with the content here */
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
Another approach for your situation, which I think is more user-friendly, is to use something like FileBrowser asset which allow user to choose a file in a keyboard-free manner. I don't know if it works with VR though.

Related

How do you create files in arbitrary locations using Swift?

How do you go about creating files at arbitrary locations given you have the absolute path? I want to create a file in some arbitrary location /Users/me/random/project3/<moreStuffInThePath. How would I go about doing this?
To create a directory I would do:
try FileManager.default.createDirectory(atPath: "/Users/me/random/project3/<directoryName>"
This worked fine for directories.
However the counterpart for creating files does not work:
FileManager.default.createFile(atPath: "Users/me/random/project3/something.txt", contents: someString.data(using: .utf8)) // always returns false
I have checked out other stack overflow threads on creating files. Everything I found was about creating files in the .documentDirectory or it was outdated.
Eg: How to create text file for writing
Create empty file swift
Read and write a String from text file
TLDR: How do I create text files/files with random extensions at arbitrary locations on the pc given I have the absolute path?
I would also be grateful if someone could explain why all tutorials available on this matter are about the document directory. Why is it so significant?
Thank you!
This is most likely because your app is sandboxed.
Go to your project settings, select the macOS target and go to "Signing and Capabilities". You should see something like this:
If your app needs to create files in arbitrary locations, you must remove the app sandbox by clicking on the "x" on the top right. Note that this will cause your app to be rejected from the Mac App Store.
If you want to upload your app to the Mac App Store, then you must keep the sandbox, and write to "user selected locations" only. Select "Read/Write" rather than "Read Only" next to "User Selected File". (Although it says "File", this includes directories too) This allows you to write to locations chosen by a user using something like an NSSavePanel.

Load an asset from outside a 'resources' folder

I'd like to load "dynamic" assets (in particular, FBX files) that are located outside of a "Resources" folder so that the user can add/remove/modify files with no need to re-build the project.
To make those files easily accessible for the user in different kinds of devices (target platforms are desktop/android/ios), it seems reasonable to use either Application.persistentDataPath or Application.streamingAssetsPath.
Now the problem is: How do I load them (as proper Unity objects) ?
This obviously can't be done via Resources.Load() as they are outside a "Resources" folder.
Using WWW, retrieving the files should be easy and it could actually work for some types of files (e.g. textures, see http://docs.unity3d.com/ScriptReference/WWW-texture.html) but I cannot see a way to load something else.
The idea is that I'd like to be able to use those loaded objects like so:
(GameObject) Instantiate(loadedObject, new Vector3(0, 0, 0), Quaternion.identity);
I would really appreciate your help (preferrably with a solution that does not require a "pro license").
I know two options that you can try to use in this case:
Build an asset bundle for each dynamic asset you want to load, but this seems not really what you desire, because the user still have to use Unity3d to generate the asset bundle.
Use an custom 3d model loader, like described in this post, seems the best fit for your case.

Play Framework - only getting file content from Ok.sendFile(file)

My users need to download a file when hitting a certain controller in my Play application. I thought this would do the trick:
def downloadFile = Action {
Ok.sendFile(new File("example.zip"))
}
But it seems to only give the actual content of the file instead of downloading the file. Could anybody tell me what I'm doing wrong?
Thanks
Try this instead:
def index = Action {
Ok.sendFile(
content = new java.io.File("/tmp/fileToServe.pdf"),
fileName = _ => "termsOfService.pdf"
)
}
Its from the documentation itself.
Now you don’t have to specify a file name since the web browser will
not try to download it, but will just display the file content in the
web browser window. This is useful for content types supported
natively by the web browser, such as text, HTML or images.
See this: https://www.playframework.com/documentation/2.0/ScalaStream
Turns out the REST client we are using is automatically converting the file straight into its content instead of letting us download the file. Hitting it from a normal browser works as intended.

how to work with html resources in a blackberry-eclipse application?

I am a beginner in blackberry app development.
I had installed the blackberry plugin in eclipse successfully. my first app needs to load html files from a folder I had created named "assets" inside my project. so how can I load the html file "assets/index.html"?
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
Ramadanclass theApp = new Ramadanclass();
theApp.enterEventDispatcher();
}
mainscreen:
public final class Ramadan extends MainScreen
{
public Ramadan()
{
setTitle("Ramadan");
}
}
Edit: what I tried:
BrowserField myBrowserField = new BrowserField();
add(myBrowserField);
myBrowserField.requestContent("assets/index.html");
which had this error
no navigation request handler for assets/index.html
As the link that #Turdnugget supplied says:
The BrowserField class does not access resources using a folder structure. The BrowserField class displays the first resource found that matches the specifed file name.
That means that even if you put index.html into an assets folder, the BlackBerry build process and/or runtime will flatten out (i.e. ignore) your directory structure and therefore you'll have to load the html file with:
myBrowserField.requestContent("local:///index.html");
As you can imagine, this works ok if you only have one file named index.html in your project, but if you have two files with that name, in different folders, the browser field may not load the one you want. I have projects where I do have different versions of html files that are loaded depending on the device, or the version of the app (full vs. free). So, I like to be able to store my html resources in different folders.
To accomplish this, I've used the following code with BrowserField:
browser = new BrowserField();
add(browser);
InputStream content = getClass().getResourceAsStream("/resourcesWeb/index.html");
try {
byte[] html = IOUtilities.streamToBytes(content);
browser.displayContent(new String(html), "http://localhost");
} catch (IOException e) {
e.printStackTrace();
}
For this to work, I store the file index.html in a folder named resourcesWeb (name it whatever you like), and that folder is located inside the top-level res folder.
Note that this is not Android. There is not normally an assets directory. When you use the BlackBerry Eclipse plug-in to create a new project, it creates a src and a res folder for you. I would use that. Create a new project if you need to. Then, keep all non-source code resources (html, xml, images, etc.) somewhere under the res folder.
If you didn't create your project this way, you may find that your app is built without including the assets folder you created. You can probably fix this by right clicking on your project in Eclipse, and selecting Properties. You can add the assets folder to the Build Path using this dialog:
and notice that assets should show up in the Order and Export tab as well.
However, I think it's easiest just to create and use projects the way the BlackBerry Eclipse plugin wants to. This will also make your projects more familiar to other BlackBerry Java developers that see your code. In my opinion, you should save Android naming conventions for Android projects.

iPhone save game state

I'm implementing a save game feature to my game, so I'm looking for the best way to store the saved game.
Since I'm using C++ everywhere it's possible, I can't use NSKeyedArchiver to store the state of the game.
I am thinking in saving the state manually in a file, so I have a questions about that:
In which subfolder of apphome should I save that file? Looking into the iOS Programming Guide I didn't found a folder that perfeclty fit that need. Should I create a custom subfolder in Library or am I missing something?
Thanks in advance
Write the file into the Documents directory (NSDocumentDirectory). Thats the path to save your stuff, you can alter its content in any way by eg. adding subfolders for your save games or whatever.
DON'T save it inside the library folder!