怎么用vs编辑unity动作编辑不能引用using System.Data.SqlClient

当前位置: >
Unity3D连接Sql Server数据库详解
时间: 14:13 来源:Unity之家 作者:unity.jb51.net 浏览:
最近一些朋友问我u3d PC端&连接数据库的问题。现在抽出一点时间,为大家写一个连接数据库以及打印出数据表中的数据案例首先,我们需要添加一个外部引用&& &&然后在VS 中添加引用数据库Test的表以及数据表中的数据为大家贴上图:好了!准备工作我们已经做好啦~~(最后我们会提供源码文件下载地址)接下来我们来试着连接数据库。我就直接贴出代码了:using&UnityE
using&System.C
using&System.Data.SqlC
public&class&Conect&:&MonoBehaviour
&&&&///&&summary&
&&&&///&1.Data&Source选择的服务器。这里使用“.”指本机。也可使用IP地址
&&&&///&2.Initial&Catalog=Test&&为&指定名字为Test的数据库
&&&&///&3.登录用户名Id为sa(这里不用多说)
&&&&///&4.Password为自己设定的。也可修改
&&&&///&&/summary&
&&&&SqlConnection&sqlcon&=&new&SqlConnection(&Data&Source=.;Initial&Catalog=TUser&Id=Password=123456;&);
&&&&SqlCommand&cmd&=&new&SqlCommand();
&&&&private&string&
&&&&void&Start()
&&&&&&&&ConectSql();
&&&&///&&summary&
&&&&///&连接数据库
&&&&///&&/summary&
&&&&void&ConectSql()
&&&&&&&&try
&&&&&&&&&&&&cmd.Connection&=&
&&&&&&&&&&&&mandType&=&mandType.T
&&&&&&&&&&&&mandText&=&&SELECT&age&FROM&Presonal&;&//&因为我的数据库里面只有一条数据,所以这里就这么写了
&&&&&&&&&&&&SqlDataAdapter&da&=&new&SqlDataAdapter(cmd);
&&&&&&&&&&&&da.SelectCommand.Connection.Open();
&&&&&&&&&&&&tempip&=&da.SelectCommand.ExecuteScalar().ToString();
&&&&&&&&&&&&da.SelectCommand.Connection.Close();
&&&&&&&&&&&&sqlcon.Close();
&&&&&&&&&&&&sqlcon.Dispose();
&&&&&&&&&&&&Debug.Log(tempip);
&&&&&&&&catch&(System.Exception&e)
&&&&&&&&&&&&print(e);
}项目源码和数据库下载地址:将上面的脚本挂载到物件上,运行。就会看到效果啦。。。
(责任编辑:向阳)
免责声明:Unity之家部分内容来源于互联网,如有侵权,请联系我们,本站将立即进行处理。
猜你也喜欢看这些 ??????
其他类型的数据库编程 ??????急用求助unity 3d 如何连接sql server?_unity3d吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:43,290贴子:
急用求助unity 3d 如何连接sql server?收藏
最近做作业时碰见的一个问题需要将游戏是=时产生的一个分数变量coins导入数据库game,部分代码如下:大神帮我看看有神马问题using UnityEusing Susing System.Dusing System.Cusing System.Data.SqlCpublic class mark : MonoBehaviour {
// Use this for initialization
void Start () {
SqlConnection sqlcon = new SqlConnection (
//&server=PC-VZMW;& +
&data source=user id=password=123456; initial catalog =game&); //name of database
SqlCommand cmd=new SqlCommand();
cmd.Connection =
mandType = mandType.T
//sql connection
mandText = & USE game GO
EXEC insertmark @mark=&;//执行存储过程insertmark
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.SelectCommand.Connection.Open ();
string tempip =
da.SelectCommand.ExecuteScalar ().ToString ();
da.SelectCommand.Connection.Close ();
print (&get& + tempip);
void CollectCoin(Collider2D coinCollider)
Destroy(coinCollider.gameObject);
AudioSource.PlayClipAtPoint(coinCollectSound, transform.position);
void DisplayCoinsCount()
//游戏界面显示分数
Rect coinIconRect = new Rect(10, 10, 32, 32);
GUI.DrawTexture(coinIconRect, coinIconTexture);
GUIStyle style = new GUIStyle();
style.fontSize = 30;
style.fontStyle = FontStyle.B
style.normal.textColor = Color.
Rect labelRect = new Rect(coinIconRect.xMax, coinIconRect.y, 60, 32);
GUI.Label(labelRect, coins.ToString(), style);
// Update is called once per frame
void Update () {
在这之前,我先试了一下将一个数据从数据库里抓出来,编译没有报错,但是显示不出结果,这是为神马呢?代码如下:using UnityEusing Susing System.Cusing System.Data.SqlCpublic class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
SqlConnection sqlcon = new SqlConnection (
//&server=PC-VZMW;& +
&data source=user id=password=123456; initial catalog =stud&); //name of database
SqlCommand cmd=new SqlCommand();
cmd.Connection =
mandType = mandType.T
//sql connection
mandText = & select* from student where sno=;&;
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.SelectCommand.Connection.Open ();
string tempip =
da.SelectCommand.ExecuteScalar ().ToString ();
da.SelectCommand.Connection.Close ();
print (&this is text area!& + tempip);
// Update is called once per frame
void Update () {
直接连数据库作死 反编译游戏源码 数据库直接被弄掉
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或这几天一直在研究热更新方案
主要思路是:
1.先将代码打包成dll,然后用unity 打包成assetsbundle,
2.WWW加载进入主程序,
3使用System.Reflection.Assembly来创建程序集,
4.然后通过GetType(className),来获取这个类
5.AddComponent进入主程序,加载的dll就执行起来了。
ExportAssetBundles.cs
//打包工具,该工具是网上找来都。谢谢作者!
public class ExportAssetBundles : MonoBehaviour {
//在Unity编辑器中添加菜单
[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void ExportResource()
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
if (path.Length != 0)
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | pleteAssets, BuildTarget.StandaloneWindows);
1234567891011121314151617181920
//打包工具,该工具是网上找来都。谢谢作者!public class ExportAssetBundles : MonoBehaviour {&&&&&//在Unity编辑器中添加菜单&&&&&&[MenuItem("Custom Editor/Create AssetBunldes ALL")]&&&&static void ExportResource()&&&&{&&&&&&&&// 打开保存面板,获得用户选择的路径&&&&&&&&&&string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");&&&&&&&&&if (path.Length != 0)&&&&&&&&{&&&&&&&&&&&&// 选择的要保存的对象&&&&&&&&&&&&&&Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);&&&&&&&&&&&&//打包&&&&&&&&&&&&&&BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);&&&&&&&&&&&&&&}&&&&}&&}
using UnityE
using System.C
using System.R
//代码加载器
public class Index : MonoBehaviour
private WWW
public static WWW uiWWW;
private System.Reflection.A
// Use this for initialization
void Start ()
StartCoroutine(loadScript());
private IEnumerator loadScript()
//加载我的代码资源
www = new WWW("http://localhost/Main.assetbundle");
AssetBundle bundle = www.assetB
TextAsset asset = bundle.Load("Main", typeof(TextAsset)) as TextA
assembly = System.Reflection.Assembly.Load(asset.bytes);
Assembly[] assLis = System.AppDomain.CurrentDomain.GetAssemblies();
System.Type script = assembly.GetType("Main");
gameObject.AddComponent(script);
12345678910111213141516171819202122232425262728293031323334353637
using UnityEngine;using System.Collections;using System.Reflection;&//代码加载器public class Index : MonoBehaviour{&&&&&private WWW www;&&&&&public static WWW uiWWW;&&&&&private System.Reflection.Assembly assembly;& // Use this for initialization void Start () &&&&{&&&&&&&&StartCoroutine(loadScript()); }&&&&&private IEnumerator loadScript()&&&&{&&&&&&&&//加载我的代码资源&&&&&&&&www = new WWW("http://localhost/Main.assetbundle");&&&&&&&&yield return www;&&&&&&&&&AssetBundle bundle = www.assetBundle;&&&&&&&&TextAsset asset = bundle.Load("Main", typeof(TextAsset)) as TextAsset;&&&&&&&&assembly = System.Reflection.Assembly.Load(asset.bytes);&&&&&&&&&Assembly[] assLis = System.AppDomain.CurrentDomain.GetAssemblies();&&&&&&&&&System.Type script = assembly.GetType("Main");&&&&&&&&&gameObject.AddComponent(script);&&&&}}
因为在加载的时候遇见安全沙箱问题,所以我将这个策略文件记录下来,方便下次复制粘贴
crossdomain.xml
&?xml version="1.0"?&
&cross-domain-policy&
&site-control permitted-cross-domain-policies=”master-only” /&
&allow-access-from domain="" /&
&allow-access-from domain="*"/&
&/cross-domain-policy&
&?xml version="1.0"?&&cross-domain-policy&&site-control permitted-cross-domain-policies=”master-only” /&&allow-access-from domain="" /&&allow-access-from domain="*"/&&/cross-domain-policy&
本地调试程序时解决跨域问题的方法:
Edit-&Project Settings-&Eidtor
刚开始的时候想使用序列化来存储一些数据,但是后来却连一个很简单的类序列化dll里面都没法获得
官方对象序列号
[MenuItem("Custom Editor/WriteSpriteData")]
static void FileWriteSpriteData()
TextAsset textasset = AssetDatabase.LoadAssetAtPath("Assets/Resources/Packer/Packer.txt", typeof(TextAsset)) as TextA
Atlas atlas = ScriptableObject.CreateInstance&Atlas&();
//Json其实是NGUIJson这个类,我只是把他提出来。改了个名字
atlas.mList = Json.LoadSpriteData(textasset as TextAsset);
if (atlas.mList == null)
string path = "Assets/Resources/Packer/Packer.asset";
AssetDatabase.CreateAsset(atlas, path);
//Atlas是一个只有一个mList属性都类 mList = new List&UISpriteData&();
Object o = AssetDatabase.LoadAssetAtPath(path, typeof(Atlas));
Object texture = AssetDatabase.LoadAssetAtPath("Assets/Resources/Packer/Packer.mat", typeof(Material));
Object[] t = {texture};
BuildPipeline.BuildAssetBundle(o, t, "Assets/Resources/Packer/Packer.assetbundle");
//AssetDatabase.DeleteAsset(path);
12345678910111213141516171819202122232425
[MenuItem("Custom Editor/WriteSpriteData")]&&&&static void FileWriteSpriteData()&&&&{&&&&&&&&TextAsset textasset = AssetDatabase.LoadAssetAtPath("Assets/Resources/Packer/Packer.txt", typeof(TextAsset)) as TextAsset;&&&&&&&&&Atlas atlas = ScriptableObject.CreateInstance&Atlas&();//Json其实是NGUIJson这个类,我只是把他提出来。改了个名字&&&&&&&&atlas.mList = Json.LoadSpriteData(textasset as TextAsset);&&&&&&&&&if (atlas.mList == null)&&&&&&&&&&&&return;&&&&&&&&&string path = "Assets/Resources/Packer/Packer.asset";&&&&&&&&&AssetDatabase.CreateAsset(atlas, path);//Atlas是一个只有一个mList属性都类 mList = new List&UISpriteData&();&&&&&&&&Object o = AssetDatabase.LoadAssetAtPath(path, typeof(Atlas));&&&&&&&&Object texture = AssetDatabase.LoadAssetAtPath("Assets/Resources/Packer/Packer.mat", typeof(Material));&&&&&&&&&Object[] t = {texture};&&&&&&&&&BuildPipeline.BuildAssetBundle(o, t, "Assets/Resources/Packer/Packer.assetbundle");&&&&&&&&&&&&&&&&//AssetDatabase.DeleteAsset(path);&&&&}
这是使用序列化数据的加载方式,在不用反射的情况下,下面代码加载能够成功,但是使用了反射,下面的代码就加载不成功了。这个问题我也很费解,暂时我没办法解决
读取序列化对象
IEnumerator LoadAtlas()
www = new WWW("http://localhost/Packer.assetbundle");
//WoodenAtlas.assetbundle
//Packer.assetbundle
//用来断点都时候看看里面所包含都数据
Object[] os = www.assetBundle.LoadAll();
Material mete = www.assetBundle.Load("Packer", typeof(Material)) as M
Atlas atlas = www.assetBundle.mainAsset as A
GameObject go = new GameObject("UIAtlas");
UIAtlas uiatlas = go.AddComponent&UIAtlas&();
uiatlas.spriteMaterial =
uiatlas.spriteList = atlas.mL
GameObject sprite = new GameObject("Sprite");
UISprite ui = NGUITools.AddChild&UISprite&(sprite);
ui.atlas =
ui.spriteName = "dynamite";
Debug.Log("Load");
www.assetBundle.Unload(false);
www.Dispose();
1234567891011121314151617181920212223242526
IEnumerator LoadAtlas()&&&&{&&&&&&&&www = new WWW("http://localhost/Packer.assetbundle");&&&&&&&&//WoodenAtlas.assetbundle&&&&&&&&//Packer.assetbundle&&&&&&&&yield return www;//用来断点都时候看看里面所包含都数据&&&&&&&&Object[] os = www.assetBundle.LoadAll();&&&&&&&&&Material mete = www.assetBundle.Load("Packer", typeof(Material)) as Material;&&&&&&&&Atlas atlas = www.assetBundle.mainAsset as Atlas;&&&&&&&&GameObject go = new GameObject("UIAtlas");&&&&&&&&UIAtlas uiatlas = go.AddComponent&UIAtlas&();&&&&&&&&uiatlas.spriteMaterial = mete;&&&&&&&&uiatlas.spriteList = atlas.mList;&&&&&&&&&GameObject sprite = new GameObject("Sprite");&&&&&&&&UISprite ui = NGUITools.AddChild&UISprite&(sprite);&&&&&&&&ui.atlas = uiatlas;&&&&&&&&ui.spriteName = "dynamite";&&&&&&&&Debug.Log("Load");&&&&&&&&&www.assetBundle.Unload(false);&&&&&&&&www.Dispose();&&&&&&&&&&&&}
因为要看一下代码的执行效率,所以我寻找到了这个类。感觉还可以。使用josn数据,mat文件创建一个UIAtlas的时间大概是30毫秒左右。
System.Diagnostics.S
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
System.Diagnostics.Stopwatch;Stopwatch stopWatch = new Stopwatch();stopWatch.Start();Thread.Sleep(10000);stopWatch.Stop();
我使用没有任何Unity环境以外代码来实现壳的制作(我们暂且将其称为Index,其实他就是上面的Index类,代码少得可怜。)
然后主程序是在另一个Unity项目中(这个项目在发布的时候打包成Main.dll)
Main.dll项目通过上面的Index来加载,然后添加到一个GameObject上,主程序的Awake()方法就会执行(Awake是整个程序的主入口)
这个时候所有的资源加载都会在Main.dll里面完成。
在这个过程中,遇到了一个比较麻烦的问题就是,我打包的一些UIAtlas.prefab文件上的UIAtlas这个类,无法找到。
这让我有一些无法理解,因为NGUI的代码已经打包进入了Main.dll,那么为什么我加载prefab的时候,却找不到UIAtlas这个类呢?
最后我只能动态的制作UIAtlas对象来完成这样工作。
那么这样的话,以后做界面,做任何prefab都不能绑定脚本了。都只能加载到内存中动态AddComponent了。这样界面也得用配置文件了。
不过对于我这种从页游转过来的程序,这到不是问题,我有现成的界面编辑器(我博客里有),直接生成XML在游戏中进行组装了。对于能够热更新来说,这点麻烦,其实应该不算麻烦了。
动态生成UIAtlas后,创建了几个Sprite、Button,基本的功能都已经实现,说明这个解决方案是可行的。接下来我将把这个方案运用到我的项目中,更加全面的去实验一下。
这样设计的优点:
1、对IOS的打包也是比较方便。打包IOS 直接拿Main项目打包就可以了。因为不需要热更新了。把代码打包在本地就行了。
2、打包Android项目的时候发布apk只需要发布Index,项目发布版本和没写代码一样大,想到这里我想吐槽一下,Unity就算不写任何代码,发布一个apk也得有7M左右。这也太大了点吧。我可啥都没做啊。
1、哪位大神能给我说说上面我遇到的那个问题,为什么找不到绑定在prefab上的类呢?这是程序集的问题么?哎,刚转C#的人伤不起。
[…] 3、这也是最主要的,如果你看过我上一篇文章《Unity代码热更新解决方案测试结果总结》你就会知道我为什么要这么做了,都是泪啊。。 […]VS2008中为什么没有system.data.sqlclient的引用,难道需要自己打上去????_百度知道
VS2008中为什么没有system.data.sqlclient的引用,难道需要自己打上去????
我有更好的答案
自打默认引用都基本 要连接sql server数据库 using引用自加
那为什么我加上去以后没有他内部的方法呢?
其他类似问题
为您推荐:
vs2008的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁VS2008提示说没有using System.Data.Linq.SqlClient
[问题点数:20分,结帖人yilaozhuang]
VS2008提示说没有using System.Data.Linq.SqlClient
[问题点数:20分,结帖人yilaozhuang]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2010年 总版技术专家分年内排行榜第一2009年 总版技术专家分年内排行榜第一
2011年 总版技术专家分年内排行榜第二
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 unity动作编辑 的文章

 

随机推荐