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:
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?
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
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;
}
}
}