2021年12月8日 星期三

C# 抓取指定月份顯示民國 和 Day與Week

 利用 C# 抓取年月與週,以下程式設計方式: Interface方式來呈現

Calendar (C#) 腳本----參數管理----:  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Calendar 
{
    private string year;    // 年
    private string month;   // 月
    private string days;    // 日

    GetCalenderData getCalenderData;

    public Calendar(string year, string month, string day, GetCalenderData getCalenderData)
    {
        this.year = year;
        this.month = month;
        this.days = day;
        this.getCalenderData = getCalenderData;
    }

    public List<Data_data> getData()
    {
        return getCalenderData.getData(int.Parse(year), int.Parse(month), int.Parse(days));
    }

    public int dayNumber()
    {
        return getCalenderData.dayNumber(int.Parse(year), int.Parse(month), int.Parse(days));
    }
}

public interface GetCalenderData
{
    List<Data_data> getData(int year, int month, int day);     // 取得行事曆資料
    int dayNumber(int year, int month, int day);
}

// 暫存用
public class Data_data
{
    public string year;
    public string month;
    public string day;
    public string week;

    public Data_data(string year, string month, string day, string week)
    {
        this.year = year;
        this.month = month;
        this.day = day;
        this.week = week;
    }
}
InterfaceCalender (C#) 腳本----方法撰寫---- :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class InterfaceCalender : GetCalenderData
{
    DateTime dateTime;

    public int dayNumber(int year, int month, int day)
    {
        int dayNum = 0;

        for (int x = 1; x <= DateTime.DaysInMonth(dateTime.Year, dateTime.Month); x++)
        {
            dayNum = x;
        }
    
        return dayNum;
    }

    // 取得行事曆資料
    public List<Data_data> getData(int year, int month, int day)
    {
        List<Data_data> classExcels = new List<Data_data>();

        for(int i = 1; i <= DateTime.DaysInMonth(dateTime.Year, dateTime.Month); i++)
        {
            dateTime = new DateTime(year, month, i);
            
            classExcels.Add(new Data_data(yearConversion(dateTime.Year).ToString(), dateTime.Month.ToString(), i.ToString(),
                weekEngToChin(dateTime.DayOfWeek.ToString())));
    }

        return classExcels;
    }

    // 西元轉民國(公式)
    int yearConversion(int inVid)
    {
        return inVid - 1911;
    }

    // 英文轉中文(星期)
    string weekEngToChin(string dayOfWeek)
    {
        string chinWeek = "";

        if (dayOfWeek.ToString().Equals("Monday"))
            chinWeek = "一";
        if (dayOfWeek.ToString().Equals("Tuesday"))
            chinWeek = "二";
        if (dayOfWeek.ToString().Equals("Wednesday"))
            chinWeek = "三";
        if (dayOfWeek.ToString().Equals("Thursday"))
            chinWeek = "四";
        if (dayOfWeek.ToString().Equals("Friday"))
            chinWeek = "五";
        if (dayOfWeek.ToString().Equals("Saturday"))
            chinWeek = "六";
        if (dayOfWeek.ToString().Equals("Sunday"))
            chinWeek = "日";

        return chinWeek;
    }
}
TestRun (C#) 腳本----執行----:

public class TestRun : MonoBehaviour{ Calendar calendar; void Start() { calendar = new Calendar("2021", "12", "1", new InterfaceCalender()); } void Update() { foreach(var str in calendar.getData()) { Debug.Log(str.month +"月" + str.day + "日" + "星期:" + str.week); } } }

結果圖:


2021年9月30日 星期四

Unity UI 基本跑馬燈

 文字跑馬燈,所先要了解,怎麼顯示模式。

大致上作法 可以用兩種方式來使用。

  • 時間方式計算
  • 數字累加

本文採取第二種數字累加的方式來呈現。

邏輯:   數字: 1 代表1個字,表示說 數字累加的方式來顯示文字數量。

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

本文都採取使用C# 方式來進行編排,不太用到Unity設計界面來操作設定,

但是要先建立需要使用的遊戲物件即可。此篇介面採取比例 (16:9)

使用的遊戲物件:

  • Canvas (Unity UI)
  • Text (Unity UI)
  • EventSystem (Unity UI)
  • Camera
完成結果後

image























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

程式碼部分:   

腳本名稱: InterfaceObj

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 介面
public class InterfaceObj
{
    // image Obj 
    public Image imageObj(Image image, Color color , float pointX, float pointY, float sizeX, float sizeY)
    {
        image.transform.position = new Vector2(Screen.width / 2 * pointX, Screen.height / 2 * pointY);
        image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);

        image.color = color;

        return image;
    }

    // text Obj
    public Text textObj(Text text, string message, FontStyle fontStyle, Color color ,int textSize, float pointX, float pointY, float sizeX, float sizeY)
    {
        text.text = message;
        text.fontStyle = fontStyle;
        text.color = color;
        text.fontSize = textSize;

        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);

        return text;
    }
}

// 介面效果類別
public class interfaceAfterEffects
{
    /*--------------------------
        * 文字效果
    --------------------------*/

    private float marquee_startTime = 0;  // 跑馬燈開始時間

    // 文字跑馬燈
    public void textEffects(TextEffects effects, Text text, string message, float speed, bool start)
    {
     text.text = text_Marquee(message, speed, start);   
    }

    // 跑馬燈
    private string text_Marquee(string message, float speed, bool start)
    {
        return message.Substring(0, (int)text_marquee_timeData(message.Length, speed, start));
    }

    // 計數器(跑馬燈顯示)
    private float text_marquee_timeData(float textSize, float speed, bool start)
    {
        float time = (marquee_startTime < textSize && start.Equals(true)) ? marquee_startTime += speed :
            (start.Equals(false)) ? marquee_startTime = 0 : marquee_startTime = textSize;

        return time;
    }
}

腳本名稱: UI_Layout

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_Layout : MonoBehaviour
{
    InterfaceObj interfaceObj = new InterfaceObj();

    // 文字訊息(系統)
    public void system_mesage_text(Text cameraModel_Text, Text recording_Text, Text InternalLab_Text, Text section8_Text, Text scanning_Text, Text activated, float pointX, float pointY, float sizeX, float sizeY)
    {
        interfaceObj.textObj(cameraModel_Text, "CAMERA 05s", FontStyle.Normal, new Color(0, 0.9f, 1, 1), (Screen.width / 2) / 10, 0.43f, 1.7f, 0.66f, 0.21f);
        interfaceObj.textObj(recording_Text, "REC.", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 16, 0.19f, 1.4f, 0.15f, 0.13f);
        interfaceObj.textObj(InternalLab_Text, "INTERNAL LAB 13", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 27, 0.55f, 1.44f, 0.32f, 0.1f);
        interfaceObj.textObj(section8_Text, "SECTION 8 - LEVEL 06", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 29, 0.58f, 1.33f, 0.38f, 0.08f);
        interfaceObj.textObj(scanning_Text, "SCANNING", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 12, 0.34f, 0.85f, 0.47f, 0.18f);
        interfaceObj.textObj(activated, "SYSTEM ACTIVATED", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 18, 0.4f, 0.7f, 0.6f, 0.12f);
    }
}

腳本名稱: Layout_Run

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Layout_Run : UI_Layout
{
    public Text cameraModel_Text;
    public Text recording_Text;
    public Text internalLab_Text;
    public Text section8_Text;
    public Text scanning_Text;
    public Text activated_Text;

    // 跑馬燈
    interfaceAfterEffects scanning_Text_marquess = new interfaceAfterEffects();
    interfaceAfterEffects activated_Text_marquess = new interfaceAfterEffects();

    public bool sw = false;

    private void Update()
    {
        systemLayoutMessge();

        if (Input.GetKeyDown(KeyCode.W))
            sw = !sw;
    }

    private void systemLayoutMessge()
    {
   
        system_mesage_text(cameraModel_Text, recording_Text, internalLab_Text, section8_Text, scanning_Text, activated_Text, pointX, pointY, sizeX, sizeY);

        // 文字跑馬燈
        scanning_Text_marquess.textEffects(scanning_Text, "SCANNING", 0.6f, sw);
        activated_Text_marquess.textEffects(activated_Text, "SYSTEM ACTIVATED", 0.4f, sw);

    }
}

2021年7月29日 星期四

Visual Studio Code for Unity ( Visual Studio Code 撰寫Unity 程式)

目前 很夯 Visual Studio Code  來撰寫程式,主要是開啟速度較快與程式撰寫者,可自行匯入模組,那我們如何進行用 Visual Studio Code 來撰寫Unity底下程式,如步驟以下:

官方連結:  Visual Studio Code  

下載完成後_介面如下:












Unity開發,這裡使用 C#語言來撰寫,因此下載 .NET Core SDK 工具來協助方便撰寫。

官方連結:  .NET Core SDK 

再到此圖紅色框框點選下載



下載安裝後,在安裝常用組件:






再到Unity3D 軟體,設定程式腳本編譯器選擇



























出現以下圖片結果表示成功了 XD,若沒辦法顯示,請進行重新開機即可






2021年6月29日 星期二

Unity to Excel 建立班表 (固定格式)

因由Unity 發佈後,無法建立Excle原因,主要是Dll少檔案

Excel 全部相關DLL下載

提取码:0xso

備用載點

---------------------- Excel 班表格式 ------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Data;
using Excel;
using OfficeOpenXml;
using System;

public class CreateRoster_Excel
{
    private int _year, _month;
    private string _companyName, _unit;
   
    public CreateRoster_Excel(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 fileName = "班表";
    // 檔案路徑
    private string filePath;                                             

    // Excel 固定格式
    int companyNameYear = 1;                                      // 年
    int positionNumber = 2;                                       // 職位
    int dayExcelNuber = 3;                                        // 天

    // excel 組件
    ExcelPackage excelPackage;
    ExcelWorksheet workSheets;

    // 每年每月類別
    QueryMonthAndDays queryMonthAndDays;

    // 檔案類別
    FileInfo fileInfo;

    public void test()
    {
        createExcelFile();
        excel_Fixed();
    }

    // excel 固定格式建立
    private void excel_Fixed()
    {
        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, System.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, System.Drawing.Color.Red);

            //加班
            workSheets.Cells[dayExcelNuber, Int32.Parse(dayweekData.day) + 3].Value = "加班";
        }

        excelStyle(1, 1, 3);
        excelPackage.Save();
    }

    // excel 樣式
    void excelStyle(int fromRow, int fromCol, int toRow)
    {
        // 字形
        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, queryMonthAndDays.getData().Count + 3].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        workSheets.Cells[fromRow, fromCol, toRow, queryMonthAndDays.getData().Count + 3].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        workSheets.Cells[fromRow, fromCol, toRow, queryMonthAndDays.getData().Count + 3].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        workSheets.Cells[fromRow, fromCol, toRow, queryMonthAndDays.getData().Count + 3].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
    }

    // 表格顏色
    void tableColor(int fromRow, int fromCol, System.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;
    }
 }

---------------------------以下每年每月時間抓取---------------------------------

using UnityEngine;
using System;
using System.Collections.Generic;


// List 管理
public class Data_data
{
    public string year { get; set; }        // 年
    public string month { get; set; }       // 月
    public string day { get; set; }         // 日
    public string week { get; set; }        // 週期

    public Data_data(string year, string month, string day, string week)
    {
        this.year = year;
        this.month = month;
        this.day = day;
        this.week = week;
    }
}

public class QueryMonthAndDays
{
    int _year;      // 年
    int _month;     // 月
    int _day;       // 日

    DateTime startdate;

    // 建構子
    public QueryMonthAndDays(int year, int month, int day)
    {
        this._year = year;
        this._month = month;
        this._day = day;

        startdate = new DateTime(_year, _month, _day);
    }

    // 天數
    public int DayNumber()
    {
        int dayNumber = 0;
        for(int x=1; x <= DateTime.DaysInMonth(startdate.Year, startdate.Month); x++)
        {
            dayNumber = x;
        }

        return dayNumber;
    }

    // 執行
    public List<Data_data> getData()
    {
        List<Data_data> data = new List<Data_data>();
        
        for (int x = 1; x <= DateTime.DaysInMonth(startdate.Year, startdate.Month); x++)
        {
            startdate = new DateTime(startdate.Year, startdate.Month, x);

            data.Add(new Data_data(vidToRepublic(startdate.Year).ToString(), startdate.Month.ToString(), x.ToString(),
                weekEngToChin(startdate.DayOfWeek.ToString())));      
        }

        return data;
    } 

    // 西元轉換民國 (公式)
    int vidToRepublic(int dayofYear)
    {
        int vidNumber = 1911; // 公式

        int sum = dayofYear - vidNumber;

        return sum;           
    }

    // 英文轉中文(星期)
    string weekEngToChin(string dayOfWeek)
    {
        string chinWeek = "";

        if (dayOfWeek.ToString().Equals("Monday"))
            chinWeek = "一";
        if (dayOfWeek.ToString().Equals("Tuesday"))
            chinWeek = "二";
        if (dayOfWeek.ToString().Equals("Wednesday"))
            chinWeek = "三";
        if (dayOfWeek.ToString().Equals("Thursday"))
            chinWeek = "四";
        if (dayOfWeek.ToString().Equals("Friday"))
            chinWeek = "五";
        if (dayOfWeek.ToString().Equals("Saturday"))
            chinWeek = "六";
        if (dayOfWeek.ToString().Equals("Sunday"))
            chinWeek = "日";

        return chinWeek;
    }
}


------------------------- 圖片顯示 --------------------

結果顯示: Excel 建立班表格式


2021年3月8日 星期一

跟隨物件

可使用子彈跟蹤
----------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet_Missile : MonoBehaviour
{

    private Transform targetTr;   // 跟隨物件
    public float dist = 5f;
    public float height = 5.0f;
    public float dampTrace = 10.0f;

    // Update is called once per frame
    void Update()
    {
        targetTr = GameObject.Find("Planet").transform;

        transform.position = Vector3.Lerp(transform.position, targetTr.position - (targetTr.forward * dist) + (Vector3.up * height), Time.deltaTime * dampTrace);
        transform.LookAt(targetTr.position);
    }
}

2019年9月3日 星期二

賽車儀錶板_簡易版

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Canvas_Obj : MonoBehaviour
{
    private const float MAX_SPEED_ANGLE = -166;         // 儀錶板角度 (開始刻度)
    private const float ZERO_SPEED_ANGLE = 180;         // 儀錶板角度 (結束刻度)

    public RectTransform needleTranform;                // 儀錶板指針

    private float speedMax;
    private float speed;

    public float forward_Speed = 50;     // 加速儀錶板指針
    public float break_Speed = -100;    //  減速加入儀表板指針
    public float null_Speed = -20;      // 未加速儀表板指針

    void Awake()
    {
        speed = 0;  
        speedMax = 220f;
    }

    void Update()
    {
        HadlePlayerInput();
        needleTranform.eulerAngles = new Vector3(0, 0, GetSpeedRotation());       
    }  

    private void HadlePlayerInput()
    {
        float vertical = Input.GetAxis("Vertical");
        float pointerSpeed = (vertical > 0) ? pointerSpeed = forward_Speed : (vertical < 0) ? pointerSpeed = break_Speed : pointerSpeed = null_Speed;

        speed += pointerSpeed * Time.deltaTime;

        speed = Mathf.Clamp(speed, 0f, speedMax);
    }

    private float GetSpeedRotation()
    {
        float totalAngleSize = ZERO_SPEED_ANGLE - MAX_SPEED_ANGLE;

        float speedNormalized = speed / speedMax;

        return ZERO_SPEED_ANGLE - speedNormalized * totalAngleSize;
    }
}



結果圖:


2019年3月13日 星期三