下载网站建设,网站建设业务经理岗位职责,网站建设方案案例,网站建设公司愿景1.Navicat准备 test 数据库#xff0c;并在test数据库下创建 user 数据表#xff0c;预先插入测试数据。 2.启动 Unity Hub 新建一个项目#xff0c;然后在Unity编辑器的 Project视图 中#xff0c;右击新建一个 Plugins 文件夹将连接 MySQL的驱动包 导入#xff08;附加驱…1.Navicat准备 test 数据库并在test数据库下创建 user 数据表预先插入测试数据。 2.启动 Unity Hub 新建一个项目然后在Unity编辑器的 Project视图 中右击新建一个 Plugins 文件夹将连接 MySQL的驱动包 导入附加驱动包链接之后Unity会自动引用它们。 3.在 Hierarchy视图 中点击 Main Camera 对象然后在右边的 Inspector视图 中点击 Add Component添加脚本组件名称如下。 4.AccessMySQL脚本组件连接数据库和执行SQL语句代码如下
...//your code//MySQL 连接对象public static MySqlConnection dbConnection;//连接或关闭 MySQL数据库public void ConnectMySQL(string connectionStr,bool isOpen){if(isOpen){try{dbConnection new MySqlConnection(connectionStr);dbConnection.Open();Debug.Log(连接MySQL数据库成功);}catch(System.Exception e){throw new System.Exception(连接MySQL数据库失败e.Message.ToString());}}else{if(dbConnection ! null){dbConnection.Close();dbConnection.Dispose();dbConnection null;}}}//SQL语句执行方法public DataSet ExecuteQuery(string sqlStr,MySqlConnection ConnectionDB){if(dbConnection.State ConnectionState.Open){//表的集合DataSet dataSet new DataSet();try{MySqlDataAdapter data new MySqlDataAdapter(sqlStr,ConnectionDB);data.Fill(dataSet);}catch(System.Exception e){throw new System.Exception(SQLsqlStr/ne.Message.ToString());}return dataSet;}return null;}// Start is called before the first frame updatevoid Start(){string connectionStr Server 127.0.0.1;port3306;DataBasetest;UIDroot;Pwd1008;ConnectMySQL(connectionStr,true);string sqlQuery select * from user;DataSet ds ExecuteQuery(sqlQuery,dbConnection);// Debug.Log(检索到ds.Tables[0].Rows.Count 条数据);for(int i 0;ids.Tables[0].Rows.Count;i){Debug.Log(姓名 ds.Tables[0].Rows[i][name] 年龄ds.Tables[0].Rows[i][age]);}}
...//your code
5.Unity编辑器的控制台显示执行效果代码正常运行成功