這是示範專案使用的是Unity UI 功能當成介面,文字顯示部分則是用InputField之物件來做顯示,主要是為了方便使用。
UI_Canvas 物件需要:
Button 物件與組件:
程式碼(放到Canvas 物件) 設定:
------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;
using UnityEngine.UI;
public class Article : MonoBehaviour
{
public GameObject pathBack, scrollbar_FilePatch, path_Message, enter;
// 檔案位置
private string patch = @"C:\Users\JohnLin\Desktop\測試\";
public string path_Type
{
set { patch = value; }
get { return patch; }
}
// 檔案位置List
private List<string> file_patch = new List<string>();
private string patch_result;
public float pointX, pointY;
// 讀取文字檔內容
private string readtxtMessage;
// 刪除重復編號
private int delectNumber;
void Start()
{
// 按鈕屬性
button_(enter, 0.12f, 0.58f, 0.2f, 30);
}
void Update()
{
// 顯示路徑背景
image_(pathBack, 0.42f, 1.32f, 0.8f, 1.2f);
// 拉霸
scrollbar_(scrollbar_FilePatch, 0.86f, 1.4f, 0, 0);
}
// Button 屬性
Button button_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Button btn = obj.GetComponent<Button>();
// posistion
btn.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
// imageSize
btn.image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, sizeY);
switch (btn.name)
{
case "Enter":
btn.onClick.AddListener(() => btnOnClick_Enter());
break;
}
return btn;
}
// 探聽(確定)
public void btnOnClick_Enter()
{
clearList();
StartCoroutine(readFire());
patch_result = "";
for (int x = 0; x < file_patch.Count; x++)
patch_result += file_patch[x] + "\n";
text_(path_Message).text = patch_result;
}
// Image 屬性
Image image_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Image image = obj.GetComponent<Image>();
// posistion
image.rectTransform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
// imageSize
image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);
return image;
}
// 拉霸
Scrollbar scrollbar_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Scrollbar scrollbar = obj.GetComponent<Scrollbar>();
scrollbar.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
//scrollbar.size = 12;
return scrollbar;
}
// Text 屬性
Text text_(GameObject obj)
{
Text text = obj.GetComponent<Text>();
text.horizontalOverflow = HorizontalWrapMode.Wrap;
text.verticalOverflow = VerticalWrapMode.Overflow;
return text;
}
// 自動閱讀檔案
IEnumerator readFire()
{
DirectoryInfo di = new DirectoryInfo(path_Type);
foreach (FileInfo fileInfo in di.GetFiles("*.*", SearchOption.AllDirectories))
{
file_patch.Add(fileInfo.ToString());
}
yield return new WaitForSeconds(0.5f);
}
/// <summary>
/// 讀取文建資料
/// </summary>
/// <returns></returns>
private List<string> readFileTxtMessage(string str)
{
StreamReader streamReader = new StreamReader(str);
List<string> readTxtMessageList = new List<string>();
readTxtMessageList.Clear();
while ((readtxtMessage = streamReader.ReadLine()) != null)
readTxtMessageList.Add(readtxtMessage);
streamReader.Close();
return readTxtMessageList;
}
// 清除List
void clearList()
{
file_patch.Clear();
}
}
----------------------
結果圖:
UI_Canvas 物件需要:
- InputField - 命名: Path
- Scrollbar - 命名: Scrollbar_FileName
- Button - 命名: Enter
InputField 物件與組件:
- 子物件(Text) - 命名: FileName ※屬性 Text: 清空。
- InputField: 關閉功能。
- Scroll Rect: Content(放入Text子物件),Horizontal 勾勾拿掉,Vertical Scrolbar (放入Scrollbar 物件)。
- Mask。
Scrollbar 物件與組件:
- scrolbar: Direction(更改)
Button 物件與組件:
- 子物件(Text)-命名: btnMessage ※屬性Text: 輸入確定。
物件共同組件設定:
程式碼(放到Canvas 物件) 設定:
------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;
using UnityEngine.UI;
public class Article : MonoBehaviour
{
public GameObject pathBack, scrollbar_FilePatch, path_Message, enter;
// 檔案位置
private string patch = @"C:\Users\JohnLin\Desktop\測試\";
public string path_Type
{
set { patch = value; }
get { return patch; }
}
// 檔案位置List
private List<string> file_patch = new List<string>();
private string patch_result;
public float pointX, pointY;
// 讀取文字檔內容
private string readtxtMessage;
// 刪除重復編號
private int delectNumber;
void Start()
{
// 按鈕屬性
button_(enter, 0.12f, 0.58f, 0.2f, 30);
}
void Update()
{
// 顯示路徑背景
image_(pathBack, 0.42f, 1.32f, 0.8f, 1.2f);
// 拉霸
scrollbar_(scrollbar_FilePatch, 0.86f, 1.4f, 0, 0);
}
// Button 屬性
Button button_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Button btn = obj.GetComponent<Button>();
// posistion
btn.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
// imageSize
btn.image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, sizeY);
switch (btn.name)
{
case "Enter":
btn.onClick.AddListener(() => btnOnClick_Enter());
break;
}
return btn;
}
// 探聽(確定)
public void btnOnClick_Enter()
{
clearList();
StartCoroutine(readFire());
patch_result = "";
for (int x = 0; x < file_patch.Count; x++)
patch_result += file_patch[x] + "\n";
text_(path_Message).text = patch_result;
}
// Image 屬性
Image image_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Image image = obj.GetComponent<Image>();
// posistion
image.rectTransform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
// imageSize
image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);
return image;
}
// 拉霸
Scrollbar scrollbar_(GameObject obj, float width, float height, float sizeX, float sizeY)
{
Scrollbar scrollbar = obj.GetComponent<Scrollbar>();
scrollbar.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
//scrollbar.size = 12;
return scrollbar;
}
// Text 屬性
Text text_(GameObject obj)
{
Text text = obj.GetComponent<Text>();
text.horizontalOverflow = HorizontalWrapMode.Wrap;
text.verticalOverflow = VerticalWrapMode.Overflow;
return text;
}
// 自動閱讀檔案
IEnumerator readFire()
{
DirectoryInfo di = new DirectoryInfo(path_Type);
foreach (FileInfo fileInfo in di.GetFiles("*.*", SearchOption.AllDirectories))
{
file_patch.Add(fileInfo.ToString());
}
yield return new WaitForSeconds(0.5f);
}
/// <summary>
/// 讀取文建資料
/// </summary>
/// <returns></returns>
private List<string> readFileTxtMessage(string str)
{
StreamReader streamReader = new StreamReader(str);
List<string> readTxtMessageList = new List<string>();
readTxtMessageList.Clear();
while ((readtxtMessage = streamReader.ReadLine()) != null)
readTxtMessageList.Add(readtxtMessage);
streamReader.Close();
return readTxtMessageList;
}
// 清除List
void clearList()
{
file_patch.Clear();
}
}
----------------------
結果圖:
沒有留言:
張貼留言