【问题标题】:Load 3D model in Unity using Resource folder and Mysql使用 Resource 文件夹和 Mysql 在 Unity 中加载 3D 模型
【发布时间】:2016-12-11 00:18:19
【问题描述】:

我想使用资源文件夹加载 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);


    }
}

【问题讨论】:

  • 有什么问题?发生了什么“错误”?

标签: mysql unity3d model


【解决方案1】:

首先,您在数据库管理系统中使用的是 SQLite,而不是 MySQL。其次,您编写查询的方式,

string sqlQuery = "SELECT 普通,文件夹名,文件名" + "FROM 型号地址";

将为每个模型返回ordinaryfoldernamefilename。您需要使用WHERE 子句来精确指定要使用的模型。因此,在实际执行查询之前,您需要某种方法来知道要从数据库中查询哪个模型,在这种情况下,为什么还要查询数据库呢?无论如何,您将不得不存储一些唯一标识符,因此数据库无法解决任何问题。

现在关于您编写​​的实际代码,它似乎是正确的(即它应该返回您想要的内容)。问题一定是您的表为空,返回的值不正确,或者对象在不正确的位置实例化,因此您认为它不起作用。如果您想要更具体的答案,则必须针对您面临的具体问题(即“错误”具体是什么?)对此答案发表评论。

【讨论】:

    猜你喜欢
    • 2016-04-07
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2019-08-24
    • 2012-06-24
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多