U3D连接数据

来源:互联网 发布:r软件sep“,” 编辑:程序博客网 时间:2024/06/05 10:43

1、和在.net中访问数据库中是一样的,不同点点,因为U3D中不支持.NET那种自带的配置文件,所以需要我们自己写XML配置文件信息。

2、还有我们用Application.dataPath的方法得到程序的启动路径,Application.dataPath在编辑环境下和在生成exe之后是不一样的,编辑状态下,返回的是Asset位置,生成后Assets不再存在,返回位置是_Data文件夹,所以如果你想加载exe所在位置的txt文本,要先对Application.dataPath进行处理:

<1>、方法如下:得到程序的启动目录所在位置,和exe同级别

 public static string GetAppPath()
        {
            string strAppPath = Application.dataPath;
            int num = strAppPath.LastIndexOf("/");
            strAppPath = strAppPath.Substring(0, num);
            return strAppPath;
        }

<2>、得到XML文件路径,要把XML文件放到Asset同级别下。在发布之后,放到与exe.同级别下。

public static string GetXMLPath()
        {
            if (GetAppPath() != "")
            {
                string AppPath = GetAppPath();
                return AppPath + "/Configuration.xml";
            }
            else
            {
                return "";
            }
        }


<3>、得到节点写的配置信息

public static string GetConfigInfo()
        {
            string strReturn = string.Empty;
            XmlDocument xml = new XmlDocument();
            xml.Load(GetXMLPath());
            XmlNode xnDs = xml.SelectSingleNode("/configuration/connectionStrings");
            if (xnDs != null)
            {
                strReturn = xnDs.InnerText.Trim();
            }
            return strReturn;
        }

3因为我引入mono下的DLL老是出问题,所以,我就自己封装个访问的SQLHelper类在VS中,使用的System.Data是.NET的本本身的

4、发布时要注意(个人感觉是unity3d本身设计的问题)还有同时引入I18N.dll、I18N.West.dll、I18N.CJK.dll三个dll放到asset下,一起打包才可以的。目录为:

 C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\unity

0 0
原创粉丝点击