- Home /
Equivalent of OpenFilePanel without using UnityEditor?
I used OpenFilePanel
to allow players to select text files they have created and make levels from that, but have since discovered the issue with using this. See my code below:
if (GUI.Button(new Rect(210, 260, 100, 40), "Select file")) {
filePath = EditorUtility.OpenFilePanel("level",Application.streamingAssetsPath,"txt");
if(filePath.Length != 0) {
selectedFile = true;
}
}
As you can see I simply let users select a file from the Panel which pops up, and then save that path which is used later to create the level accordingly, and it works fine, but using UnityEditor
I can't compile this. Is there an alternative which does the same?
Answer by niall1111 · Aug 13, 2014 at 01:19 PM
It's quite a bit more difficult than doing it in the editor, and sort of ugly, but I did it starting from this thing: http://wiki.unity3d.com/index.php?title=ImprovedFileBrowser
A lot of faff for a file browser when the editor has such a simple way of doing it. Anyway, if anyone sees this and is looking for a quicker way, there's a free asset here: https://www.assetstore.unity3d.com/en/#!/content/18308
Answer by gkngkc · Jan 11, 2017 at 08:43 AM
You can use this simple native file wrapper standalone builds;
Answer by DanielSRRosky1999 · Nov 25, 2019 at 05:08 AM
Download and import this Unity Asset Store: https://assetstore.unity.com/packages/tools/gui/runtime-file-browser-113006
This plugin is for Mac, Windows and Android
Answer by viju · Jan 27, 2016 at 03:17 PM
I was finding difficulty to do it using OpenFilePanel for my simple requirement of opening some files. So, I did a bit of lateral thinking to do it this way.
Create a Panel and a Dropdown with any name. I have named the dropdown as "File Selection". Please see the image attached.
Wrote a small code to load all the files from "Application.persistentDataPath".
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class BehaviorScript : MonoBehaviour
{
// Use this for initialization
void Start ()
{
DirectoryInfo directoryInfo = new DirectoryInfo (Application.persistentDataPath);
FileInfo[] fileInfo = directoryInfo.GetFiles ("*.*", SearchOption.AllDirectories);
GameObject.Find ("File Selection").GetComponent<Dropdown> ().options.Clear ();
foreach (FileInfo file in fileInfo) {
Dropdown.OptionData optionData = new Dropdown.OptionData (file.Name);
GameObject.Find ("File Selection").GetComponent<Dropdown> ().options.Add (optionData);
GameObject.Find ("File Selection").GetComponent<Dropdown> ().value = 1;
}
}
}
Answer by coolraiman · Jan 27, 2016 at 06:12 PM
you cant use it in a build
in the editor it work but it cant get in a build. you may need to make your own custom file selector/explorer
also, if you want to build without removing code everytime do it like how i did
#if UNITY_EDITOR
using UnityEditor;
#endif
//only in editor
#if UNITY_EDITOR
//prevent multiple dialogs
public bool dialogIsOpen = false;
protected virtual void Update()
{
if(rend == null)
{
rend = GetComponent<Renderer>();
}
//make sure to only show dialog in editor mode while not playing
if(!EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isCompiling && !EditorApplication.isPaused)
{
//get the colliders
MeshCollider box = GetComponent<MeshCollider>();
BoxCollider poly = GetComponent<BoxCollider>();
//if no dialogs && no collider && currently a sprite
if(!dialogIsOpen && box == null && poly == null && rend.material.mainTexture.name != null)
{
setCollider();
}
}
}
//show a dialog ans ask the user wich collider to use
private void setCollider()
{
dialogIsOpen = true;
//destroy current collider
DestroyImmediate(GetComponent<MeshCollider>());
DestroyImmediate(GetComponent<BoxCollider>());
//show dialog box
if(UnityEditor.EditorUtility.DisplayDialog("Choose a Component", "You need one of the two component for your button to work","MeshCollider", "BoxCollider"))
{
gameObject.AddComponent<MeshCollider>();
}
else
{
gameObject.AddComponent<BoxCollider>();
}
dialogIsOpen = false;
}
#endif
i used it with [ExecuteInEditMode] to make some kind of conditionnal [RequireComponent] to make sure the game designer dont forget some component or to prevent to duplicate prefabs with just a different collider
Your answer
Follow this Question
Related Questions
Working with Unity Editor 1 Answer
Custom Unity Editor Issue 1 Answer
Unity Editor script arrows to display a variable 2 Answers