2018年4月14日 星期六

struct List 範例


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct data_data
{
    public data_data(string name, string nickname)
    {
        this.Name = name;
        this.NickName = nickname;
    }

    public string Name { get; private set; }
    public string NickName { get; private set; }
}

public class struct_example : MonoBehaviour {

    List list = new List();

 // Use this for initialization
 void Start () {

        list.Add(new data_data("小名", "名仔"));
        list.Add(new data_data("蠟筆小新", "小新"));

        //--------- print
        for (int x = 0; x < list.Count; x++)
            Debug.Log(list[x].Name +"," + list[x].NickName);
    }
}

2018年4月11日 星期三

斷詞系統之長詞優先法與詞位標籤_測試版

斷詞系統長詞優先法分為: 正向及反向,會產生岐異與未知詞上的問題,若是詞庫夠大就能解決了。 這裡只範例長詞優先法之程式。

已解決了正反向優先詞重覆字問題  2018/4/09 15:00
已添加詞位標籤  2018/4/09 15:00

語料庫下載_Mysql

------------SQL------------

using System.Collections.Generic;
using System.Data;
using System;
using MySql.Data.MySqlClient;
using System.Text;

namespace SQL
{
    public class SQL
    {
        public static MySqlConnection mySqlConnection;

        // DataBase Ip;
        private static string HOST = "SQL IP";
        public static string HOST_TYPE
        {
            set { HOST = value; }
            get { return HOST; }
        }

        // DataBase user Id
        private static string ID = "SQL USER ID";
        public static string ID_TYPE
        {
            set { ID = value; }
            get { return ID; }
        }

        // DataBase password
        private static string PASSWORD = "SQL PASSWORD";
        public static string PASSWORD_TYPE
        {
            set { PASSWORD = value; }
            get { return PASSWORD; }
        }

        // DataBase Name
        private static string DATABASE = "DATABASE_NAME";
        public static string DATABASE_TYPE
        {
            set { DATABASE = value; }
            get { return DATABASE; }
        }

        private static string PORT = "SQL PORT";
        public static string PORT_TYPE
        {
            set { PORT = value; }
            get { return PORT; }
        }

        // 連線結果
        public static string RESULT = "";

        // 訊息
        public static List string  MESSAGE = new List string ();

        // Connect to SQL;
        public static void openSqlConnection(string connction)
        {
            try
            {
                mySqlConnection = new MySqlConnection(connction);
                mySqlConnection.Open();
                RESULT = mySqlConnection.ServerVersion;
            }
            catch
            {
                RESULT = "無法連接資料庫";
            }
        }

        // Close SQL
        public static void closeSqlConnection()
        {
            mySqlConnection.Close();
            mySqlConnection.Dispose();
            mySqlConnection = null;
        }

        // SQL Query
        public static void DOQUERY(string sqlQuery)
        {
            IDbCommand dbcommand = mySqlConnection.CreateCommand();
            dbcommand.CommandText = sqlQuery;
            IDataReader reader = dbcommand.ExecuteReader();
            reader.Close();
            reader = null;
            dbcommand.Dispose();
            dbcommand = null;
        }

        #region Get DataSet
        public static DataSet GetDataSet(string sqlString)
        {
            DataSet ds = new DataSet();

            try
            {
                MySqlDataAdapter da = new MySqlDataAdapter(sqlString, mySqlConnection);
            }
            catch (Exception e)
            {
                throw new Exception("SQL:" + sqlString + "\n");
            }
            return ds;
        }
        #endregion

        // string to utf8
        public static string STRING_UTF8(string messae)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            byte[] bytes = Encoding.UTF8.GetBytes(messae);
            string utf8ReturnString = encoder.GetString(bytes);

            return utf8ReturnString;
        }

        // 詞位標籤
        public static string INQUIRE_Lexical(string dataBaseTitle, string message)
        {
            string sqlText = "select * from " + dataBaseTitle + " where Message='" + message + "'";
            string str1 = "";

            MySqlCommand cmd = new MySqlCommand(sqlText, mySqlConnection);
            MySqlDataReader data = cmd.ExecuteReader();

            while (data.Read())
            {
                try
                {
                    str1 = data[5].ToString();
                }
                finally
                {

                }
            }

            data.Close();

            return str1;
        }


        // 資料庫查詢_Message(詞)
        // ※ 斷詞用_撈出可能性
        public static void INQUIRE_TABLE(string dataBaseTitle, string message)
        {
            string sqlText = "select * from " + dataBaseTitle + " where Message='" + message + "'";

            MySqlCommand cmd = new MySqlCommand(sqlText, mySqlConnection);
            MySqlDataReader data = cmd.ExecuteReader();

            while (data.Read())
            {
                try
                {
                    MESSAGE.Add(data[0].ToString());
                }
                finally
                {

                }
            }

            data.Close();
        }

        // 清除
        public static void MYSQL_MESSAGE_CLERA()
        {
            MESSAGE.Clear();
        }
    }
}


------------斷字處理功能------------

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class SegmentationSymbol
{
    // 斷詞標記括號
    private string[] specialSymbol = new string[] { "(", ")" };

    // 斷字狀態
    private string statusBroken;

    // 刪除符號字元 狀態: 1
    public string delectSymbol(string userMessage)
    {
        string strs = userMessage;

        for (int x = 0; x &lt; specialSymbol.Length; x++)
            strs = strs.Replace(specialSymbol[x], " ");

        return strs;
    }

    // 斷字字數
    private void status_conter(int status)
    {
        statusBroken = "";

        for (int x = 0; x &lt;= status; x++)
            statusBroken += @"\S";
    }

  
    // 斷字狀態
    public List<match> brokenSatus(string message, int status)
    {
        status_conter(status);

        List<match> match = new List<match>();
        MatchCollection splitResult = Regex.Matches(message, statusBroken, RegexOptions.IgnoreCase);

        foreach (Match test in splitResult)
            match.Add(test);

        return match;
    }

    // 刪除訊息
    public string delectWord(string message, int status)
    {
        string str = message;

        return str.Remove(status).ToString();
    }

   
    //  組合處理
    public List<string> delect_Word_Combination(string message, int status)
    {
        status_conter(status);

        string str = message;
        List<string> match = new List<string>();
        List<string> stringList = new List<string>();

        MatchCollection splitResult = Regex.Matches(str, statusBroken, RegexOptions.IgnoreCase);
        foreach (Match test in splitResult)
            match.Add(test.ToString());
        foreach (string strs in match)
        {
            stringList.Add(str.Replace(strs, ""));

            return stringList;
        }

        return stringList;
    }

    
    //  處理斷一次訊息
    public List<string> delect_oneWord(string message, int status)
    {
        status_conter(status);

        string str = message;
        List<string> match = new List<string>();
        List<string> stringList = new List<string>();

        MatchCollection splitResult = Regex.Matches(str, statusBroken, RegexOptions.IgnoreCase);
        foreach (Match test in splitResult)
            match.Add(test.ToString());
        foreach (string strs in match)
            stringList.Add(str.Replace(strs, ""));

        return stringList;
    }
}


------------斷詞程式------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
using System;

public class LongWordsFirst : MonoBehaviour
{

    public string str = "近年來知名夜市", logeWordForward, logeWordReverse, combination_string;

    private SegmentationSymbol segmentationSymbol = new SegmentationSymbol();
    private List<string> listMessage = new List<string>();
    private int messageNumber, messageNumber2, forward_Num;

    // 正向長詞優先法
    private List<string> longeWordForward_Message = new List<string>();

    // 正向長詞優先校正
    private List<string> forward_exclufr = new List<string>();

    // 反向長詞優先法
    private List<string> longeWordReverse_Message = new List<string>();

    // 正向詞位標籤
    private List<string> forwardLexical = new List<string>();
    // 反向詞位標籤
    private List<string> reverseLexical = new List<string>();

    // 長詞優先與詞位標籤完成
    public string fulfilForward, fulfilReverse, fulfil_ForwardLexical, fulfil_ReverseLexical;

    // 斷字系統資料表,文法組合資料表
    public static string dataBasetitle = "glossary", dataBasetitle2 = "grammar";

    void Start()
    {
        openDataBase();
    }

    // 打開資料庫
    void openDataBase()
    {
        string connectionString = string.Format("Server = {0}; Database = {1}; UserID = {2}; Password = {3}; Port = {4};", CMySql.HOST_TYPE, CMySql.DATABASE_TYPE, CMySql.ID_TYPE, CMySql.PASSWORD_TYPE, CMySql.PORT_TYPE);
        SQL.SQL.openSqlConnection(connectionString);
    }

    void OnGUI()
    {
        if (GUILayout.Button("測試"))
        {
            cutting_();
        }

        if (GUILayout.Button("詞位"))
        {
            // 導正順序
            StartCoroutine(order());
        }
    }

    void OnApplicationQuit()
    {
        SQL.SQL.closeSqlConnection();
    }

    // 切字
    private void cutting_()
    {
        listClear();

        // 正向優先輸入訊息
        logeWordForward = str;
        logeWordReverse = str;

        combination_string = str;

        messageNumber = logeWordReverse.Length;
        messageNumber2 = logeWordForward.Length;
        forward_Num = combination_string.Length;

        StartCoroutine(loneWordForward(logeWordForward.Length));
        StartCoroutine(longWordReverse(0));

        // 長詞詞位標籤
        StartCoroutine(forwardLable());
        StartCoroutine(reverseLable());

        // 長詞優先法校正
        StartCoroutine(ForwardAnalysis(0));
    }

    IEnumerator order()
    {
        while (true)
        {
            if (combination_string.Length == 0)
            {
                for (int x = forward_exclufr.Count - 1; x &gt;= 0; x--)
                    fulfilForward += forward_exclufr[x] + "|";

                for (int x = longeWordReverse_Message.Count - 1; x &gt;= 0; x--)
                    fulfilReverse += longeWordReverse_Message[x] + "|";

                for (int x = forwardLexical.Count - 1; x &gt;= 0; x--)
                    fulfil_ForwardLexical += forwardLexical[x] + "|";

                for (int x = reverseLexical.Count - 1; x &gt;= 0; x--)
                    fulfil_ReverseLexical += reverseLexical[x] + "|";

                break;
            }

            yield return new WaitForSeconds(0.5f);
        }
    }

    // 反向長詞詞位標籤
    IEnumerator reverseLable()
    {
        int x = 0;

        while (true)
        {
            try
            {
                if (x &lt; longeWordReverse_Message.Count)
                {
                    reverseLexical.Add(SQL.SQL.INQUIRE_Lexical(dataBasetitle, longeWordReverse_Message[x]));
                    x++;
                }
            }
            catch
            {
                break;
            }

            yield return new WaitForSeconds(0.5f);
        }
    }

    // 正向長詞詞位標籤
    IEnumerator forwardLable()
    {
        int x = 0;
        while (true)
        {
            try
            {

                if (x &lt; forward_exclufr.Count)
                {
                    forwardLexical.Add(SQL.SQL.INQUIRE_Lexical(dataBasetitle, forward_exclufr[x]));
                    x++;
                }

            }
            catch
            {
                break;
            }
            yield return new WaitForSeconds(0.5f);
        }
    }

    // 正向長詞優先法
    IEnumerator loneWordForward(int lenght)
    {
        while (true)
        {
            foreach (Match match1 in segmentationSymbol.brokenSatus(logeWordForward, logeWordForward.Length))
            {
                SQL.SQL.INQUIRE_TABLE(dataBasetitle, match1.ToString());

                foreach (string sqlMessage in SQL.SQL.MESSAGE)
                {
                    if (sqlMessage == match1.ToString())
                    {
                        longeWordForward_Message.Add(match1.ToString());

                        logeWordForward = segmentationSymbol.delectWord(logeWordForward, messageNumber2);
                    }

                }

                SQL.SQL.MYSQL_MESSAGE_CLERA();
            }



            foreach (Match match in segmentationSymbol.brokenSatus(logeWordForward, messageNumber2))
            {
                SQL.SQL.INQUIRE_TABLE(dataBasetitle, match.ToString());
                foreach (string str in SQL.SQL.MESSAGE)
                {
                    if (str == match.ToString())
                        longeWordForward_Message.Add(str);
                    logeWordForward = inspection(logeWordForward, longeWordForward_Message);
                    messageNumber2 = logeWordForward.Length;

                }

                SQL.SQL.MYSQL_MESSAGE_CLERA();
            }

            if (messageNumber2 &gt;= 0)
            {
                messageNumber2 -= 1;
            }

            yield return new WaitForSeconds(0.5f);
        }
    }

    // 後向長詞優先法
    IEnumerator longWordReverse(int lenght)
    {
        while (true)
        {
            foreach (Match match in segmentationSymbol.brokenSatus(logeWordReverse, logeWordReverse.Length))
            {
                SQL.SQL.INQUIRE_TABLE(dataBasetitle, match.ToString());
                foreach (string str in SQL.SQL.MESSAGE)
                {
                    if (str == match.ToString())
                    {
                        longeWordReverse_Message.Add(match.ToString());
                        logeWordReverse = segmentationSymbol.delectWord(logeWordReverse, match.Length);
                    }
                }
            }

            SQL.SQL.INQUIRE_TABLE(dataBasetitle, logeWordReverse);

            foreach (string str in segmentationSymbol.delect_Word_Combination(logeWordReverse, lenght))
            {
                SQL.SQL.INQUIRE_TABLE(dataBasetitle, str);

                foreach (string sqlMessage in SQL.SQL.MESSAGE)
                {
                    if (sqlMessage == str)
                    {
                        longeWordReverse_Message.Add(sqlMessage);
                        logeWordReverse = segmentationSymbol.delectWord(logeWordReverse, logeWordReverse.Length - sqlMessage.Length);
                        lenght = 0;
                    }
                }
            }

            lenght += 1;

            foreach (string str22 in SQL.SQL.MESSAGE)
            {
                if (str22 == logeWordReverse)
                {
                    longeWordReverse_Message.Add(logeWordReverse);
                    logeWordReverse = segmentationSymbol.delectWord(logeWordReverse, logeWordReverse.Length - str22.Length);
                }
            }


            if (lenght &gt;= combination_string.Length)
                lenght = 0;

            yield return new WaitForSeconds(0.5f);
        }
    }

    // 正向長詞優先法校正
    IEnumerator ForwardAnalysis(int lenght)
    {
        while (true)
        {

            foreach (string str in segmentationSymbol.delect_Word_Combination(combination_string, lenght))
            {
                Debug.Log(str);
                for (int x = 0; x &lt; longeWordForward_Message.Count; x++)
                {
                    if (str == longeWordForward_Message[x])
                    {
                        forward_exclufr.Add(str);
                        combination_string = segmentationSymbol.delectWord(combination_string, combination_string.Length - longeWordForward_Message[x].Length);
                        longeWordForward_Message.Remove(str);
                        lenght = 0;
                    }
                }
            }
            lenght += 1;

            for (int x = 0; x &lt; longeWordForward_Message.Count; x++)
            {
                if (combination_string == longeWordForward_Message[x])
                {
                    forward_exclufr.Add(combination_string);
                    combination_string = inspection(combination_string, forward_exclufr);
                    longeWordForward_Message.Remove(str);
                    lenght = 0;
                }
            }

            if (lenght &gt;= combination_string.Length)
                lenght = 0;

            yield return new WaitForSeconds(0.5f);
        }
    }

    // 檢查沒有數據
    private string inspection(string message, List<string> list)
    {
        string str = message;

        foreach (string str1 in list)
        {
            str = str.Replace(str1, "");
        }

        return str;
    }

    // list 清除
    private void listClear()
    {
        longeWordForward_Message.Clear();
        longeWordReverse_Message.Clear();
        forward_exclufr.Clear();
    }
}



------------結果圖------------



2018年4月5日 星期四

指定路徑搜尋所有子資料

這是示範專案使用的是Unity UI 功能當成介面,文字顯示部分則是用InputField之物件來做顯示,主要是為了方便使用。

UI_Canvas 物件需要:
  • InputField - 命名: Path
  • Scrollbar - 命名: Scrollbar_FileName
  • Button - 命名: Enter

InputField 物件與組件:
  • 子物件(Text) - 命名: FileName ※屬性 Text: 清空。
  • InputField: 關閉功能。
  • Scroll Rect: Content(放入Text子物件),Horizontal 勾勾拿掉,Vertical Scrolbar (放入Scrollbar 物件)。
  • Mask。

Scrollbar 物件與組件: 
  • scrolbar: Direction(更改)


Button  物件與組件:

  • 子物件(Text)-命名: btnMessage ※屬性Text: 輸入確定。
物件共同組件設定:

















程式碼(放到Canvas 物件) 設定:
















------------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;
using UnityEngine.UI;

public class Article : MonoBehaviour
{
    public GameObject pathBack, scrollbar_FilePatch, path_Message, enter;

    // 檔案位置
    private string patch = @"C:\Users\JohnLin\Desktop\測試\";
    public string path_Type
    {
        set { patch = value; }
        get { return patch; }
    }

    // 檔案位置List
    private List<string> file_patch = new List<string>();

    private string patch_result;

    public float pointX, pointY;

    // 讀取文字檔內容
    private string readtxtMessage;

    // 刪除重復編號
    private int delectNumber;

    void Start()
    {
        // 按鈕屬性
        button_(enter, 0.12f, 0.58f, 0.2f, 30);
    }
   
    void Update()
    {
        // 顯示路徑背景
        image_(pathBack, 0.42f, 1.32f, 0.8f, 1.2f);

        // 拉霸
        scrollbar_(scrollbar_FilePatch, 0.86f, 1.4f, 0, 0);
    }

    // Button 屬性
    Button button_(GameObject obj, float width, float height, float sizeX, float sizeY)
    {
        Button btn = obj.GetComponent<Button>();

        // posistion
        btn.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
        // imageSize
        btn.image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, sizeY);

        switch (btn.name)
        {
            case "Enter":
                btn.onClick.AddListener(() => btnOnClick_Enter());
                break;
        }

        return btn;
    }

    // 探聽(確定)
    public void btnOnClick_Enter()
    {
        clearList();
        StartCoroutine(readFire());
        patch_result = "";

        for (int x = 0; x < file_patch.Count; x++)
            patch_result += file_patch[x] + "\n";

        text_(path_Message).text = patch_result;
    }

    // Image 屬性
    Image image_(GameObject obj, float width, float height, float sizeX, float sizeY)
    {
        Image image = obj.GetComponent<Image>();

        // posistion
        image.rectTransform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);

        // imageSize
        image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);

        return image;
    }

    // 拉霸
    Scrollbar scrollbar_(GameObject obj, float width, float height, float sizeX, float sizeY)
    {
        Scrollbar scrollbar = obj.GetComponent<Scrollbar>();
        scrollbar.transform.position = new Vector2(Screen.width / 2 * width, Screen.height / 2 * height);
        //scrollbar.size = 12;

        return scrollbar;
    }

    // Text 屬性
    Text text_(GameObject obj)
    {
        Text text = obj.GetComponent<Text>();
        text.horizontalOverflow = HorizontalWrapMode.Wrap;
        text.verticalOverflow = VerticalWrapMode.Overflow;

        return text;
    }

    // 自動閱讀檔案
    IEnumerator readFire()
    {
        DirectoryInfo di = new DirectoryInfo(path_Type);

        foreach (FileInfo fileInfo in di.GetFiles("*.*", SearchOption.AllDirectories))
        {
            file_patch.Add(fileInfo.ToString());
        }
     
        yield return new WaitForSeconds(0.5f);
    }

    /// <summary>
    /// 讀取文建資料
    /// </summary>
    /// <returns></returns>
    private List<string> readFileTxtMessage(string str)
    {
        StreamReader streamReader = new StreamReader(str);

        List<string> readTxtMessageList = new List<string>();
        readTxtMessageList.Clear();

        while ((readtxtMessage = streamReader.ReadLine()) != null)
            readTxtMessageList.Add(readtxtMessage);

        streamReader.Close();
        return readTxtMessageList;
    }

    // 清除List
    void clearList()
    {
        file_patch.Clear();
    }
}

----------------------

結果圖:





2017年11月4日 星期六

UI 簡單的跑馬燈

看到電影有一個字一個字在UI顯示,自己做了一個簡單的程式來玩玩 :)
------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;

public class Layout_UI : MonoBehaviour {

    public bool marquee_start; // 跑馬燈開關
    private float marquee_startTime = 0;  // 跑馬燈時間
    public Text text;
 
 // Update is called once per frame
 void Update () {
         
         if (Input.GetKeyDown(KeyCode.W))
            sw = !sw;
       marquee_start = sw;
       
       text.text = system_message_text_Marquee("跑馬燈", 0.4f);
    }

    // 跑馬燈
    private string system_message_text_Marquee(string message, float speed)
    {
        return message.Substring(0, (int)timeData(message.Length, speed));
    }


    // 計數器(跑馬燈用)
    private float timeData(float textSize, float speed)
    {
        float time = (marquee_startTime < textSize && marquee_start.Equals(true)) ? marquee_startTime += speed :
            (marquee_start.Equals(false)) ? marquee_startTime = 0 : marquee_startTime = textSize;
        return time;
    }

}

2017年10月14日 星期六

2017年9月8日 星期五

C# 簡單分割字元及利用正規表達式找到訊息

正規表達式操考網站
正規表達式_測試網站

-----------------------
using System.Collections.Generic;
using System.Text.RegularExpressions;

/// 
/// 括號字元分割
/// 
public class SegmentationSymbol
{
    private string[] specialSymbol = new string[] { "(", ")" };
 
    /// 
    /// 刪除符號字元_括號
    /// 
    /// 
    /// 
    public string delectSymbol(string userMessage)
    {
        string strs = userMessage;

        for (int x = 0; x < specialSymbol.Length; x++)
            strs = strs.Replace(specialSymbol[x], " ");

        return strs;
    }

    /// 
    /// 利用正規表達式
    /// 
    /// 
    public List getA(string message)
    {
        string adjData = @"(\S+ A)";
        List match = new List();

        MatchCollection splitResult = Regex.Matches(message, adjData, RegexOptions.IgnoreCase);
        foreach (Match test in splitResult)
            match.Add(test);

        return match;
    }
}


-----------------
結果圖


2017年9月1日 星期五

Unity連接MySQL及寫入資料與查詢

請下載 Xmpp 軟體
如果要解決中文上的問題,安裝後請到: D:\xmpp\mysql\bin (本文章是安裝在D曹)
此路徑下找到my.ini檔案下進行更改。
更改內容:
[mysqld]
default-character-set=utf8

[client]
default-character-set=utf8
init_connect='SET NAMES utf8'

再開啟MySql:












開啟後建立自己的資料庫: 本文資料庫名稱 corpus
再建立兩欄資料表 指令:

create table 資料表名稱(
ID integer auto_increment primary key,
Message VARCHAR(20));

若是資料表調整錯誤可以進行網頁頁面更正:












標記框框(解碼中文)




















補充:
自動編號排列指令:
ALTER TABLE test AUTO_INCREMENT = 0

凍仁大大整理的筆記之分享(MySql 指令)~~

Unity(2018版本) 連接 MySQL 套件這裡提供兩個地方載點:
提取码:6rbi

載點2

----------------------------

Unity 操作(Script):

using UnityEngine;
using System;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
using System.Text;

public class DataBaseTest : MonoBehaviour
{
    private bool writeDataBaseTable = false;
    private string id = "ID(不必輸入)", message = "請輸入訊息";

    void OnGUI()
    {
        // DataBase 連線按鈕
        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height, 200, 30));
        if (GUILayout.Button("Open DataBase InterNet"))
            openDataBase();
        GUILayout.EndArea();

        // DataBase 連線訊息
        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height * 30, 200, 30));
        GUILayout.Label(CMySql.result);
        GUILayout.EndArea();

        // 連接DataBase後事件
        if (CMySql.result != "")
        {
            GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height * 60, 100, 30));
            if(GUILayout.Button("寫入資料")){
                writeDataBaseTable = true;
            }
            GUILayout.EndArea();
        }

        // 寫入資料庫 UI
        if (writeDataBaseTable)
            writeDataUI(); 
    }

    /// 
    /// 寫入資料
    /// 
    void writeDataUI()
    {
        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height * 90, 100, 30));
        id = GUILayout.TextField(id, 200);
        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(Screen.width / Screen.width * 100, Screen.height / Screen.height * 90, 100, 30));
        message = GUILayout.TextField(message, 200);
        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height * 120, 100, 30));
        if (GUILayout.Button("寫入"))
            CMySql.WriteTable( "test" ,id, message);
        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(Screen.width / Screen.width * 100, Screen.height / Screen.height * 120, 100, 30));
        if (GUILayout.Button("取消"))
            writeDataBaseTable = false;
        GUILayout.EndArea();

    }
    

    /// 
    /// 關閉應用程式
    /// 
    void OnApplicationQuit()
    {
        CMySql.closeSqlConnection();
    }

    /// 
    /// openDataBase
    /// 
    void openDataBase()
    {
        string connectionString = string.Format("Server = {0}; Database = {1}; UserID = {2}; Password = {3};", CMySql.hostType, CMySql.dataBaseType, CMySql.idType, CMySql.passWordTpye);
        CMySql.openSqlConnection(connectionString);
        CMySql.myObjType = CMySql.GetDataSet(connectionString);
        Debug.Log(CMySql.result);
    }
}


資料庫 Script (C#):

/**
 * 2017/8/30 8:10
 * 連接Mysql
 * 寫入資料(中文解決), 讀取資料
 * MySql.Data.MySqlClient download path: https://dev.mysql.com/downloads/connector/net/1.0.html
 * MySql.Data.MySqlClient download path: http://pan.baidu.com/s/1gePtERT 
 */

using System.Collections.Generic;
using System.Data;
using System;
using MySql.Data.MySqlClient;
using System.Text;
using UnityEngine;

public class CMySql
{

    // Just like MyConn.conn in Story Tolls before
    public static MySqlConnection dbConnection;

    // DataBase Ip
    private static string host = "127.0.0.1";
    public static string hostType
    {
        set { host = value; }
        get { return host; }
    }

    // DataBase user Id
    private static string id = "root";
    public static string idType
    {
        set { id = value; }
        get { return id; }
    }

    // DataBase Password
    private static string passWord = "";
    public static string passWordTpye
    {
        set { passWord = value; }
        get { return passWord; }
    }

    // DataBase Name
    private static string dataBase = "corpus";
    public static string dataBaseType
    {
        set { dataBase = value; }
        get { return dataBase; }
    }

    // 連線結果
    public static string result = "";

    // 讀取資 (欄位2)
    static List MessageField2 = new List();

    // DataSet
    private static DataSet myObj;
    public static DataSet myObjType
    {
        set { myObj = value; }
        get { return myObj; }
    }

    // Connect to database
    public static void openSqlConnection(string connectionString)
    {
        dbConnection = new MySqlConnection(connectionString);
        dbConnection.Open();
        result = dbConnection.ServerVersion;
    }

    // closeSql
    public static void closeSqlConnection()
    {
        dbConnection.Close();
        dbConnection = null;
    }

    // MySQL Query
    public static void doQuery(string sqlQuery)
    {
        IDbCommand dbCommand = dbConnection.CreateCommand();
        dbCommand.CommandText = sqlQuery;
        IDataReader reader = dbCommand.ExecuteReader();
        reader.Close();
        reader = null;
        dbCommand.Dispose();
        dbCommand = null;
    }

    #region Get DataSet
    public static DataSet GetDataSet(string sqlString)
    {
        DataSet ds = new DataSet();

        try
        {
            MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);
        }
        catch (Exception e)
        {
            throw new Exception("SQL:" + sqlString + "\n");
            e.Message.ToString();
        }
        return ds;
    }
    #endregion

    // 轉utf8
    public static string messageToUtf8(string message)
    {
        UTF8Encoding encoder = new UTF8Encoding();
        byte[] bytes = Encoding.UTF8.GetBytes(message);
        string utf8ReturnString = encoder.GetString(bytes);

        return utf8ReturnString;
    }

    static string ID;
    static string Message;

    // 寫入資料表
    public static void WriteTable(string DataBaseTable ,string id, string message)
    {
        MySqlCommand command = dbConnection.CreateCommand();
       
        string sqlText = "select count(1) from " + DataBaseTable + " where Message=('" + message + "')";

        MySqlCommand cmd1 = new MySqlCommand(sqlText, dbConnection);
        int count = (int)(long)cmd1.ExecuteScalar();
        
        if (count > 0)
        {
            Debug.Log("已經有資料囉");
        }
        else
        {
            ID = id;
            Message = messageToUtf8(message);

            command.CommandText = "Insert into " + DataBaseTable + "(ID, Message) value('" + ID + "','" + Message + "')";
            command.ExecuteNonQuery();

            Debug.Log("載入資料成功");
        }
    }

    /// 
    /// 資料庫查詢
    /// 
    /// 
    /// 
    public static void INQUIRE_TABLE(string dataBaseTitle, string message)
    {
        string sqlText = "select * from " + dataBaseTitle + " where Message='" + message + "'";

        MySqlCommand cmd = new MySqlCommand(sqlText, mySqlConnection);
        MySqlDataReader data = cmd.ExecuteReader();

        while (data.Read())
        {
            try
            {
                UnityEngine.Debug.Log((data[0] + " -- " + data[1]));
            }catch(Exception e)
            {
                data.Close();
                closeSqlConnection();
            }
        }

        data.Close();
    }
}


結果圖:
※寫入資料後,記得Mysql 要重新讀取一次才會出現。










第二筆資輸入:


























若是資料重復: