Skip to content

Instantly share code, notes, and snippets.

@tm8r
Created May 13, 2016 11:32
Show Gist options
  • Save tm8r/c7b3520a2db62727127075136d7536ed to your computer and use it in GitHub Desktop.
Save tm8r/c7b3520a2db62727127075136d7536ed to your computer and use it in GitHub Desktop.
MenuPresenter
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using UniLinq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace Viewer
{
public class MenuPresenter : MonoBehaviour
{
Dropdown fileDropDown;
InputField directoryInput;
Dictionary<string, string> fileDictionary;
List<Dropdown.OptionData> defaultOption;
void Start ()
{
fileDropDown = GameObject.Find ("FileDropdown").GetComponent<Dropdown> ();
defaultOption = fileDropDown.options;
// DropDownを監視
fileDropDown.ObserveEveryValueChanged (_ => _.value)
.Subscribe (index => {
var value = fileDropDown.options [index].text;
StartCoroutine (LoadFbx (value));
});
directoryInput = GameObject.Find ("DirectoryInput").GetComponent<InputField> ();
// InputFieldを監視
directoryInput.OnEndEditAsObservable ().Subscribe (UpdateDropDown);
}
void UpdateDropDown (string dir)
{
fileDictionary = new Dictionary<string, string> ();
if (!Directory.Exists (dir)) {
fileDropDown.options = defaultOption;
return;
}
Directory.GetFiles (dir)
.Where (path => path.ToLower ().EndsWith (".fbx"))
.ToList ()
.ForEach (path => fileDictionary.Add (Path.GetFileName (path), path));
fileDropDown.options = ConvertArrayToOptions (fileDictionary.Keys.ToArray ());
}
List<Dropdown.OptionData> ConvertArrayToOptions (string[] files)
{
var list = files.Select (path => {
var data = new Dropdown.OptionData ();
data.text = path;
return data;
}).ToList ();
list.Insert (0, defaultOption [0]);
return list;
}
IEnumerator LoadFbx (string path)
{
if (fileDictionary == null || !fileDictionary.ContainsKey (path)) {
yield break;
}
Debug.Log ("未実装だよ!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment