unity > 自作 > FileSelector > FileSelectorシーンからScene1のfilename書換えするバージョン

```txt:動作確認
Unity 5.1.1-f on MacOS X 10.8.5
```

##準備
Scene1
[こちら](http://qiita.com/7of9/items/8ea0d330ccf912c2c3f1)と同じ

FileSelector :Scene
[こちら](http://qiita.com/7of9/items/566d62e2c35bf108336d)と同じ。ただし、Scene2という名前からFileSelectorに変更した。
さらに、各ButtonにSelectButtonControl.cs (下記)を関連付けた。

##code

```csharp:Button1Control.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Button1Control : MonoBehaviour {
	
	public static string filename; // used from FileSelector:Scene
	
	void Start () {
		DontDestroyOnLoad (this);
	}
	
	public void Button1Click() {
		Application.LoadLevel ("FileSelector");
	}
	
	public Text myText; // should be related to TextFileName:Text
	void OnGUI() {
		myText.text = filename;
	}
}
```

```SelectButtonControl.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SelectButtonControl : MonoBehaviour {
	
	public void ButtonSelector() {
		GameObject work = GameObject.Find (gameObject.name + "Text");
		if (work) {
			Button1Control.filename = work.GetComponent<Text>().text;
		} else { // error
			Button1Control.filename = gameObject.name;
		}
		Application.LoadLevel ("Scene1");
	}
}
```

##実行の様子
![Scene1_unity_-_150813-fileSelector_-_PC__Mac___Linux_Standalone__Personal_.jpg](https://qiita-image-store.s3.amazonaws.com/0/32870/55f65f7c-c44b-46e8-3f51-3ae38c8808be.jpeg)

![Scene1_unity_-_150813-fileSelector_-_PC__Mac___Linux_Standalone__Personal_.jpg](https://qiita-image-store.s3.amazonaws.com/0/32870/15fe0789-47f0-f4bd-353a-9a4a544e877f.jpeg)

![Scene1_unity_-_150813-fileSelector_-_PC__Mac___Linux_Standalone__Personal_.jpg](https://qiita-image-store.s3.amazonaws.com/0/32870/a05498b8-bfc3-78d3-e43a-bb04be74c9d6.jpeg)


##code02 (w/ mouse wheel)

FileSelectorシーンにおいて、mouse wheelによるスクロールバー制御を足したのはこちら。

```csharp:ScrollScript.cs
using UnityEngine;
using System.Collections;

using UnityEngine.UI; // for Text
using System.IO; // for Directory()

// Button1
// - Button1Text
// Button2
// - Button2Text
// Button3
// - Button3Text
// Button4
// - Button4Text
// ScrollbarFile
// EventSystem

public class ScrollScript : MonoBehaviour {
	
	private string [ ] s_files = Directory.GetFiles("."); 
	
	private void dispFilesOnButtons(string filename, int idx, int shift) {
		int pos = idx - shift;
		GameObject work = GameObject.Find ("Button" + pos.ToString () + "Text");
		if (work) {
			work.GetComponent<Text>().text = filename;
		}
	}

	private void changeScrollBarValue(string scrollBarName, bool isUp) {
		GameObject work = GameObject.Find (scrollBarName);
		if (work) {
			float aRatio = (float)work.GetComponent<Scrollbar>().value;
			if (isUp) {
				aRatio -= 0.05f; // for from top to bottom direction
			} else {
				aRatio += 0.05f; // for from top to bottom direction
			}
			work.GetComponent<Scrollbar>().value = aRatio;
		}
	}

	private int getShiftValue(string scrollBarName, int maxFileNum) {
		GameObject work = GameObject.Find (scrollBarName);
		if (work) {
			float aRatio = (float)work.GetComponent<Scrollbar>().value;
			return (int)(maxFileNum * aRatio);
		}
		return 0;
	}
	
	private void clearButtonsText()
	{
		foreach (Text aText in Text.FindObjectsOfType<Text> ()) {
			if (aText.name.Contains("Button")) { // e.g. Button2Text
				aText.text = "";
			}
		}
	}
	
	private void updateFileList()
	{
		int shift = getShiftValue ("ScrollbarFile", s_files.Length);
		
		clearButtonsText ();
		int count = 1;
		foreach (string aFile in s_files) {
			dispFilesOnButtons(aFile, count, shift);
			count++;
			Debug.Log(aFile);
		}
	}
	
	void Start() {
		updateFileList ();
	}
	
	public void ScrollBarChange() {
		updateFileList ();
	}   

	void OnGUI() {
		float val = Input.GetAxis ("Mouse ScrollWheel");
		if (val > 0.0f) {
			changeScrollBarValue("ScrollbarFile", /* isUp=*/true);
			Debug.Log ("up");
		} else if (val < 0.0f) {
			changeScrollBarValue("ScrollbarFile", /* isUp=*/false);
			Debug.Log ("down");
		} else {
			// do nothing
		}
	}

}
```