實驗1: 仿真系統的UI主界面設計
1.實驗目的
(1)熟悉Unity中UI界面的設計與編寫;
(2)熟悉UI界面中場景轉換,UI與場景內容相互關聯的方式。
(3)熟悉Unity中MySQL數據庫的操作
2.實驗內容
新建一個Unity場景,在此場景中實現如下功能:
(1)自行設計一個登錄、注冊UI界面;
(2)添加數據庫的動態鏈接庫文件,提前設計數據庫表格(自行設計);
(3)連接數據庫,實現增、刪、改、查等數據庫對用戶的操作;
(4)UI界面中包括canvas、Image、RawImage、Button等多種UI元素;
(5)實現點擊Play按鈕轉換場景,點擊Exit退出游戲的功能;
(6)實現主界面添加音量滑動桿、靜音等功能,添加背景音樂和音效音樂;
(7)為UI界面單獨設置一個場景,并設置編號為0。
3.實驗步驟
第一步:創建UI界面
(1)創建畫布,附加背景
創建canvas作為畫布,接著創建Raw Image和Image去實現基礎背景的搭建
?附加圖片,并拖拽到和畫布一樣大小,背景設計完成。
(2)添加交互組件
首先添加InputField組件 作為我們的輸入框,去實現賬號密碼框的設計
右鍵-->UI--->InputField
可以修改下面的TEXT去修改 輸入框的默認內容
接著,添加Text 去搭建一個基本的登錄框
添加Button組件,設計登錄,注冊,退出按鈕。
一個簡易的登錄注冊頁面完成
接著,添加Dropdown,Toggle,Slider組件 進行排版得到完整的UI界面
第二步:實現交互功能
第四步,添加腳本代碼實現功能
- 添加背景音樂,制作靜音,調節音量功能
右鍵--->Audio ?添加一個音樂組件
導入音樂素材,拖入Audio組件中
添加腳本實現音量靜音與控制
勾選toggle實現,靜音
?private?void?PlayMusic(bool?arg0)
?{
?????if?(arg0)
?????{
?????????ads.Pause();
?????}
?????else
?????{
?????????ads.Play();
?????}
?}
根據布爾值 判斷是否勾選,如果勾選了靜音按鈕,就關閉音樂
拖動slider實現控制音量
?private?void?ChangeVolume(float?arg0)
?{
?????ads.volume = arg0;
?}
2.連接數據庫,實現登錄注冊功能
第一步:在官網下載插件MySQL Connector Net
下載安裝完成后,添加數據庫的動態鏈接庫文件
第二步:創建一個空對象,附加腳本實現連接
/*實現登錄注冊功能
登錄:獲取輸入框中的字符串--->連接并打開數據庫--->查找用戶名密碼
Y = --->對比密碼-->Y=-->關閉數據庫--->登錄成功
Y = --->對比密碼-->N=-->關閉數據庫--->登錄失敗
N = --->關閉數據庫--->登錄失敗
注冊:獲取輸入框中的字符串--->連接并打開數據庫--->查找用戶名密碼
Y =>關閉數據庫 --->注冊失敗
N => 添加用戶名密碼--->關閉數據庫--->注冊成功
分解:
1.連接并打開數據庫
2.查找用戶名密碼
3.關閉數據庫
4.對比密碼
5.添加用戶名和密碼
接口:登錄1234 注冊1235
對象:GUI對象 輸入框*2 按鈕*2,文本數據庫:MySQLConnection,..Command...Reader字符串
*/using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MySql.Data.MySqlClient;
using System;
using UnityEngine.SceneManagement;public class Component9 : MonoBehaviour
{public InputField inputField1;public InputField inputField2;public Button button1;public Button button2;public Text Tip;MySqlConnection sqlConnection;string strConn = "server=localhost;port=3306;Username=root;password=root;Database=2113042122wxh;charset=utf8;";string username;string password;string usernameDB;string passwordDB;// Start is called before the first frame updatevoid Start(){button1.onClick.AddListener(Login);}public void Login(){username = inputField1.text;password = inputField2.text;ConnectDB();SelectDB(username);CloseDB();CompareDB(password);}private void CompareDB(string password){if(username == usernameDB&&password == passwordDB){SceneManager.LoadScene(1);}else{Tip.text = "登錄失敗";}}private void CloseDB(){if (sqlConnection.State.ToString() == "Open"){sqlConnection.Close();Debug.Log(sqlConnection.State);}}private Boolean SelectDB(String n){string strSql = "select * from tb_user where username = '" + n + "';";using (MySqlCommand mySqlCommand = new MySqlCommand(strSql, sqlConnection)){//執行ExecuteNonQuery()方法using (MySqlDataReader reader = mySqlCommand.ExecuteReader()){while (reader.Read()){usernameDB = reader.GetString(1);passwordDB = reader.GetString(2);return true;}}}return false;}public void ConnectDB(){try{sqlConnection = new MySqlConnection(strConn);sqlConnection.Open();Debug.Log(sqlConnection.State);}catch (Exception){throw;}}public void Register(){username = inputField1.text;password = inputField2.text;//連接打開數據庫ConnectDB();//查找用戶名密碼if (SelectDB(username) == true){Tip.text = "用戶存在";}else{if (AddDB(username, password) == 1){Tip.text = "添加成功";}else{Tip.text = "添加失敗";}}CloseDB();}private int AddDB(string n, string p){//寫sql語句string strSql = "insert into tb_user(username,password) values ('" + n + "','" + p + "')";//創建MySQL對象using (MySqlCommand mySqlCommand = new MySqlCommand(strSql, sqlConnection)){//執行ExecuteNonQuery()方法mySqlCommand.ExecuteNonQuery();}return 1;}
}
4.實驗心得
省略