close

上一篇 有提到Unity 寫入Excel 基本功能,延續上一篇的方法來實踐有趣的事情吧。

這次 我們會使用Unity UI 功能,來方便設計基本的介面設定。

此篇由程式方式來設定UI設定,比較方便,不用到版面慢慢設定的麻煩。

首先 我們要先知道,我們介面需要使用甚麼物件來進行。

此篇使用物件: Text, InputField, Button

因此建立 UI Class 來方式來撰寫物件設定的功能

Script Name: UIClass

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class UIClass : MonoBehaviour
{
    public InputField inputField(InputField obj, float pointX, float pointY, float sizeX, float sizeY){
  
        obj.transform.position = new Vector2(Screen.width / 2 * pointX, Screen.height /2 * pointY);
        obj.image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);
  
        return obj;
    }
  
    public Text text(Text text, string message, float pointX, float pointY, float sizeX, float sizeY, Color color, int textSize, FontStyle fontStyle){
        
        text.transform.position = new Vector2(Screen.width /2 * pointX, Screen.height /2 * pointY);
        text.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);
  
        text.text = message;
        text.fontSize = textSize;
        text.fontStyle = fontStyle;
        text.color = color;
  
        return text;
    }
  
    public Button button(Button button, string button_Name, float pointX, float pointY, float sizeX, float sizeY, UnityAction onClick){
  
        button.transform.position = new Vector2(Screen.width / 2 * pointX, Screen.height /2 * pointY);
        button.image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);
  
        GameObject buttonName = button.transform.GetChild(0).gameObject;
        buttonName.name = "button_Name";
        buttonName.GetComponent<Text>().text = button_Name;
        
        button.onClick.AddListener(onClick);
  
        return button;
    }
}

 

程式部分: 

1. UnityAction - (記得匯入) using UnityEngine.Events; 

※ 由於Unity UI Button 是兩個物件: button與Text物件形成父子物件,為了好分辨,用程式直接進行修改,就不用針對一個一個物件慢慢修改。

2. GameObject buttonName = button.transform.GetChild(0).gameObject;

  •     buttonName.name = "button_Name";
  •     buttonName.GetComponent<Text>().text = button_Name

-------------------------------------------------------------------------------

Script Name : Layout

 

using UnityEngine;
using UnityEngine.UI;
using System;

public class Layout : UIClass
{
    public InputField year_inputField, month_inputField, company_inputField;
    public Text year_text, month_text, company_text;
  
    public Button enter_btn, quit_btn;
  
    ClassSchedule classSchedule;
  
    private void Start() {
  
        button_function();
    }
  
    private void Update() {
        layout_();
    }
  
    // 按鈕
    void button_function(){
  
         // 按鈕: 建立班表與應用程式離開
        button(enter_btn, "建立班表格式", 1.3f, 0.3f, 0.3f, 0.2f, Enter_Onclick);
        button(quit_btn, "結束應用程式", 1.7f, 0.3f, 0.3f, 0.2f, Quit_Onclick);
    }
  
    // 介面
    void layout_(){
        
        // 年
        inputField(year_inputField, 0.3f, 1.7f, 0.3f, 0.2f);
        text(year_text, "年", 0.5f, 1.7f, 0.05f, 0.07f, Color.white, 16, FontStyle.Normal);
  
        // 月
        inputField(month_inputField, 0.9f, 1.7f, 0.3f, 0.2f);
        text(month_text, "月", 1.1f, 1.7f, 0.05f, 0.07f, Color.white, 16, FontStyle.Normal);
  
        // 公司名稱
        inputField(company_inputField, 1.65f, 1.7f, 0.3f, 0.2f);
        text(company_text, "公司名稱", 1.36f, 1.7f, 0.14f, 0.07f, Color.white, 16, FontStyle.Normal);
    }
  
    void Enter_Onclick(){
  
        classSchedule = new ClassSchedule(Int32.Parse(year_inputField.text), Int32.Parse(month_inputField.text), company_inputField.text, "行政");
  
        classSchedule.classShedule_Fixed();
    }
  
    void Quit_Onclick(){
        Application.Quit();
    }
}

 

程式部分:

1.  classSchedule = new ClassSchedule(Int32.Parse(year_inputField.text), Int32.Parse(month_inputField.text), company_inputField.text, "行政");

※ 也可以直接進行ClassSchedule 指定參數,不建議使用。

2. button(enter_btn, "建立班表格式", 1.3f, 0.3f, 0.3f, 0.2f, Enter_Onclick);  -  Enter_Onclick : 直接寫成方法來探聽即可。

-------------------------------------------------------------------------------

Script Name : classShedule_Fixed

 

using System;
using System.IO;
using OfficeOpenXml;
using Drawing = System.Drawing;

public class ClassSchedule
{
    private int _year, _month;
    private string _companyName, _unit;
   
    public ClassSchedule(int year, int month, string companyName, string unit)
    {
        this._year = year;
        this._month = month;
        this._companyName = companyName;
        this._unit = unit;
    }
  
    // 建立資料位置
    private string folderPath = @"D:\班表\";
  
    // 檔案路徑
    private string filePath;                                             
  
    // Excel 固定格式
    int companyNameYear = 1;  // 年
    int positionNumber = 2;  // 職位
    int dayExcelNuber = 3;   // 天
  
    // excel 組件
    ExcelPackage excelPackage;
    ExcelWorksheet workSheets;
  
    // 每年每月類別
    QueryMonthAndDays queryMonthAndDays;
  
    // 檔案類別
    FileInfo fileInfo;
  
    public void classShedule_Fixed(){
  
        // 建立資料夾與檔案位置
        createExcelFile();
  
        queryMonthAndDays = new QueryMonthAndDays(_year, _month, 1);
  
        excelPackage = new ExcelPackage(fileInfo);
        workSheets = excelPackage.Workbook.Worksheets.Add(queryMonthAndDays.getData()[0].month + "月");
  
        // 寫入公司名稱
        workSheets.Cells[companyNameYear, 1].Value = queryMonthAndDays.getData()[0].year + "年" + queryMonthAndDays.getData()[0].month + "月 " + _companyName + " (" + "單位: " + _unit + ")";
        workSheets.Cells[companyNameYear, 1, 1, queryMonthAndDays.getData().Count + 3].Merge = true;
  
        // 寫入單位
        workSheets.Cells[positionNumber, 1].Value = _unit;
        workSheets.Cells[positionNumber, 1, positionNumber, queryMonthAndDays.getData().Count + 3].Merge = true;
  
        tableColor(positionNumber, 1, Drawing.Color.Yellow);
  
                // 寫入年月與公司名稱
        foreach (var dayweekData in queryMonthAndDays.getData())
        {
            workSheets.Cells[dayExcelNuber, 1].Value = "員工";
            // 天與週次
            workSheets.Cells[dayExcelNuber, Int32.Parse(dayweekData.day) + 1].Value = dayweekData.week + dayweekData.day;
  
            // 休假
            workSheets.Cells[dayExcelNuber, Int32.Parse(dayweekData.day) + 2].Value = "休假";
            // 表格上色
            tableColor(dayExcelNuber, queryMonthAndDays.getData().Count + 2, Drawing.Color.Red);
  
            //加班
            workSheets.Cells[dayExcelNuber, Int32.Parse(dayweekData.day) + 3].Value = "加班";
        }
  
        excelStyle(companyNameYear, 1, dayExcelNuber, queryMonthAndDays.getData().Count + 3);
        excelPackage.Save();
    }
        // excel 樣式
    void excelStyle(int fromRow, int fromCol, int toRow, int toCol)
    {
        // 字形
        workSheets.Cells.Style.Font.Name = "標楷體";
        // 文字大小
        workSheets.Cells.Style.Font.Size = 16;
        // 水平置中
        workSheets.Cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        // 垂直中
        workSheets.Cells.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        // 表格框線
        workSheets.Cells[fromRow, fromCol, toRow, toCol].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        workSheets.Cells[fromRow, fromCol, toRow, toCol].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        workSheets.Cells[fromRow, fromCol, toRow, toCol].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        workSheets.Cells[fromRow, fromCol, toRow, toCol].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
    }
  
    // Drawing
    void tableColor(int fromRow, int fromCol, Drawing.Color color)
    {
        workSheets.Cells[fromRow, fromCol].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
        workSheets.Cells[fromRow, fromCol].Style.Fill.BackgroundColor.SetColor(color);
    }
  
    // 建立檔案
    private void createExcelFile()
    {
        // 建立資料夾
        if (!Directory.Exists(folderPath + @"\" + _unit))
            Directory.CreateDirectory(folderPath + @"\" + _unit);
  
        // 判斷檔案是否存在
        if (fileExist())
        {
            fileInfo.Delete();
            fileInfo = new FileInfo(filePath);
        }
  
    }
  
    // 檔案是否存在
    bool fileExist()
    {
        filePath = folderPath + @"\" + _unit + @"\" + _year.ToString() + "年" + _month.ToString() + "月" + ".xlsx";
        fileInfo = new FileInfo(filePath);
  
        bool fileExist = fileInfo.Exists;
  
        return fileExist;
    }
}

 

程式部分:

1. using Drawing = System.Drawing -  記得Unity 專案匯入System.Drawing.dll 才能執行

2. excelPackage = new ExcelPackage(fileInfo) -  檔案位置

3. workSheets = excelPackage.Workbook.Worksheets.Add(queryMonthAndDays.getData()[0].month + "月") - Excel 活頁簿

4. excelStyle(companyNameYear, 1, dayExcelNuber, queryMonthAndDays.getData().Count + 3) - excelStyle(X位置, Y軸位置, X幾格, Y幾格)

5. excelPackage.Save(); - Excel 寫入後存檔

-------------------------------------------------------------------------------

Unity 介面部分

GameObjcet :

  1. Main Camera
  2. EventSystem
  3. Canvas:   
    • Year_text -Text
    • Year_inputField - InputField
    • Month_text - Text
    • Month_inputField - InputField
    • Company_text - Text
    • Company_inputField - InputField
    • Enter_btn - Button
    • Quit_btn - Button

那些物件不用設定任何事情,只需要改名及把Layout 腳本放到Canvas物件中。

螢幕擷取畫面 2021-05-19 045114

Layout 腳本顯示Public 物件名稱放入場景物件

再來調整製作過程需要的解析度,本版主習慣用 16:9 的比例來做專案,除非遊戲企劃規定,依據更改。

image

※ 順便一提: 此篇沒有UI 介面設計,單位輸入的,如果使用者需要可以自行加入。

以上設定好後,就可以試試了,以下執行結果。

image

螢幕擷取畫面 2021-05-19 045114

 

 

arrow
arrow
    文章標籤
    unity教學 Excel 軟體
    全站熱搜

    Xauxas 發表在 痞客邦 留言(0) 人氣()