[C#] help me run AngryAnt fileBrowser

Hello,

I looked around and seems like not only me is having the problem

I used this script

Also since it was throwing errors, I replaced

directorySelection = BehaveLibrary.Resources.SelectList( directoryInfo.GetDirectories(), null ) as DirectoryInfo;

with

directorySelection = SelectList( directoryInfo.GetDirectories(), null,null ) as DirectoryInfo;

And similarly

 fileSelection = BehaveLibrary.Resources.SelectList( directoryInfo.GetFiles(), null ) as FileInfo;

with

fileSelection = SelectList( directoryInfo.GetFiles(), null,null ) as FileInfo;

By adding the SelectList code from here
http://www.unifycommunity.com/wiki/index.php?title=SelectList

Unfortunately I am not good in C# right now, so I cannot go further. I need to declare some variables like

 ref location, ref directoryScroll, ref fileScroll

But I cannot figure out how. The full code I have now is:

using UnityEngine;
using System.IO;
using System.Collections;
//using BehaveLibrary;


public class FileBrowser1 : MonoBehaviour {

            public static object SelectList( ICollection list, object selected, GUIStyle defaultStyle, GUIStyle selectedStyle )

        {         
            foreach( object item in list )
            {
                if( GUILayout.Button( item.ToString(), ( selected == item ) ? selectedStyle : defaultStyle ) )
                {
                    if( selected == item )
                    // Clicked an already selected item. Deselect.
                    {
                        selected = null;
                    }
                    else
                    {
                        selected = item;
                    }
                }
            }
       
            return selected;
        }
       
       
       
        public delegate bool OnListItemGUI( object item, bool selected, ICollection list );
       
       
       
        public static object SelectList( ICollection list, object selected, OnListItemGUI itemHandler )
        {
            ArrayList itemList;
           
            itemList = new ArrayList( list );
           
            foreach( object item in itemList )
            {
                if( itemHandler( item, item == selected, list ) )
                {
                    selected = item;
                }
                else if( selected == item )
                // If we *were* selected, but aren't any more then deselect
                {
                    selected = null;
                }
            }
       
            return selected;
        }
    
    public static bool FileBrowser( ref string location, ref Vector2 directoryScroll, ref Vector2 fileScroll )

{
    bool complete;
    DirectoryInfo directoryInfo;
    DirectoryInfo directorySelection;
    FileInfo fileSelection;
    int contentWidth;
   
   
    // Our return state - altered by the "Select" button
    complete = false;
   
    // Get the directory info of the current location
    fileSelection = new FileInfo( location );
    if( (fileSelection.Attributes  FileAttributes.Directory) == FileAttributes.Directory )
    {
        directoryInfo = new DirectoryInfo( location );
    }
    else
    {
        directoryInfo = fileSelection.Directory;
    }
   

    if( location != "/"  GUI.Button( new Rect( 10, 20, 410, 20 ), "Up one level" ) )
    {
        directoryInfo = directoryInfo.Parent;
        location = directoryInfo.FullName;
    }

   
    // Handle the directories list
    GUILayout.BeginArea( new Rect( 10, 40, 200, 300 ) );
        GUILayout.Label( "Directories:" );
        directoryScroll = GUILayout.BeginScrollView( directoryScroll );
          //  directorySelection = BehaveLibrary.Resources.SelectList( directoryInfo.GetDirectories(), null ) as DirectoryInfo;
                directorySelection = SelectList( directoryInfo.GetDirectories(), null,null ) as DirectoryInfo;
        GUILayout.EndScrollView();
    GUILayout.EndArea();
   
    if( directorySelection != null )
    // If a directory was selected, jump there
    {
        location = directorySelection.FullName;
    }
   
   
    // Handle the files list
    GUILayout.BeginArea( new Rect( 220, 40, 200, 300 ) );
        GUILayout.Label( "Files:" );
        fileScroll = GUILayout.BeginScrollView( fileScroll );
            //fileSelection = BehaveLibrary.Resources.SelectList( directoryInfo.GetFiles(), null ) as FileInfo;
            fileSelection = SelectList( directoryInfo.GetFiles(), null,null ) as FileInfo;
        GUILayout.EndScrollView();
    GUILayout.EndArea();
   
    if( fileSelection != null )
    // If a file was selected, update our location to it
    {
        location = fileSelection.FullName;
    }


    // The manual location box and the select button
    GUILayout.BeginArea( new Rect( 10, 350, 410, 20 ) );
    GUILayout.BeginHorizontal();       
        location = GUILayout.TextArea( location );

        contentWidth = ( int )GUI.skin.GetStyle( "Button" ).CalcSize( new GUIContent( "Select" ) ).x;
        if( GUILayout.Button( "Select", GUILayout.Width( contentWidth ) ) )
        {
            complete = true;
        }
    GUILayout.EndHorizontal();
    GUILayout.EndArea();


    return complete;
}
    

    public void FileBrowserWindow( int idx )

    {
        if( FileBrowser( ref location, ref directoryScroll, ref fileScroll ) )
        {
            fileBrowser = false;
        }
    }

    public void OnGUI()
    {
        if( fileBrowser )
        {
            GUI.Window( 0, new Rect( ( Screen.width - 430 ) / 2, ( Screen.height - 380 ) / 2, 430, 380 ), FileBrowserWindow, "Browse" );
            return;
        }
    }


    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

Did you get this working? If so how did u do it?

Thanks!

I’d suggest UniFileBrowser if you don’t mind paying a few bucks.

–Eric

Pure perfectionism at its best, Eric has the coolest tools.

Eric stuff is definitely great, but I prefered learning scripting at that time. I actually improved the script I found at wiki

Nikolay116, do you mind sharing the sulution? It would be a big help for me!

Thanks