實驗1: 仿真系統(tǒng)的UI主界面設(shè)計
1.實驗?zāi)康?/h2>
(1)熟悉Unity中UI界面的設(shè)計與編寫;
(2)熟悉UI界面中場景轉(zhuǎn)換,UI與場景內(nèi)容相互關(guān)聯(lián)的方式。
(3)熟悉Unity中MySQL數(shù)據(jù)庫的操作
2.實驗內(nèi)容
新建一個Unity場景,在此場景中實現(xiàn)如下功能:
(1)自行設(shè)計一個登錄、注冊UI界面;
(2)添加數(shù)據(jù)庫的動態(tài)鏈接庫文件,提前設(shè)計數(shù)據(jù)庫表格(自行設(shè)計);
(3)連接數(shù)據(jù)庫,實現(xiàn)增、刪、改、查等數(shù)據(jù)庫對用戶的操作;
(4)UI界面中包括canvas、Image、RawImage、Button等多種UI元素;
(5)實現(xiàn)點擊Play按鈕轉(zhuǎn)換場景,點擊Exit退出游戲的功能;
(6)實現(xiàn)主界面添加音量滑動桿、靜音等功能,添加背景音樂和音效音樂;
(7)為UI界面單獨設(shè)置一個場景,并設(shè)置編號為0。
3.實驗步驟
第一步:創(chuàng)建UI界面
(1)創(chuàng)建畫布,附加背景
創(chuàng)建canvas作為畫布,接著創(chuàng)建Raw Image和Image去實現(xiàn)基礎(chǔ)背景的搭建
?附加圖片,并拖拽到和畫布一樣大小,背景設(shè)計完成。
(2)添加交互組件
首先添加InputField組件 作為我們的輸入框,去實現(xiàn)賬號密碼框的設(shè)計
右鍵-->UI--->InputField
可以修改下面的TEXT去修改 輸入框的默認內(nèi)容
接著,添加Text 去搭建一個基本的登錄框
添加Button組件,設(shè)計登錄,注冊,退出按鈕。
一個簡易的登錄注冊頁面完成
接著,添加Dropdown,Toggle,Slider組件 進行排版得到完整的UI界面
第二步:實現(xiàn)交互功能
第四步,添加腳本代碼實現(xiàn)功能
- 添加背景音樂,制作靜音,調(diào)節(jié)音量功能
右鍵--->Audio ?添加一個音樂組件
導(dǎo)入音樂素材,拖入Audio組件中
添加腳本實現(xiàn)音量靜音與控制
勾選toggle實現(xiàn),靜音
?private?void?PlayMusic(bool?arg0)
?{
?????if?(arg0)
?????{
?????????ads.Pause();
?????}
?????else
?????{
?????????ads.Play();
?????}
?}
根據(jù)布爾值 判斷是否勾選,如果勾選了靜音按鈕,就關(guān)閉音樂
拖動slider實現(xiàn)控制音量
?private?void?ChangeVolume(float?arg0)
?{
?????ads.volume = arg0;
?}
2.連接數(shù)據(jù)庫,實現(xiàn)登錄注冊功能
第一步:在官網(wǎng)下載插件MySQL Connector Net
下載安裝完成后,添加數(shù)據(jù)庫的動態(tài)鏈接庫文件
第二步:創(chuàng)建一個空對象,附加腳本實現(xiàn)連接文章來源:http://www.zghlxwxcb.cn/news/detail-752761.html
/*
實現(xiàn)登錄注冊功能
登錄:獲取輸入框中的字符串--->連接并打開數(shù)據(jù)庫--->查找用戶名密碼
Y = --->對比密碼-->Y=-->關(guān)閉數(shù)據(jù)庫--->登錄成功
Y = --->對比密碼-->N=-->關(guān)閉數(shù)據(jù)庫--->登錄失敗
N = --->關(guān)閉數(shù)據(jù)庫--->登錄失敗
注冊:獲取輸入框中的字符串--->連接并打開數(shù)據(jù)庫--->查找用戶名密碼
Y =>關(guān)閉數(shù)據(jù)庫 --->注冊失敗
N => 添加用戶名密碼--->關(guān)閉數(shù)據(jù)庫--->注冊成功
分解:
1.連接并打開數(shù)據(jù)庫
2.查找用戶名密碼
3.關(guān)閉數(shù)據(jù)庫
4.對比密碼
5.添加用戶名和密碼
接口:登錄1234 注冊1235
對象:GUI對象 輸入框*2 按鈕*2,文本
數(shù)據(jù)庫: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 update
void 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))
{
//執(zhí)行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;
//連接打開數(shù)據(jù)庫
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 + "')";
//創(chuàng)建MySQL對象
using (MySqlCommand mySqlCommand = new MySqlCommand(strSql, sqlConnection))
{
//執(zhí)行ExecuteNonQuery()方法
mySqlCommand.ExecuteNonQuery();
}
return 1;
}
}
4.實驗心得
省略文章來源地址http://www.zghlxwxcb.cn/news/detail-752761.html
到了這里,關(guān)于Unity UI設(shè)計 軟件構(gòu)造實驗報告的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!