List简介: 在 Unity 中,List(T是一种数据类型,比如int、string、GameObject等)是一个非常有用的集合类型,它位于System.Collections.Generic命名空间下。它可以动态地调整大小,能够方便地添加、删除和访问元素。
博客将会介绍Listd的使用方法。希望这篇博客对Unity的开发者有所帮助。 大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。 欢迎点赞评论哦.下面就让我们进入正文吧 !
提示:以下是本篇文章正文内容,下面案例可供参考

示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>();
}
}语法:listName.Add(item); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>();
stringList.Add("Hello");
stringList.Add("World");
}
}语法:listName.Insert(index, item); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<float> floatList = new List<float>();
floatList.Add(1.0f);
floatList.Add(3.0f);
floatList.Insert(1, 2.0f);
}
}打印出来就是 1.0,2.0,3.0
可以通过索引来访问List中的元素,索引从 0 开始。 语法:T element = listName[index]; 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int secondElement = intList[1];
Debug.Log(secondElement);
}
}Count属性:返回List中元素的个数。 语法:int count = listName.Count; 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<GameObject> gameObjectList = new List<GameObject>();
gameObjectList.Add(new GameObject());
gameObjectList.Add(new GameObject());
int count = gameObjectList.Count;
Debug.Log(count);
}
}Remove(T item):从List中删除指定的元素(如果有多个相同元素,只删除第一个)。 语法:listName.Remove(item); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "apple"};
stringList.Remove("apple");
}
}RemoveAt(int index):从List中删除指定索引位置的元素。 语法:listName.RemoveAt(index); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
intList.RemoveAt(1);
}
}Clear()方法:移除List中的所有元素。 语法:listName.Clear(); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<Transform> transformList = new List<Transform>();
transformList.Add(transform);
transformList.Clear();
}
}IndexOf(T item):返回指定元素在List中的第一个索引,如果不存在则返回 - 1。 语法:int index = listName.IndexOf(item); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "apple"};
int index = stringList.IndexOf("apple");
int index1 = stringList.IndexOf("app");
Debug.Log (index+" "+index1);
}
}1.Contains(T item):检查List是否包含指定元素,返回true或false。 语法:bool contains = listName.Contains(item); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<float> floatList = new List<float>() {1.0f, 2.0f, 3.0f};
bool containsTwo = floatList.Contains(2.0f);
}
}Sort()方法:对List中的元素进行排序。对于简单数据类型(如int、float、string等),会按照默认的排序规则进行排序。 语法:listName.Sort(); 示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {3, 1, 2};
intList.Sort();
}
}打印出来是 1,2,3
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
//反转列表
intList.Reverse();
}
}打印出来是3,2,1
代码如下:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "cherry"};
for (int i = 0; i < stringList.Count; i++)
{
Debug.Log(stringList[i]);
}
}
}代码如下:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<GameObject> gameObjectList = new List<GameObject>();
gameObjectList.Add(new GameObject("Object1"));
gameObjectList.Add(new GameObject("Object2"));
foreach (GameObject obj in gameObjectList)
{
Debug.Log(obj.name);
}
}
}功能:将List中的元素转换为另一种类型的List。 语法:List newList = oldList.ConvertAll(conversionDelegate);,其中conversionDelegate是一个转换函数,用于定义如何将T类型转换为TOutput类型。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
List<string> stringList = intList.ConvertAll<string>(i => i.ToString());
}
}语法:bool exists = list.Exists(match);,其中match是一个Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
bool existsGreaterThanTwo = intList.Exists(i => i > 2);
}
}语法:T foundElement = list.Find(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int foundElement = intList.Find(i => i > 1);
}
}语法:List foundElements = list.FindAll(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3, 4, 5};
List<int> evenNumbers = intList.FindAll(i => i % 2 == 0);
}
}功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中第一个匹配元素的从零开始的索引。 语法:int index = list.FindIndex(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int index = intList.FindIndex(i => i > 1);
}
}功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中的最后一个匹配元素。 语法:T foundElement = list.FindLast(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int foundElement = intList.FindLast(i => i < 3);
}
}功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中最后一个匹配元素的从零开始的索引。 语法:int index = list.FindLastIndex(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int index = intList.FindLastIndex(i => i < 3);
}
}语法:list.ForEach(action);,其中action是一个Action类型的委托,用于定义要对每个元素执行的操作。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
intList.ForEach(i => Debug.Log(i));
}
}功能:创建一个包含源List中指定范围的元素的新List。 语法:List newList = list.GetRange(index, count);,其中index是起始索引,count是要包含的元素数量。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3, 4, 5};
List<int> subList = intList.GetRange(1, 3);
}
}语法:bool allMatch = list.TrueForAll(match);,其中match是Predicate类型的委托,用于定义匹配条件。 示例:
using System;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {2, 4, 6};
bool allEven = intList.TrueForAll(i => i % 2 == 0);
}
}本次总结的就是List的api详解, 有需要会继续增加功能 如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢 你的点赞就是对博主的支持,有问题记得留言评论哦! 不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒!