Unity中List常用的增删改查和对比操作

一、对比:

 #region 对比
    //对比相等(顺序也要相同)
    private void Test1()
    {
        List<int> listA1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        List<int> listA2 = new List<int> { 1, 2, 3, 4, 5, 6 };

        //SequenceEqual方法需引用using System.Linq;
        if (listA1.SequenceEqual(listA2))
        {
            Debug.Log("A1==A2");
        }
        else
        {
            Debug.Log("A1!=A2");
        }

        List<int> listB1 = new List<int> { 1, 2, 2, 4, 5, 6 };
        List<int> listB2 = new List<int> { 1, 2, 4, 4, 5, 6 };
        if (listB1.SequenceEqual(listB2))
        {
            Debug.Log("B1==B2");
        }
        else
        {
            Debug.Log("B1!=B2");
        }

        List<int> listC1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        List<int> listC2 = new List<int> { 1, 3, 2, 5, 4, 6 };
        if (listB1.SequenceEqual(listB2))
        {
            Debug.Log("C1==C2");
        }
        else
        {
            Debug.Log("C1!=C2");
        }
    }

    //对比元素相同(可乱序)
    private void Test2()
    {
        List<int> listA1 = new List<int> { 1, 1, 2, 2, 3, 3 };
        List<int> listA2 = new List<int> { 2, 2, 1, 1, 3, 3 };
        if (IsEqual(listA1, listA2))
        {
            Debug.Log("listA1和listA2所含元素相同");
        }
        else
        {
            D
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用MySQL进行增删改查,需要在Unity使用MySQL Connector/NET驱动程序。下面是一个简单的Unity C#代码示例,说明如何连接到MySQL数据库并执行增删改查操作: 首先,需要在Unity安装MySQL Connector/NET驱动程序。可以从MySQL官方网站上下载。 然后,需要在C#项目添加对MySQL Connector/NET的引用。可以在Visual Studio右键单击项目,选择“添加引用”,然后选择MySQL Connector/NET。 接下来,需要在C#代码使用MySQL Connector/NET命名空间。可以使用以下代码: using MySql.Data.MySqlClient; 然后,需要创建一个MySQL连接对象,并将其连接到数据库。可以使用以下代码: string connectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); 在这个代码,需要将“myServerAddress”替换为MySQL服务器的地址,“myDataBase”替换为要连接的数据库名称,“myUsername”替换为MySQL用户名,“myPassword”替换为MySQL密码。 接下来,可以使用MySQL命令对象执行SQL查询。可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM myTable"; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Debug.Log(reader["columnName"].ToString()); } 在这个代码,需要将“myTable”替换为要查询的表名称,“columnName”替换为要检索的列名称。 要执行其他操作,如插入、更新或删除,可以使用类似的方法。 例如,要插入一条记录,可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO myTable (columnName1, columnName2) VALUES ('value1', 'value2')"; command.ExecuteNonQuery(); 在这个代码,需要将“myTable”替换为要插入记录的表名称,“columnName1”和“columnName2”替换为要插入的列名称,“value1”和“value2”替换为要插入的值。 完成所有操作后,需要关闭MySQL连接。可以使用以下代码: connection.Close(); 这是一个简单的Unity使用MySQL进行增删改查的示例代码。注意,这只是一个起点,需要进一步学习MySQL Connector/NET和SQL查询语言才能深入了解如何使用MySQL进行增删改查
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值