unity Linq测试

左键按下给TEXT列表随机生成数字 抬起左键该TEXT 排序并且 给所在层级2以上的TEXT 更改颜色
unity Linq测试_第1张图片

using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class No06_Do : MonoBehaviour
{
public Text[] MyTexts;

void Start()
{
    MyTexts = transform.GetComponentsInChildren();
}

void Update()
{ 
    if (Input.GetMouseButtonDown(0))
    {
        MyTexts.ToList().ForEach(x => x.text = Random.Range(0, 20).ToString());
        MyTexts.ToList().ForEach(x => x.color = Color.black);
    }

    if (Input.GetMouseButtonUp(0))
    {
        MyTexts.ToList().OrderBy(myd => int.Parse(myd.text)).ToList().ForEach(x => x.transform.SetAsLastSibling());
        MyTexts.ToList().Where(item => item.transform.GetSiblingIndex() > 2 && item.transform.GetSiblingIndex() < MyTexts.Length).ToList().ForEach(x => x.color = Color.yellow);

		//var numQuery = from num in MyTexts
        //               where (num.transform.GetSiblingIndex() >2)
        //               select num as Text;

        //numQuery.ToList().ForEach(x=> Debug.Log(x.text));
    }
}

}

你可能感兴趣的:(UNITY)