示例#1
0
    void OnGUI()
    {
        Rect rect = new Rect(0, 0, 100, 20);

        // Create database first
        if (!db.Exists)
        {
            if (GUI.Button(new Rect(Screen.width / 2 - 125, Screen.height / 2 - 40, 250, 80), "Create DB"))
            {
                CreateDB();
            }
            return;
        }

        // after creating database and a table in DB
        if (isTableCreated)
        {
            GUI.Label(rect, "ID");
            rect.x = 150;
            GUI.Label(rect, "Name");

            rect.y = 30;
            rect.x = 0;
            if (btnName_SaveUpdate == "Save")
            {
                _id = GUI.TextField(rect, _id);
            }
            else
            {
                GUI.TextField(rect, _id);
            }

            rect.x = 150;
            _name  = GUI.TextField(rect, _name);

            rect.x = rect.x + rect.width + 10;
            if (GUI.Button(rect, btnName_SaveUpdate))
            {
                if (btnName_SaveUpdate == "Save")
                {
                    AddRow(_id, _name);
                }
                else if (btnName_SaveUpdate == "Update")
                {
                    UpdateRow(_id, _name);
                }
            }
        }


        // Display all data from database
        rect.x      = 0;
        rect.y      = 60;
        rect.width  = Screen.width;
        rect.height = Screen.height - 120;

        scrollPos   = GUI.BeginScrollView(rect, scrollPos, new Rect(0, 0, rect.width - 30, allIDs.Count * 30));
        rect.width  = 125;
        rect.height = 25;
        for (int i = 0; i < allIDs.Count; i++)
        {
            // id label
            rect.x = 0;
            rect.y = i * 25;
            GUI.Label(rect, allIDs[i]);
            // name
            rect.x += 130;
            GUI.Label(rect, allNames[i]);
            // update button
            rect.x += 130;
            if (GUI.Button(rect, "Edit"))
            {
                _id   = allIDs[i];
                _name = allNames[i];
                btnName_SaveUpdate = "Update";
            }
            // remove button
            rect.x += 130;
            if (GUI.Button(rect, "Delete"))
            {
                DeleteRow(allIDs[i]);
            }
        }
        GUI.EndScrollView();

        // clear table
        rect.y      = Screen.height - 25;
        rect.x      = 0;
        rect.width  = 100;
        rect.height = 20;
        GUI.enabled = isTableCreated;
        if (GUI.Button(rect, "Clear Table"))
        {
            db.ClearTable("Users");

            Refresh();
        }
        GUI.enabled = true;

        // delete table
        rect.x += 110;
        if (GUI.Button(rect, btnName_CreateDeleteTable))
        {
            if (btnName_CreateDeleteTable == "Delete Table" && db.DeleteTable("Users"))
            {
                allIDs.Clear();
                allNames.Clear();
                btnName_CreateDeleteTable = "Create Table";
                isTableCreated            = false;
            }
            else if (btnName_CreateDeleteTable == "Create Table")
            {
                if (CreateTable())
                {
                    btnName_CreateDeleteTable = "Delete Table";
                    isTableCreated            = true;
                }
            }
        }

        // delete table
        rect.x += 110;
        if (GUI.Button(rect, "Delete DB"))
        {
            if (db.DeleteDatabase())
            {
                allIDs.Clear();
                allNames.Clear();
                btnName_CreateDeleteTable = "Create Table";
                isTableCreated            = false;
            }
        }
    }
 public void Truncate()
 {
     _db.ClearTable(_tableIdentifier);
 }