UnityMol  0.9.6-875
UnityMol viewer / In developement
FileBro.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine.UI;
5 using System.IO;
6 using System;
7 
8 public class FileBro : MonoBehaviour
9 {
10 
11  public string fileToOpen;
12  public Button fileButton;
13  public Button foldButton;
14  public GameObject fileBro;
15  public GameObject folderBro;
16  private string currentPath;
17  private float lastClicked = 0.0f;
18  private string lastPath = null;
19 
20  void Awake ()
21  {
22  // Setting the curent path to the absolute path of current project
23  currentPath = Application.dataPath + "/../samples/";
24  }
25  // Use this for initialization
26  void Start ()
27  {
28  // Create the initial folder content
29  UpdateBro (currentPath);
30  }
31 
36  void UpdateBro (string pathToGo)
37  {
38  // Change the current path to the path of clik on folder
39  currentPath = pathToGo;
40  Debug.Log (currentPath);
41 
42  DirectoryInfo dir = new DirectoryInfo (pathToGo);
43  FileInfo[] info = dir.GetFiles ("*.*");
44  DirectoryInfo[] infod = dir.GetDirectories ("*");
45 
46  // Destroy buttons generated by last call
47  foreach (Transform child in fileBro.transform) {
48  GameObject.Destroy (child.gameObject);
49  }
50 
51 
52  // Generate "folder" buttons
53  foreach (DirectoryInfo f in infod) {
54  Button fbButton = Instantiate (foldButton) as Button;
55  //Set name
56  fbButton.GetComponentInChildren<Text> ().text = f.Name;
57  //Assign to parent
58  fbButton.transform.SetParent (fileBro.transform, false);
59  //Capturing the value instead of the loop iterator (fixed in 5 but not 4.6)
60  string captured = f.FullName;
61  //Adding the listener
62  fbButton.onClick.AddListener (() => (UpdateBro (captured)));
63  }
64 
65 
66 
67  //Generate "file" buttons
68  foreach (FileInfo f in info) {
69  Button fbButton = Instantiate (fileButton) as Button;
70  //Set name
71  fbButton.GetComponentInChildren<Text> ().text = f.Name;
72  //Assign to parent
73  fbButton.transform.SetParent (fileBro.transform, false);
74  //Capturing the value instead of the loop iterator (fixed in 5 but not 4.6)
75  string captured = f.FullName;
76  //Adding the listener
77  fbButton.onClick.AddListener (() => (SetFileToOpen (captured)));
78  }
79 
80  foreach (Transform child in folderBro.transform) {
81  GameObject.Destroy (child.gameObject);
82  }
84  }
85 
90  {
91  DirectoryInfo dir = new DirectoryInfo (currentPath);
92  List<DirectoryInfo> directories = dir.Split ();
93  foreach (DirectoryInfo dirf in directories) {
94  Button fbButton = Instantiate (foldButton) as Button;
95  //Set name
96  fbButton.GetComponentInChildren<Text> ().text = dirf.Name;
97  //Assign to parent
98  fbButton.transform.SetParent (folderBro.transform, false);
99  //Capturing the value instead of the loop iterator
100  string captured = dirf.FullName;
101  //Adding the listener
102  fbButton.onClick.AddListener (() => (UpdateBro (captured)));
103 
104  }
105  }
106 
107  void SetFileToOpen (string fileSelected)
108  {
109  fileToOpen = fileSelected;
110  if(lastPath != null && lastPath == fileSelected && Time.time - lastClicked < 0.5f){
111  //TODO Change this ugly call
112  GameObject.Find("UIManager").GetComponent<LoadGUI>().OpenFileCallback();
113  }
114  lastClicked = Time.time;
115  lastPath = fileSelected;
116  }
117 
121  public void GoToParent ()
122  {
123  Debug.Log (currentPath);
124  UpdateBro (Directory.GetParent (currentPath).FullName);
125  }
126 
127 
128 }
void UpdateBro(string pathToGo)
Change current path and updates the content of the browser.
Definition: FileBro.cs:36
Button fileButton
Definition: FileBro.cs:12
void SetFileToOpen(string fileSelected)
Definition: FileBro.cs:107
string currentPath
Definition: FileBro.cs:16
void GoToParent()
Move to the parent directory and display it
Definition: FileBro.cs:121
Button foldButton
Definition: FileBro.cs:13
GameObject fileBro
Definition: FileBro.cs:14
float lastClicked
Definition: FileBro.cs:17
void UpdateParentsFolderPanel()
Update the panel which contain parents folder
Definition: FileBro.cs:89
string fileToOpen
Definition: FileBro.cs:11
string lastPath
Definition: FileBro.cs:18
void Awake()
Definition: FileBro.cs:20
GameObject folderBro
Definition: FileBro.cs:15
void Start()
Definition: FileBro.cs:26