Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

File Browser

Discussion in 'Immediate Mode GUI (IMGUI)' started by Daniel_Brauer, Apr 6, 2011.

  1. Votapil

    Votapil

    Joined:
    Jun 26, 2016
    Posts:
    1
    Yhe, can you reload this, pls!)
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,654
    Oh sorry, didn't mean to delete it off my dropbox, I'll try to find the package and reupload it tonight, hang in there!
     
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,654
    EDIT: Posted code below

    Well fellas, I hate to say it but I can't find the unitypackage I made for this example :(

    Worse yet, the project I used to test this out on that had the working scripts I've butchered while testing out other ways to do it, and have since deleted the files :/

    If somebody out there so happened to download my variant and could share it back here that'd be real helpful for some users I'm sure! Otherwise, you'll have to tear apart the above examples and rework them for the latest unity version - but I can tell you it's doable cuz.... I did it lol
     
    Last edited: Jan 26, 2017
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,654
    For anybody who comes along needing it - here is what your "FileBrowser.cs" would look like after the fix:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.IO;
    4. using System.Collections.Generic;
    5.  
    6. /*
    7.     File browser for selecting files or folders at runtime.
    8. */
    9.  
    10. public enum FileBrowserType
    11. {
    12.     File,
    13.     Directory
    14. }
    15.  
    16. public class FileBrowser
    17. {
    18.  
    19.     // Added to disable directory (folder) browsing
    20.     public bool currentDirOnly = false;
    21.     // Added to allow very small number of files
    22.     private float minScrollHeight = 270.0f;
    23.  
    24.     // Called when the user clicks cancel or select
    25.     public delegate void FinishedCallback(string path);
    26.     // Defaults to working directory
    27.     public string CurrentDirectory
    28.     {
    29.         get
    30.         {
    31.             return m_currentDirectory;
    32.         }
    33.         set
    34.         {
    35.             SetNewDirectory(value);
    36.             SwitchDirectoryNow();
    37.         }
    38.     }
    39.     protected string m_currentDirectory;
    40.     // Optional pattern for filtering selectable files/folders. See:
    41.     // http://msdn.microsoft.com/en-us/library/wz42302f(v=VS.90).aspx
    42.     // and
    43.     // http://msdn.microsoft.com/en-us/library/6ff71z1w(v=VS.90).aspx
    44.     public string SelectionPattern
    45.     {
    46.         get
    47.         {
    48.             return m_filePattern;
    49.         }
    50.         set
    51.         {
    52.             m_filePattern = value;
    53.             ReadDirectoryContents();
    54.         }
    55.     }
    56.     protected string m_filePattern;
    57.  
    58.     // Optional image for directories
    59.     public Texture2D DirectoryImage
    60.     {
    61.         get
    62.         {
    63.             return m_directoryImage;
    64.         }
    65.         set
    66.         {
    67.             m_directoryImage = value;
    68.             BuildContent();
    69.         }
    70.     }
    71.     protected Texture2D m_directoryImage;
    72.  
    73.     // Optional image for files
    74.     public Texture2D FileImage
    75.     {
    76.         get
    77.         {
    78.             return m_fileImage;
    79.         }
    80.         set
    81.         {
    82.             m_fileImage = value;
    83.             BuildContent();
    84.         }
    85.     }
    86.     protected Texture2D m_fileImage;
    87.  
    88.     // Browser type. Defaults to File, but can be set to Folder
    89.     public FileBrowserType BrowserType
    90.     {
    91.         get
    92.         {
    93.             return m_browserType;
    94.         }
    95.         set
    96.         {
    97.             m_browserType = value;
    98.             ReadDirectoryContents();
    99.         }
    100.     }
    101.     protected FileBrowserType m_browserType;
    102.     protected string m_newDirectory;
    103.     protected string[] m_currentDirectoryParts;
    104.  
    105.     protected string[] m_files;
    106.     protected GUIContent[] m_filesWithImages;
    107.     protected int m_selectedFile;
    108.  
    109.     protected string[] m_nonMatchingFiles;
    110.     protected GUIContent[] m_nonMatchingFilesWithImages;
    111.     protected int m_selectedNonMatchingDirectory;
    112.  
    113.     protected string[] m_directories;
    114.     protected GUIContent[] m_directoriesWithImages;
    115.     protected int m_selectedDirectory;
    116.  
    117.     protected string[] m_nonMatchingDirectories;
    118.     protected GUIContent[] m_nonMatchingDirectoriesWithImages;
    119.  
    120.     protected bool m_currentDirectoryMatches;
    121.  
    122.     protected GUIStyle CentredText
    123.     {
    124.         get
    125.         {
    126.             if (m_centredText == null)
    127.             {
    128.                 m_centredText = new GUIStyle(GUI.skin.label);
    129.                 m_centredText.alignment = TextAnchor.MiddleLeft;
    130.                 m_centredText.fixedHeight = GUI.skin.button.fixedHeight;
    131.             }
    132.             return m_centredText;
    133.         }
    134.     }
    135.     protected GUIStyle m_centredText;
    136.  
    137.     protected string m_name;
    138.     protected Rect m_screenRect;
    139.  
    140.     protected Vector2 m_scrollPosition;
    141.  
    142.     protected FinishedCallback m_callback;
    143.  
    144.     // Browsers need at least a rect, name and callback
    145.     public FileBrowser(Rect screenRect, string name, FinishedCallback callback, bool noBrowse)
    146.     {
    147.         m_name = name;
    148.         m_screenRect = screenRect;
    149.         m_browserType = FileBrowserType.File;
    150.         m_callback = callback;
    151.         currentDirOnly = noBrowse;
    152.         SetNewDirectory(Application.persistentDataPath);
    153.         //        SetNewDirectory(Directory.GetCurrentDirectory());
    154.         SwitchDirectoryNow();
    155.     }
    156.  
    157.     protected void SetNewDirectory(string directory)
    158.     {
    159.         m_newDirectory = directory;
    160.     }
    161.  
    162.     protected void SwitchDirectoryNow()
    163.     {
    164.         if (m_newDirectory == null || m_currentDirectory == m_newDirectory)
    165.         {
    166.             return;
    167.         }
    168.         m_currentDirectory = m_newDirectory;
    169.         m_scrollPosition = Vector2.zero;
    170.         m_selectedDirectory = m_selectedNonMatchingDirectory = m_selectedFile = -1;
    171.         ReadDirectoryContents();
    172.     }
    173.  
    174.     protected void ReadDirectoryContents()
    175.     {
    176.         if (m_currentDirectory == "/")
    177.         {
    178.             m_currentDirectoryParts = new string[] { "" };
    179.             m_currentDirectoryMatches = false;
    180.         }
    181.         else {
    182.             m_currentDirectoryParts = m_currentDirectory.Split(Path.DirectorySeparatorChar);
    183.             Debug.Log("GetDirectoryName returns: " + Path.GetDirectoryName(m_currentDirectory));
    184.             if (SelectionPattern != null)
    185.             {
    186.                 string path = m_currentDirectory;
    187.                 if (path != "C:\\")
    188.                 {
    189.                     // If Path.GetDirectoryName gets a C:\ it returns a null??
    190.                     path = Path.GetDirectoryName(m_currentDirectory);
    191.                 }
    192.  
    193.                 string[] generation = Directory.GetDirectories(
    194.                     path,
    195.                     SelectionPattern
    196.                 );
    197.                 m_currentDirectoryMatches = Array.IndexOf(generation, m_currentDirectory) >= 0;
    198.             }
    199.             else {
    200.                 m_currentDirectoryMatches = false;
    201.             }
    202.         }
    203.  
    204.         if (BrowserType == FileBrowserType.File || SelectionPattern == null)
    205.         {
    206.             //Debug.Log("Looking for Type.File: " + SelectionPattern);
    207.             m_directories = Directory.GetDirectories(m_currentDirectory);
    208.             m_nonMatchingDirectories = new string[0];
    209.         }
    210.         else {
    211.             m_directories = Directory.GetDirectories(m_currentDirectory, SelectionPattern);
    212.             var nonMatchingDirectories = new List<string>();
    213.             foreach (string directoryPath in Directory.GetDirectories(m_currentDirectory))
    214.             {
    215.                 if (Array.IndexOf(m_directories, directoryPath) < 0)
    216.                 {
    217.                     nonMatchingDirectories.Add(directoryPath);
    218.                 }
    219.             }
    220.             m_nonMatchingDirectories = nonMatchingDirectories.ToArray();
    221.             for (int i = 0; i < m_nonMatchingDirectories.Length; ++i)
    222.             {
    223.                 int lastSeparator = m_nonMatchingDirectories[i].LastIndexOf(Path.DirectorySeparatorChar);
    224.                 m_nonMatchingDirectories[i] = m_nonMatchingDirectories[i].Substring(lastSeparator + 1);
    225.             }
    226.             Array.Sort(m_nonMatchingDirectories);
    227.         }
    228.  
    229.         for (int i = 0; i < m_directories.Length; ++i)
    230.         {
    231.             m_directories[i] = m_directories[i].Substring(m_directories[i].LastIndexOf(Path.DirectorySeparatorChar) + 1);
    232.         }
    233.  
    234.         if (BrowserType == FileBrowserType.Directory || SelectionPattern == null)
    235.         {
    236.             m_files = Directory.GetFiles(m_currentDirectory);
    237.             m_nonMatchingFiles = new string[0];
    238.         }
    239.         else {
    240.             m_files = Directory.GetFiles(m_currentDirectory, SelectionPattern);
    241.             //Debug.Log("m_files count: " + m_files.Length);
    242.             var nonMatchingFiles = new List<string>();
    243.             foreach (string filePath in Directory.GetFiles(m_currentDirectory))
    244.             {
    245.                 if (Array.IndexOf(m_files, filePath) < 0)
    246.                 {
    247.                     nonMatchingFiles.Add(filePath);
    248.                 }
    249.             }
    250.             m_nonMatchingFiles = nonMatchingFiles.ToArray();
    251.             for (int i = 0; i < m_nonMatchingFiles.Length; ++i)
    252.             {
    253.                 m_nonMatchingFiles[i] = Path.GetFileName(m_nonMatchingFiles[i]);
    254.             }
    255.             Array.Sort(m_nonMatchingFiles);
    256.         }
    257.         for (int i = 0; i < m_files.Length; ++i)
    258.         {
    259.             m_files[i] = Path.GetFileName(m_files[i]);
    260.         }
    261.         Array.Sort(m_files);
    262.         BuildContent();
    263.         m_newDirectory = null;
    264.     }
    265.  
    266.     protected void BuildContent()
    267.     {
    268.         m_directoriesWithImages = new GUIContent[m_directories.Length];
    269.         for (int i = 0; i < m_directoriesWithImages.Length; ++i)
    270.         {
    271.             m_directoriesWithImages[i] = new GUIContent(m_directories[i], DirectoryImage);
    272.         }
    273.         m_nonMatchingDirectoriesWithImages = new GUIContent[m_nonMatchingDirectories.Length];
    274.         for (int i = 0; i < m_nonMatchingDirectoriesWithImages.Length; ++i)
    275.         {
    276.             m_nonMatchingDirectoriesWithImages[i] = new GUIContent(m_nonMatchingDirectories[i], DirectoryImage);
    277.         }
    278.         m_filesWithImages = new GUIContent[m_files.Length];
    279.         for (int i = 0; i < m_filesWithImages.Length; ++i)
    280.         {
    281.             m_filesWithImages[i] = new GUIContent(m_files[i], FileImage);
    282.         }
    283.         m_nonMatchingFilesWithImages = new GUIContent[m_nonMatchingFiles.Length];
    284.         for (int i = 0; i < m_nonMatchingFilesWithImages.Length; ++i)
    285.         {
    286.             m_nonMatchingFilesWithImages[i] = new GUIContent(m_nonMatchingFiles[i], FileImage);
    287.         }
    288.     }
    289.  
    290.     public void OnGUI()
    291.     {
    292.         GUILayout.BeginArea(
    293.             m_screenRect,
    294.             "",
    295.             GUI.skin.window
    296.         );
    297.         GUILayout.BeginVertical();
    298.         GUILayout.FlexibleSpace();
    299.         if (m_files.Length > 0)
    300.             GUILayout.Label(m_name, CentredText);
    301.         else
    302.             GUILayout.Label("No Files Found", CentredText);
    303.  
    304.         GUILayout.EndVertical();
    305.         if (!currentDirOnly)
    306.         {
    307.             GUILayout.BeginHorizontal();
    308.             for (int parentIndex = 0; parentIndex < m_currentDirectoryParts.Length; ++parentIndex)
    309.             {
    310.                 if (parentIndex == m_currentDirectoryParts.Length - 1)
    311.                 {
    312.                     GUILayout.Label(m_currentDirectoryParts[parentIndex], CentredText);
    313.                 }
    314.                 else if (GUILayout.Button(m_currentDirectoryParts[parentIndex]))
    315.                 {
    316.                     string parentDirectoryName = m_currentDirectory;
    317.                     for (int i = m_currentDirectoryParts.Length - 1; i > parentIndex; --i)
    318.                     {
    319.                         parentDirectoryName = Path.GetDirectoryName(parentDirectoryName);
    320.                     }
    321.                     SetNewDirectory(parentDirectoryName);
    322.                 }
    323.             }
    324.             GUILayout.FlexibleSpace();
    325.             GUILayout.EndHorizontal();
    326.         }
    327.         m_scrollPosition = GUILayout.BeginScrollView(
    328.                 m_scrollPosition,
    329.                 false,
    330.                 true,
    331.                 GUI.skin.horizontalScrollbar,
    332.                 GUI.skin.verticalScrollbar,
    333.                 GUI.skin.box, GUILayout.Height(minScrollHeight)
    334.             );
    335.         if (!currentDirOnly)
    336.         {
    337.             m_selectedDirectory = GUILayoutx.SelectionList(
    338.                 m_selectedDirectory,
    339.                 m_directoriesWithImages,
    340.                 DirectoryDoubleClickCallback
    341.             );
    342.             if (m_selectedDirectory > -1)
    343.             {
    344.                 m_selectedFile = m_selectedNonMatchingDirectory = -1;
    345.             }
    346.             m_selectedNonMatchingDirectory = GUILayoutx.SelectionList(
    347.                 m_selectedNonMatchingDirectory,
    348.                 m_nonMatchingDirectoriesWithImages,
    349.                 NonMatchingDirectoryDoubleClickCallback
    350.             );
    351.             if (m_selectedNonMatchingDirectory > -1)
    352.             {
    353.                 m_selectedDirectory = m_selectedFile = -1;
    354.             }
    355.         }
    356.         GUI.enabled = BrowserType == FileBrowserType.File;
    357.         m_selectedFile = GUILayoutx.SelectionList(
    358.             m_selectedFile,
    359.             m_filesWithImages,
    360.             FileDoubleClickCallback
    361.         );
    362.         GUI.enabled = true;
    363.         if (m_selectedFile > -1)
    364.         {
    365.             m_selectedDirectory = m_selectedNonMatchingDirectory = -1;
    366.         }
    367.         if (SelectionPattern == null)
    368.         {
    369.             GUI.enabled = false;
    370.             GUILayoutx.SelectionList(
    371.                 -1,
    372.                 m_nonMatchingFilesWithImages
    373.             );
    374.             GUI.enabled = true;
    375.         }
    376.         GUILayout.EndScrollView();
    377.         GUILayout.BeginHorizontal();
    378.         GUILayout.FlexibleSpace();
    379.         if (GUILayout.Button("Cancel", GUILayout.Width(150)))
    380.         {
    381.             m_callback(null);
    382.         }
    383.         if (BrowserType == FileBrowserType.File)
    384.         {
    385.             GUI.enabled = m_selectedFile > -1;
    386.         }
    387.         else {
    388.             if (SelectionPattern == null)
    389.             {
    390.                 GUI.enabled = m_selectedDirectory > -1;
    391.             }
    392.             else {
    393.                 GUI.enabled = m_selectedDirectory > -1 ||
    394.                                 (
    395.                                     m_currentDirectoryMatches &&
    396.                                     m_selectedNonMatchingDirectory == -1 &&
    397.                                     m_selectedFile == -1
    398.                                 );
    399.             }
    400.         }
    401.         if (GUILayout.Button("Select", GUILayout.Width(150)))
    402.         {
    403.             if (BrowserType == FileBrowserType.File)
    404.             {
    405.                 m_callback(Path.Combine(m_currentDirectory, m_files[m_selectedFile]));
    406.             }
    407.             else {
    408.                 if (m_selectedDirectory > -1)
    409.                 {
    410.                     m_callback(Path.Combine(m_currentDirectory, m_directories[m_selectedDirectory]));
    411.                 }
    412.                 else {
    413.                     m_callback(m_currentDirectory);
    414.                 }
    415.             }
    416.         }
    417.         GUI.enabled = true;
    418.         GUILayout.EndHorizontal();
    419.         GUILayout.EndArea();
    420.  
    421.         if (Event.current.type == EventType.Repaint)
    422.         {
    423.             SwitchDirectoryNow();
    424.         }
    425.     }
    426.  
    427.     protected void FileDoubleClickCallback(int i)
    428.     {
    429.         if (BrowserType == FileBrowserType.File)
    430.         {
    431.             m_callback(Path.Combine(m_currentDirectory, m_files[i]));
    432.         }
    433.     }
    434.  
    435.     protected void DirectoryDoubleClickCallback(int i)
    436.     {
    437.         SetNewDirectory(Path.Combine(m_currentDirectory, m_directories[i]));
    438.     }
    439.  
    440.     protected void NonMatchingDirectoryDoubleClickCallback(int i)
    441.     {
    442.         SetNewDirectory(Path.Combine(m_currentDirectory, m_nonMatchingDirectories[i]));
    443.     }
    444.  
    445. }
     
  5. lokihornsby

    lokihornsby

    Joined:
    Jun 24, 2020
    Posts:
    48
    Btw to anyone who needs it this unitypackage seems to work - i haven't tested in build yet but to anyone interested in a file browser this might help you

    To note: i reccommend setting m_textPath to be public instead of protected in SavedFileFinder.cs

    (This is copied from a previous user but i can't find the link again)
     

    Attached Files: