Skip to main content

Unity+PHP+MYSQL

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Text;
public class ZNX: MonoBehaviour {
public GUISkin ChinSkin;

private Vector2 chatScrollPosition;
bool QJ_B_Sjx=false; //收件箱
bool QJ_B_Fjx=false; //发件箱
bool QJ_B_TZ=false; //通知
bool QJ_B_XZNX=false; //写站内信
bool QJ_B_HF=false; //回复

int QJ_name_Selection=-1;
int QJ_int_Selection=-1;

string QJ_string_id="";
string QJ_string_name="";
string QJ_string_title="";
string QJ_string_body="";
string QJ_string_datetime="";

string QJ_string_G_id="";
string QJ_string_G_name="";
string QJ_string_G_title="";
string QJ_string_G_body="";
string QJ_string_G_datetime="";

string [] SQL_id;
string [] SQL_name;
string [] SQL_title;
string [] SQL_body;
string [] SQL_datetime;

static string stringToEdit_name = "";
static string stringToEdit_title = "";
static string stringToEdit_body = "";
private Rect windowRect =new Rect (20,100, 500, 400); //站内信的大小
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
public void OnGUI(){
GUI.skin = ChinSkin;

if(GUI.Button(new Rect(20,40,80,30), "收件箱"))
{
StartCoroutine(DoGetMultitermData());
if(QJ_B_Sjx)
{
QJ_B_Sjx=false;
}
else
{
QJ_B_HF=false;
QJ_B_XZNX=false;
QJ_B_Fjx=false;
QJ_B_TZ=false;
QJ_B_Sjx=true;
}
}
if(GUI.Button(new Rect(110,40,80,30), "发件箱"))
{
if(QJ_B_Fjx)
{
QJ_B_Fjx=false;
}
else
{
QJ_B_HF=false;
QJ_B_XZNX=false;
QJ_B_Sjx=false;
QJ_B_TZ=false;
QJ_B_Fjx=true;
}
}
if(GUI.Button(new Rect(200,40,80,30), "通知"))
{

if(QJ_B_TZ)
{
QJ_B_TZ=false;
}
else
{
QJ_B_HF=false;
QJ_B_XZNX=false;
QJ_B_Sjx=false;
QJ_B_Fjx=false;
QJ_B_TZ=true;
}
}
if(QJ_B_Sjx)
{
windowRect = GUI.Window (0, windowRect, QJ_B_Sjx_DoMyWindow, "收件箱");
}
if(QJ_B_Fjx)
{
windowRect = GUI.Window (0, windowRect, QJ_B_Fjx_DoMyWindow ,"发件箱");
}
if(QJ_B_TZ)
{
windowRect = GUI.Window (0, windowRect, QJ_B_TZ_DoMyWindow, "通知");
}
if(QJ_B_XZNX)
{
windowRect = GUI.Window (0, windowRect, QJ_B_XZNX_DoMyWindow,"写站内信");
}
if(QJ_B_HF)
{
windowRect = GUI.Window (0, windowRect, QJ_B_HF_DoMyWindow,"回复");
}

}

//********************
//———面板———<

void QJ_B_Sjx_DoMyWindow (int windowID) //收件箱面板
{
GUI.Label (new Rect (60, 15, 100, 30), "用户");
GUI.Label (new Rect (250,15, 100, 30), "主题");
GUI.Label(new Rect (405,15, 100, 30), "删除");

GUILayout.BeginArea (new Rect(30, 35,430, 300));
GUILayout.BeginArea (new Rect(0, 0, 100, 300));
chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (120), GUILayout.Height (300));
QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_name, 1);
if(QJ_name_Selection!=QJ_int_Selection)
{
QJ_string_id=SQL_id[QJ_name_Selection];
QJ_string_name=SQL_name[QJ_name_Selection];
QJ_string_title=SQL_title[QJ_name_Selection];
QJ_string_body=SQL_body[QJ_name_Selection];
QJ_string_datetime=SQL_datetime[QJ_name_Selection];

QJ_B_HF=true;
QJ_int_Selection=QJ_name_Selection;
QJ_B_Sjx=false;
}
GUILayout.EndScrollView ();
GUILayout.EndArea();

GUILayout.BeginArea (new Rect(100, 0, 270, 300));
chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (290), GUILayout.Height (300));
QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_title, 1);
if(QJ_name_Selection!=QJ_int_Selection)
{
QJ_string_id=SQL_id[QJ_name_Selection];
QJ_string_name=SQL_name[QJ_name_Selection];
QJ_string_title=SQL_title[QJ_name_Selection];
QJ_string_body=SQL_body[QJ_name_Selection];
QJ_string_datetime=SQL_datetime[QJ_name_Selection];

QJ_B_HF=true;
QJ_int_Selection=QJ_name_Selection;
QJ_B_Sjx=false;
}
GUILayout.EndScrollView ();
GUILayout.EndArea();

GUILayout.BeginArea (new Rect(370, 0, 60, 300));
chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (60), GUILayout.Height (300));
QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_id, 1);
if(QJ_name_Selection!=QJ_int_Selection)
{
QJ_string_id=SQL_id[QJ_name_Selection];
//~ QJ_string_name=SQL_name[QJ_name_Selection];
//~ QJ_string_title=SQL_title[QJ_name_Selection];
//~ QJ_string_body=SQL_body[QJ_name_Selection];
//~ QJ_string_datetime=SQL_datetime[QJ_name_Selection];

StartCoroutine(DeleteData());

//~ QJ_B_Fjx=true;
//~ QJ_int_Selection=QJ_name_Selection;
QJ_B_Sjx=true;
}
GUILayout.EndScrollView ();
GUILayout.EndArea();

GUILayout.EndArea();

if(GUI.Button(new Rect(220,340,80,30), "写站内信"))
{
QJ_B_XZNX=true;
QJ_B_Fjx=false;
}
GUI.DragWindow (new Rect (0,0, 10000, 30));
}

void QJ_B_Fjx_DoMyWindow(int windowID) //发件箱面板
{
//~ GUI.Label (new Rect (60, 15, 100, 30), "用户");
//~ GUI.Label (new Rect (250,15, 100, 30), "主题");
//~ GUI.Label(new Rect (405,15, 100, 30), "删除");

//~ GUILayout.BeginArea (new Rect(30, 35,430, 300));
//~ GUILayout.BeginArea (new Rect(0, 0, 100, 300));
//~ chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (120), GUILayout.Height (300));
//~ QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_name, 1);
//~ if(QJ_name_Selection!=QJ_int_Selection)
//~ {
//~ QJ_string_id=SQL_id[QJ_name_Selection];
//~ QJ_string_name=SQL_name[QJ_name_Selection];
//~ QJ_string_title=SQL_title[QJ_name_Selection];
//~ QJ_string_body=SQL_body[QJ_name_Selection];
//~ QJ_string_datetime=SQL_datetime[QJ_name_Selection];

//~ QJ_B_HF=true;
//~ QJ_int_Selection=QJ_name_Selection;
//~ QJ_B_Sjx=false;
//~ }
//~ GUILayout.EndScrollView ();
//~ GUILayout.EndArea();

//~ GUILayout.BeginArea (new Rect(100, 0, 270, 300));
//~ chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (290), GUILayout.Height (300));
//~ QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_title, 1);
//~ if(QJ_name_Selection!=QJ_int_Selection)
//~ {
//~ QJ_string_id=SQL_id[QJ_name_Selection];
//~ QJ_string_name=SQL_name[QJ_name_Selection];
//~ QJ_string_title=SQL_title[QJ_name_Selection];
//~ QJ_string_body=SQL_body[QJ_name_Selection];
//~ QJ_string_datetime=SQL_datetime[QJ_name_Selection];

//~ QJ_B_HF=true;
//~ QJ_int_Selection=QJ_name_Selection;
//~ QJ_B_Sjx=false;
//~ }
//~ GUILayout.EndScrollView ();
//~ GUILayout.EndArea();

//~ GUILayout.BeginArea (new Rect(370, 0, 60, 300));
//~ chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (60), GUILayout.Height (300));
//~ QJ_name_Selection=GUILayout.SelectionGrid (QJ_name_Selection, SQL_id, 1);
//~ if(QJ_name_Selection!=QJ_int_Selection)
//~ {
//~ QJ_string_id=SQL_id[QJ_name_Selection];

//~ StartCoroutine(DeleteData());

//~ QJ_B_Sjx=true;
//~ }
//~ GUILayout.EndScrollView ();
//~ GUILayout.EndArea();

//~ GUILayout.EndArea();

//~ if(GUI.Button(new Rect(220,340,80,30), "写站内信"))
//~ {
//~ QJ_B_XZNX=true;
//~ QJ_B_Fjx=false;
//~ }
GUI.DragWindow (new Rect (0,0, 10000, 30));
}
void QJ_B_TZ_DoMyWindow(int windowID) //通知面板
{
GUI.DragWindow (new Rect (0,0, 10000, 30));
}
void QJ_B_XZNX_DoMyWindow(int windowID) //写站内信面板
{

GUI.Label (new Rect (20, 30, 100, 30), "收件人:");
GUI.Label (new Rect (20, 70, 100, 30), "主题:");
GUI.Label (new Rect (20, 110, 100, 220), "内容:");
stringToEdit_name =GUI.TextArea(new Rect (120, 30, 360, 30),stringToEdit_name, 30);
stringToEdit_title=GUI.TextArea(new Rect (120, 70, 360, 30),stringToEdit_title, 30);
stringToEdit_body =GUI.TextArea(new Rect (120, 110,360, 250),stringToEdit_body, 200);
if(GUI.Button(new Rect(120,365,80,30), "发送"))
{
StartCoroutine(SaveData()); //连接数据库
stringToEdit_name="";
stringToEdit_title="";
stringToEdit_body="";
}

GUI.DragWindow (new Rect (0,0, 10000, 30));
}

void QJ_B_HF_DoMyWindow(int windowID) //回复面板
{
GUI.Label (new Rect (20, 30, 100, 30), "收件人:");
GUI.Label (new Rect (20, 70, 100, 30), "主题:");
GUI.Label (new Rect (20, 110, 100, 220), "内容:");
GUI.Label(new Rect (120, 30, 360, 30),QJ_string_name);
GUI.Label(new Rect (120, 70, 360, 30),QJ_string_title);
GUI.Label(new Rect (120, 110,360, 150),QJ_string_body);
GUI.Label (new Rect (20, 270, 100, 30), "回复:");
QJ_string_G_body=GUI.TextArea(new Rect (120, 270,360, 90),QJ_string_G_body);

if(GUI.Button(new Rect(120,365,80,30), "发送"))
{
StartCoroutine(UP_Data());
QJ_string_G_body="";
}
GUI.DragWindow (new Rect (0,0, 10000, 30));
}

//———面板———>
//****************************
//————连接数据库——–<

//~ public GUISkin ChinSkin;
private string jsonURL="http://localhost/P/znx_cx.php"; //单条记录
private string jsonURL1="http://localhost/P/znx_insert.php"; //插入单条记录
private string jsonURL2="http://localhost/P/znx_cxq.php"; //查询多条记录
private string jsonURL3="http://localhost/P/znx_update.php"; //更新记录
private string jsonURL4="http://localhost/P/znx_delete.php"; //删除记录
public string abc="dataInfo";
public IEnumerator Test(){
WWW getwww = new WWW (jsonURL2);
yield return getwww;
}

//查询多条记录
public IEnumerator DoGetMultitermData() {
WWW getwww = new WWW (jsonURL2);
yield return getwww;
string myHash = getwww.data; //eval转换成一个object对象
JObject o = JObject.Parse(myHash);
JArray sizes = (JArray)o["user"];
string datainfo="";
List<string> JB_ids= new List<string>();
List<string> JB_names= new List<string>();
List<string> JB_titles= new List<string>();
List<string> JB_bodys= new List<string>();
List<string> JB_datetimes= new List<string>();
for(int i=0;i<sizes.Count;i++){
string id = (string)sizes[i]["id"];
string name = (string)sizes[i]["name"];
string title= (string)sizes[i]["title"];
string body= (string)sizes[i]["body"];
string datetime= (string)sizes[i]["datetime"];
//~ datainfo+=i+":"+name+":"+text+"\n";
JB_ids.Add((string)id);
JB_names.Add((string)name);
JB_titles.Add((string)title);
JB_bodys.Add((string)body);
JB_datetimes.Add((string)datetime);
}
SQL_id=JB_ids.ToArray();
SQL_name=JB_names.ToArray();
SQL_title=JB_titles.ToArray();
SQL_body=JB_bodys.ToArray();
SQL_datetime=JB_datetimes.ToArray();
//~ abc = datainfo;
}

//查询单条记录
public IEnumerator DoGetSingleData(){
WWW getwww = new WWW (jsonURL);
yield return getwww;
string myHash = getwww.data;
print(myHash);
JObject o = JObject.Parse(myHash);
abc ="username:"+ o["name"] +"\n";
}
//插入数据
public IEnumerator SaveData(){
//提交JSON数据的方法
Hashtable mydata=new Hashtable();
mydata.Add("name",stringToEdit_name);
mydata.Add("title",stringToEdit_title);
mydata.Add("body",stringToEdit_body);

//将数据转换为json字符串
string jsonstring=HashTable2Json(mydata);
//~ print("~~~~~~~~~~~~~~"+jsonstring);
WWWForm form = new WWWForm();
form.AddField("jsonstring", jsonstring);
WWW postwww = new WWW(jsonURL1, form);
yield return postwww;
if(postwww.data=="data save success"){
abc ="data save success";
//~ print("~~~~~~~~~"+abc);
}else{
abc ="data save fail";
//~ print("~~~~~~~~~"+abc);
}
}
//更新数据
public IEnumerator UP_Data(){
//提交JSON数据的方法
Hashtable mydata=new Hashtable();

mydata.Add("id",QJ_string_id);
mydata.Add("name",QJ_string_name);
mydata.Add("title",QJ_string_title);
mydata.Add("body",QJ_string_body+":"+QJ_string_G_body);

//将数据转换为json字符串
string jsonstring=HashTable2Json(mydata);
//~ print("!!!!!"+jsonstring);
WWWForm form = new WWWForm();
form.AddField("jsonstring", jsonstring);
WWW postwww = new WWW(jsonURL3, form);
yield return postwww;
print(":::"+postwww.data);
if(postwww.data=="success"){
abc ="success";
print("~~~~~~~~~"+abc);
}else{
abc ="data save fail";
print("~~~~~~~~~"+abc);
}
}

// 删除数据
public IEnumerator DeleteData(){
Hashtable mydata=new Hashtable();
mydata.Add("id",QJ_string_id);

//将数据转换为json字符串
string jsonstring=HashTable2Json(mydata);
print("~~~~~~~~~~~~~~"+jsonstring);
WWWForm form = new WWWForm();
form.AddField("jsonstring", jsonstring);
WWW postwww = new WWW(jsonURL4, form);
yield return postwww;
if(postwww.data=="data save success"){
//~ abc ="data save success";
print("~~~~~~~~~"+abc);
}else{
//~ abc ="data save fail";
print("~~~~~~~~~"+abc);
}
}

/**
* 单条记录转换JSON数据格式
*/
public static string HashTable2Json(Hashtable hashtable){
if (hashtable.Count < 1 ){
return "{}";
}
//初始化
StringBuilder sb = new StringBuilder();
sb.Append('{');
foreach (System.Collections.DictionaryEntry objDE in hashtable)
{
if (sb.Length > 1 )
sb.Append(",");
sb.Append("\"" + objDE.Key.ToString() + "\":" + "\""+objDE.Value.ToString()+ "\"");
}

sb.Append('}');
//~ print("!!!!!!!!!!!!!!!!!"+ sb.ToString());
return sb.ToString();
}
}

本条目发布于2010年4月28日。属于Unity脚本分类。