using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.IO;
using System.Data.SqlClient;
using System;
public class UnityTexture_SqlServer : MonoBehaviour
{
public GameObject cube;
public InputField inputField;
static string str_SQL_Server = "server=Yct201902261943\\SQL_SERVER_2012;database=Unity;uid=sa;pwd=123";
SqlConnection con = new SqlConnection(str_SQL_Server);
private void Start()
{
if (cube == null)
{
cube = GameObject.Find("cube");
}
if (inputField == null)
{
inputField = GameObject.Find("inputField").GetComponent<InputField>();
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
SqlOpen();
SQL_Texture();
SqlClose();
Debug.Log("Insert SQL Texture");
}
if (Input.GetKeyDown(KeyCode.W))
{
SqlOpen();
Delete_TextureInfos(inputField.text);
SqlClose();
Debug.Log("Delete SQL Texture");
}
if (Input.GetKeyDown(KeyCode.E))
{
Texture texture = cube.GetComponent<MeshRenderer>().material.mainTexture;
Texture2D t2d = TextureToTexture2D(texture);
byte[] bytesArr = t2d.EncodeToJPG();
string Mesh_Texture = Convert.ToBase64String(bytesArr);
SqlOpen();
Update_TextureInfos(inputField.text, Mesh_Texture);
SqlClose();
Debug.Log("Update SQL Texture");
}
if (Input.GetKeyDown(KeyCode.R))
{
string Mesh_Texture = "";
SqlOpen();
string error = Select_TextureInfos(inputField.text, out Mesh_Texture);
if (string.IsNullOrEmpty(error))
{
byte[] byte_Texture = System.Convert.FromBase64String(Mesh_Texture);
Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB4444, false);
var Is_Success = texture.LoadImage(byte_Texture);
texture.Apply();
cube.GetComponent<MeshRenderer>().material.mainTexture = texture;
}
SqlClose();
Debug.Log("Select SQL Texture");
}
}
private void SQL_Texture()
{
string strPath = @"C:\Users\Administrator\Desktop\Texture";
DirectoryInfo theFolder = new DirectoryInfo(strPath);
FileInfo[] theFiles = theFolder.GetFiles();
for (int i = 0; i < theFiles.Length; i++)
{
string Texture_Name = Path.GetFileNameWithoutExtension(theFiles[i].FullName);
FileStream fs = new FileStream(theFiles[i].ToString(), FileMode.Open);
byte[] byte_Texture = new byte[(int)fs.Length];
fs.Read(byte_Texture, 0, (int)fs.Length);
fs.Close();
string Mesh_Texture = System.Convert.ToBase64String(byte_Texture);
string str_error_meshInfos = Insert_TextureInfos(Texture_Name, Mesh_Texture);
Thread.Sleep(100);
if (str_error_meshInfos != null) { Debug.LogError(str_error_meshInfos); }
}
}
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
private void SqlOpen()
{
SqlClose();
con.Open();
}
private string Insert_TextureInfos(string Texture_Name, string Mesh_Texture)
{
string str_return = null;
try
{
string insert = "INSERT INTO[Unity].[dbo].[Table_Texture] ([Texture_Name],[Mesh_Texture]) values( '" + Texture_Name + "','" + Mesh_Texture + "')";
SqlCommand com_insert = new SqlCommand(insert, con);
com_insert.ExecuteNonQuery();
}
catch (Exception e)
{
str_return = e.ToString();
}
return str_return;
}
private string Delete_TextureInfos(string Texture_Name)
{
string str_return = null;
try
{
string delete = "DELETE FROM [Unity].[dbo].[Table_Texture] WHERE [Texture_Name] = '" + Texture_Name + "'";
SqlCommand com_insert = new SqlCommand(delete, con);
com_insert.ExecuteNonQuery();
}
catch (Exception e)
{
str_return = e.ToString();
}
return str_return;
}
private string Update_TextureInfos(string Texture_Name, string Mesh_Texture)
{
string str_return = null;
try
{
string update = "UPDATE [Unity].[dbo].[Table_Texture] SET [Mesh_Texture] = '" + Mesh_Texture + "' WHERE [Texture_Name] = '" + Texture_Name + "'";
SqlCommand com_insert = new SqlCommand(update, con);
com_insert.ExecuteNonQuery();
}
catch (Exception e)
{
str_return = e.ToString();
}
return str_return;
}
private string Select_TextureInfos(string Texture_Name, out string Mesh_Texture)
{
string str_return = "";
Mesh_Texture = null;
try
{
string select = "SELECT * FROM [Unity].[dbo].[Table_Texture] WHERE [Texture_Name] = '" + Texture_Name + "'";
SqlCommand com_select = new SqlCommand(select, con);
com_select.ExecuteNonQuery();
SqlDataReader read = com_select.ExecuteReader();
if (read.Read())
{
Mesh_Texture = read["Mesh_Texture"].ToString();
}
read.Close();
com_select.Dispose();
}
catch (Exception e)
{
str_return = e.ToString();
Mesh_Texture = null;
}
return str_return;
}
private void SqlClose()
{
con.Close();
}
}
如果在Unity调试测试都是可以的(本机数据库是SQL Server 2012 ),但是build后,运行生成的exe文件时却无法连接数据库。
请把I18N.CJK.dll,I18N.dll ,I18N.West.dll 这个三个DLL 加入到Assets文件夹下,然后build就可以了。
三个dll的位置:C:\Program Files \Unity\Editor\Data\Mono\lib\mono\unity(本机),或者 :链接:https://pan.baidu.com/s/1iiUWGSCR7mgnAwSXexvD7g 提取码:qq53
支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
浙公网安备 33010602011771号