使用 Visual Studio Code 編譯C# 方法:
- 下載 Visual Studio Code 後進行安裝
- 並安裝C# 相關套件
- 再來 Visual Studio Code 新增資料夾到工作區
- 終端機 打上: dotnet new console
使用 Visual Studio Code 編譯C# 方法:
Unity 練習UI 腳本,因此做了一個小型居家照顧小工具,主要家人工作關係,都要計算,就拿來當練習玩玩。
專案會持續更新.....會弄到導航計算公里數功能。
顯示結果:
本文 只提供場景的UI 功能程式。
主頁
MainInterface 腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainInterface
{
private Image buttonArea;
private RectTransform area;
ImageSetting buttonAreaSetting;
AreaSetting areaSetting;
public MainInterface(Image buttonArea, RectTransform area)
{
this.buttonArea = buttonArea;
this.area = area;
}
public void buttonArea_Layout()
{
buttonAreaSetting = new ImageSetting(buttonArea, 1f, 1f, 1.6f, 1.6f);
buttonAreaSetting.function(null, false, true, area);
// buttonArea 物件底下子物件
area.transform.parent = buttonArea.transform;
areaSetting = new AreaSetting(area, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f));
areaSetting.function(TextAnchor.MiddleCenter, false, false, false, false, true, true);
}
}
------------------------
服務時間計算
ServiceInterface 腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ServiceInterface
{
private RectTransform manage;
private Text hour, minute, titleMessage, serviceMessage;
private InputField hourInput, minuteInput, serviceInputField;
AreaSetting manageSetting;
TextSetting titleMessageSetting;
InterfaceSetting hourSetting, minuteSetting, servicesSetting;
private Image breakGroud;
private Image breaGroudArea;
ImageSetting breakGroudSetting;
ImageSetting breakGraoudAreaSetting;
public ServiceInterface(RectTransform manage, Text titleMessage,
Text hour, InputField hourInput, Text minute, InputField minuteInput)
{
this.manage = manage;
this.titleMessage = titleMessage;
this.hour = hour;
this.hourInput = hourInput;
this.minute = minute;
this.minuteInput = minuteInput;
}
public ServiceInterface(Image breakGroud, Image breaGroudArea, RectTransform manage)
{
this.breakGroud = breakGroud;
this.breaGroudArea = breaGroudArea;
this.manage = manage;
}
public ServiceInterface(RectTransform manage, Text serviceMessage, InputField serviceInputField)
{
this.manage = manage;
this.serviceMessage = serviceMessage;
this.serviceInputField = serviceInputField;
}
public void breakGroud_Layout()
{
breakGroudSetting = new ImageSetting(breakGroud, 1f, 1f, 1.6f, 1.6f);
breakGroudSetting.function(null);
// breakGroudArea 物件底下子物件
breaGroudArea.transform.parent = breakGroud.transform;
breakGraoudAreaSetting = new ImageSetting(breaGroudArea, 1f, 1.4f, 1.6f, 0.8f);
breakGraoudAreaSetting.function(null, false, true, manage);
// breaGroudArea 物件底下子物件
manage.transform.parent = breaGroudArea.transform;
manageSetting = new AreaSetting(manage, Vector2.zero, new Vector2(1, 1), new Vector2(0.5f, 1));
manageSetting.defaultFunction(new Vector2(0, Screen.height / 2 * 0.8f));
}
// 服務次數
public void serviceTime_Layout(Font font, float pointY)
{
manageSetting = new AreaSetting(manage, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f));
manageSetting.defaultFunction(Vector2.zero);
servicesSetting = new InterfaceSetting(serviceMessage, serviceInputField, font);
servicesSetting.funtcionHoliday("服務次數", pointY, Color.green);
serviceMessage.transform.parent = manage.transform;
serviceInputField.transform.parent = manage.transform;
}
public void serverTime_Layout(Font font, float pointY, string title)
{
manageSetting = new AreaSetting(manage, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f));
manageSetting.defaultFunction(Vector2.zero);
titleMessageSetting = new TextSetting(titleMessage, 0.6f, pointY, 0.58f, 0.1f);
titleMessageSetting.function(font, FontStyle.Normal, title, TextAnchor.MiddleLeft, Color.green, 7);
hourSetting = new InterfaceSetting(hour, hourInput, font);
hourSetting.dateWeekfunction("小時", pointY - 0.16f, Color.red);
minuteSetting = new InterfaceSetting(minute, minuteInput, font);
minuteSetting.dateWeekfunction("分鐘", pointY - 0.36f, Color.red);
// manage 物件底下子物件
titleMessage.transform.parent = manage.transform;
hour.transform.parent = manage.transform;
hourInput.transform.parent = manage.transform;
minute.transform.parent = manage.transform;
minuteInput.transform.parent = manage.transform;
titleMessage.transform.parent = manage.transform;
}
public int getServiceValue()
{
if (serviceInputField.text == "")
return 1;
return Int32.Parse(serviceInputField.text);
}
public int getMinuteValue()
{
if (minuteInput.text == "")
return 0;
return Int32.Parse(minuteInput.text);
}
public int getHourValue()
{
if (hourInput.text == "")
return 0;
return Int32.Parse(hourInput.text);
}
}
------------------------
單位換算
TimeConversionInterface 腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Events;
public class TimeConversionInterface
{
private Text hour, minute, calculatorProcess, calculatorResult;
private InputField hourInput, minuteInput;
private RectTransform hourManage, minuteMange, calculatorArea;
AreaSetting hourMangeSetting, minuteMangeSetting, calculatorAreaSetting;
InterfaceSetting hourSetting, minuteSetting;
public TimeConversionInterface(RectTransform hourManage, Text hour, InputField hourInput,
RectTransform minuteMange, Text minute, InputField minuteInput)
{
this.hourManage = hourManage;
this.hour = hour;
this.hourInput = hourInput;
this.minuteMange = minuteMange;
this.minute = minute;
this.minuteInput = minuteInput;
}
private Image breakGroud, instructionBreakGroud;
private Text instruction;
ImageSetting breakGroudSetting, instructionBreakGroudSetting;
TextSetting instructionSetting;
public TimeConversionInterface(Image breakGroud, Image instructionBreakGroud, Text instruction)
{
this.breakGroud = breakGroud;
this.instructionBreakGroud = instructionBreakGroud;
this.instruction = instruction;
}
TextSetting calculatorProcessSetting, calculatorResultSetting;
public TimeConversionInterface(Image breakGroud, RectTransform calculatorArea, Text calculatorProcess, Text calculatorResult)
{
this.breakGroud = breakGroud;
this.calculatorArea = calculatorArea;
this.calculatorProcess = calculatorProcess;
this.calculatorResult = calculatorResult;
}
private Button button1, button2, button3, button4;
ButtonSetting button1Setting, button2Setting, button3Setting, button4Setting;
public TimeConversionInterface(Button button1, Button button2, Button button3, Button button4)
{
this.button1 = button1;
this.button2 = button2;
this.button3 = button3;
this.button4 = button4;
}
public void calculatorHorizontalBtns(float pointY, Font font, string btnStr1, string btnStr2, string btnStr3, string btnStr4,
UnityAction unityAction1, UnityAction unityAction2, UnityAction unityAction3, UnityAction unityAction4)
{
button1Setting = new ButtonSetting(button1, 0.4f, 0.7f + pointY, 0.3f, 0.15f, unityAction1);
button1Setting.function(font, FontStyle.Normal, btnStr1, TextAnchor.MiddleCenter, Color.black, 7);
button2Setting = new ButtonSetting(button2, 0.8f, 0.7f + pointY, 0.3f, 0.15f, unityAction2);
button2Setting.function(font, FontStyle.Normal, btnStr2, TextAnchor.MiddleCenter, Color.black, 7);
button3Setting = new ButtonSetting(button3, 1.2f, 0.7f + pointY, 0.3f, 0.15f, unityAction3);
button3Setting.function(font, FontStyle.Normal, btnStr3, TextAnchor.MiddleCenter, Color.black, 7);
button4Setting = new ButtonSetting(button4, 1.6f, 0.7f + pointY, 0.3f, 0.15f, unityAction4);
button4Setting.function(font, FontStyle.Normal, btnStr4, TextAnchor.MiddleCenter, Color.black, 7);
}
// 計算機介面
public void calculayoutSetting(Font font, float povitY, int textSize)
{
breakGroudSetting = new ImageSetting(breakGroud, 1f, 0.6f, 1.6f, 0.66f);
breakGroudSetting.function(null, false, true, calculatorArea);
calculatorAreaSetting = new AreaSetting(calculatorArea, Vector2.zero, new Vector2(1, 1), new Vector2(0.5f, 1f));
calculatorAreaSetting.defaultFunction(new Vector2(1, Screen.height / 2 * povitY));
// 顯示過程
calculatorProcessSetting = new TextSetting(calculatorProcess, 1f, 0.86f, 1.5f, 0.1f);
calculatorProcessSetting.function(font, FontStyle.Normal, "", TextAnchor.MiddleLeft, Color.black, 7);
// 顯示計算後結果
calculatorResultSetting = new TextSetting(calculatorResult, 1f, 0.76f, 1.5f, 0.1f);
calculatorResultSetting.function(font, FontStyle.Normal, "", TextAnchor.MiddleLeft, Color.black, 7);
}
// 背景
public void breakGroud_Layout(string instructionMessage)
{
breakGroudSetting = new ImageSetting(breakGroud, 1f, 1f, 1.6f, 1.6f);
breakGroudSetting.function(null);
instructionBreakGroudSetting = new ImageSetting(instructionBreakGroud, 1f, 1.59f, 1.6f, 0.42f);
instructionBreakGroudSetting.function(null, false, true, instruction.rectTransform);
// instructionBreakGroud 物件底下子物件
instruction.transform.parent = instructionBreakGroud.transform;
instructionSetting = new TextSetting(instruction, 0, 0, 0, 0.74f);
instructionSetting.function(7, instructionMessage, new Vector2(0, 0f), new Vector2(1, 1), new Vector2(0.5f, 1f));
}
// 小時與分鐘
public void hourAndminute(Font font, float pointY)
{
hourMangeSetting = new AreaSetting(hourManage, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f));
hourMangeSetting.defaultFunction(Vector2.zero);
// hourManage 物件底下子物件
hour.transform.parent = hourManage.transform;
hourInput.transform.parent = hourManage.transform;
hourSetting = new InterfaceSetting(hour, hourInput, font);
hourSetting.dateWeekfunction("小時", pointY, Color.red);
minuteMangeSetting = new AreaSetting(minuteMange, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f));
minuteMangeSetting.defaultFunction(Vector2.zero);
// minuteMange 物件底下子物件
minuteMange.transform.parent = minuteMange.transform;
minuteInput.transform.parent = minuteMange.transform;
minuteSetting = new InterfaceSetting(minute, minuteInput, font);
minuteSetting.dateWeekfunction("分鐘", pointY - 0.15f, Color.red);
}
public int getHourValue()
{
if (hourInput.text == "")
return 0;
return Int32.Parse(hourInput.text);
}
public int getMinuteValue()
{
if (minuteInput.text == "")
return 0;
return Int32.Parse(minuteInput.text);
}
}
------------------------
週次計算
InterfaceObj 腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public abstract class InterfaceObj
{
protected Text message;
protected InputField inputFiled;
protected Font font;
public InterfaceObj(Text message, InputField inputFiled, Font font)
{
this.message = message;
this.inputFiled = inputFiled;
this.font = font;
}
public abstract void dateWeekfunction(string textMessage, float pointY);
public abstract void funtcionHoliday(string textMessage, float pointY);
public abstract void systemAndYear(string textMessage, float pointX);
}
InterfaceSetting 腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class InterfaceSetting : InterfaceObj
{
public InterfaceSetting(Text messge, InputField inputFiled, Font font) : base(messge, inputFiled, font) { }
TextSetting textSetting;
InputFieldSetting inputFieldSetting;
// 週次
public override void dateWeekfunction(string textMessage, float pointY)
{
textSetting = new TextSetting(message, 0.5f, pointY, 0.3f, 0.1f);
textSetting.function(font, FontStyle.Normal, textMessage, TextAnchor.MiddleLeft, Color.red, 7);
inputFieldSetting = new InputFieldSetting(inputFiled, 1.15f, pointY + 0.01f, 0.9f, 0.1f);
inputFieldSetting.function(font, FontStyle.Normal, TextAnchor.MiddleLeft, Color.black, 11,
InputField.ContentType.IntegerNumber);
}
// 假期
public override void funtcionHoliday(string textMessage, float pointY)
{
textSetting = new TextSetting(message, 0.65f, pointY, 0.6f, 0.1f);
textSetting.function(font, FontStyle.Normal, textMessage, TextAnchor.MiddleLeft, Color.red, 7);
inputFieldSetting = new InputFieldSetting(inputFiled, 1.28f, pointY + 0.01f, 0.64f, 0.1f);
inputFieldSetting.function(font, FontStyle.Normal, TextAnchor.MiddleLeft, Color.black, 11,
InputField.ContentType.IntegerNumber);
}
// 年與月
public override void systemAndYear(string textMessage, float pointX)
{
textSetting = new TextSetting(message, pointX, 1.89f, 1f, 0.13f);
textSetting.function(font, FontStyle.Normal, textMessage, TextAnchor.MiddleLeft, Color.red, 7);
inputFieldSetting = new InputFieldSetting(inputFiled, pointX - 0.65f, 1.89f, 0.3f, 0.12f);
inputFieldSetting.function(font, FontStyle.Normal, TextAnchor.MiddleLeft, Color.black, 11,
InputField.ContentType.IntegerNumber);
}
}
外部 Dll 都屬於非Unity的套件,加上Unity API 依賴 .net 架構,所以每一個版本Unity 支援的 .NET 版本 會有些不同。
因此 我們要對於專案的設定如下:
※ 匯入Dll 檔案,非Unity內部功能,會導致錯誤,因此要更改。
Unity 2019 LTS - 2020 做法:
Unity 環境下執行可正常,發佈不正常的情況下,大多數都是缺少:
那些dll檔案在Unity 安裝路徑下都能找的到,以下路徑 2019 LTS 路徑
路徑: C:\Program Files\Unity 2019.4.34f1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit
就能尋找的到了。