课程地址 : http://www.sikiedu.com/my/course/293
凉鞋大大的,可以的话大家多支持一波~
一.SelectMany
功能 :将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为⼀个序列。对每项再进⾏遍历处理再进⾏合成序列。(这可能不太理解,看完示例就明白了)
1.linq
public class LINQSelectManyExample : MonoBehaviour
{
class Student
{
public string Name;
public int Age;
}
private void Start()
{
var students = new List<Student>()
{
new Student() {Name = "张三", Age = 50},
new Student() {Name = "张三", Age = 45},
new Student() {Name = "李四", Age = 50}
};
var singleChars = students.SelectMany(student => student.Name + ":" +
student.Age);
foreach (var singleChar in singleChars)
{
Debug.Log(singleChar);
}
}
}
}
运行效果 :
2.UniRx
public class SelectManyExample : MonoBehaviour
{
void Start()
{
var urls = new List<string> { "http://sikiedu.com", "http://qframework.com" };
urls.SelectMany(url => "[" + url + "]")
.ToList()
.ForEach(singleChar =>
{
Debug.Log(singleChar);
});
}
}
运行效果:
⼀般在 UniRx 中,主要是完成 Coroutine 的顺序执⾏功能。
public class CoroutineSequenceExample : MonoBehaviour
{
IEnumerator A()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("A");
}
IEnumerator B()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("B");
}
IEnumerator C()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("C");
}
// Use this for initialization
void Start()
{
var aStream = Observable.FromCoroutine(A);
var bStream = Observable.FromCoroutine(B);
var cStream = Observable.FromCoroutine(C);
aStream.SelectMany(bStream.SelectMany(cStream))
.Subscribe();
}
}
二.Take
功能 : 从序列的开头返回指定数量的相邻元素。
1.linq
public class LINQTakeExample : MonoBehaviour
{
void Start()
{
var urls = new string[] {
"http://sikiedu.com",
"http://qframeowork.io",
"http://weibo.com",
"http://github.com",
};
urls.Take(3)
.ToList()
.ForEach(url =>
{
Debug.Log(url);
});
}
}
2.UniRx
public class UniRxTakeExample : MonoBehaviour
{
void Start()
{
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Take(2)
.Subscribe(_ => { Debug.Log("mouse clicked!"); });
}
}
值由前两次鼠标点击才会输出
三.Concat
功能 : 连接两个序列。
1.Linq
public class LINQConcatExample : MonoBehaviour
{
void Start()
{
var aClassAges = new[] { 3, 4, 5, 6 };
var bClassAges = new[] { 1, 3, 5, 7 };
aClassAges.Concat(bClassAges)
.ToList()
.ForEach(age =>
{
Debug.Log(age);
});
}
}
2.UniRx
public class UniRxConcatExample : MonoBehaviour
{
void Start()
{
var leftMouseClickStream = Observable.EveryUpdate().Where(_ => Input.GetMouseButtonDown(0)).Take(3).Select(_ => "A");
var rightMouseClickStream = Observable.EveryUpdate().Where(_ => Input.GetMouseButtonDown(1)).Take(2).Select(_ => "B");
leftMouseClickStream.Concat(rightMouseClickStream)
.Subscribe(clickEvent =>
{
Debug.Log(clickEvent);
});
}
}
按三次左键再按两次右键,输出A A A B B(否则不输出)
四.WhenAll
功能 :确定序列中的所有元素是否都满⾜条件(Linq),按照条件执行完后执行注册事件(UniRx)。
1.linq(All操作符)
public class LINQWhenAllExample : MonoBehaviour
{
void Start()
{
var ages = new int[] { 18, 19, 20, 10, 50, 100 };
Debug.Log(ages.All(age => age > 10));
}
}
2.UniRx
public class UniRxWhenAllExample : MonoBehaviour
{
IEnumerator A()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("A");
}
IEnumerator B()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("B");
}
void Start()
{
var leftClickStream = Observable.EveryUpdate().Where(_ => Input.GetMouseButtonDown(0)).Take(3).Select(_ =>
{
Debug.Log("left mouse clicked");
return Unit.Default;
});
var rightClickStream = Observable.EveryUpdate().Where(_ => Input.GetMouseButtonDown(1)).Take(4).Select(_ =>
{
Debug.Log("right mouse clicked");
return Unit.Default;
});
Observable.WhenAll(Observable.FromCoroutine(A),
Observable.FromCoroutine(B),
leftClickStream,
rightClickStream)
.Subscribe(_ =>
{
Debug.Log("all coroutine completed");
});
}
}
运行结果 :
这里顺序不一定如此