我想使用资源文件夹加载 3D 模型。我创建了一个sql数据库来存储地址。在这种情况下,我将文件“deer-3ds”存储在文件夹“Models”中,并将这些信息保存在 sql 中名为“modeladdress”的表中。 所以请帮助我更正我的代码。我知道这是 100% 错误的,但我不知道如何解决。谢谢。
using UnityEngine;
using System.Collections;
using System;
using System.Data;
using Mono.Data.Sqlite;
public class addobject : MonoBehaviour {
// Use this for initialization
void Start () {
//GameObject deer=Instantiate(Resources.Load("deer-3d.bak",typeof(GameObject)))as GameObject;
// GameObject instance = Instantiate(Resources.Load("Models/deer-3ds", typeof(GameObject))) as GameObject;
string conn = "URI=file:" + Application.dataPath + "/modeladdress.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT ordinary,foldername, filename " + "FROM modeladdress";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read ()) {
int ordinary = reader.GetInt32 (0);
string foldername = reader.GetString (1);
string filename = reader.GetString (2);
string path = foldername + "/" + filename;
//Debug.Log( "value= "+value+" name ="+name+" random ="+ rand);
GameObject instance = Instantiate(Resources.Load(path, typeof(GameObject))) as GameObject;
instance.SetActive (true);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
// Update is called once per frame
void Update () {
// GameObject instance = Instantiate(Resources.Load("Models/deer-3ds", typeof(GameObject))) as GameObject;
// instance.SetActive (true);
}
}
最佳答案
首先,您在数据库管理系统中使用的是 SQLite,而不是 MySQL。其次,您编写查询的方式,
string sqlQuery = "SELECT ordinary,foldername, filename " + "FROM modeladdress";
将为每个 模型返回普通
、文件夹名
和文件名
。您需要使用 WHERE
子句来准确指定要使用的模型。因此,在实际执行查询之前,您需要通过某种方式知道要从数据库中查询哪个模型,在这种情况下,为什么还要查询数据库呢?无论如何,您将不得不存储一些唯一标识符,这样数据库就解决不了任何问题。
现在关于您编写的实际代码,它似乎是正确的(即它应该返回您想要的)。问题一定是您的表是空的,返回的值不正确,或者对象在错误的位置实例化,因此您认为它不起作用。如果你想要一个更具体的答案,你必须用你面临的具体问题来评论这个答案(即具体是什么“错误”?)。
关于mysql - 使用 Resource 文件夹和 Mysql 在 Unity 中加载 3D 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41081740/