linq in unity
DESCRIPTION
LINQ in Unity LINQ to GameObject - https://github.com/neuecc/LINQ-to-GameObject-for-Unity/ UniRx - https://github.com/neuecc/UniRxTRANSCRIPT
![Page 1: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/1.jpg)
![Page 2: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/2.jpg)
@仕事
C#
@個人活動
http://neue.cc/
@neuecc
https://github.com/neuecc/UniRx
![Page 3: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/3.jpg)
神獄のヴァルハラゲート
モンスタハンターロアオブカード
http://gihyo.jp/dev/serial/01/grani/0001
http://grani.jp/recruit
![Page 4: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/4.jpg)
using
![Page 5: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/5.jpg)
WindowsWinForms, WPF
MacXamarin.Mac
Windows TabletWindows Store Application
Web ApplicationASP.NET MVC/Web API, OWIN
CloudMicrosoft Azure, AWS
C# Everywhere
GameUnity, PlayStation Mobile SDK
MobileXamarin.iOS
Xamarin.Android
Windows Phone 8 SDK
EmbeddedWindows Embedded
.NET Micro Framework
NUIKinect, LeapMotion
![Page 6: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/6.jpg)
WindowsWinForms, WPF
MacXamarin.Mac
Windows TabletWindows Store Application
Web ApplicationASP.NET MVC/WebAPI, OWIN
CloudMicrosoft Azure, AWS
C# Everywhere - Current
GameUnity, PlayStation Mobile SDK
MobileXamarin.iOS
Xamarin.Android
Windows Phone 8 SDK
EmbeddedWindows Embedded
.NET Micro Framework
NUIKinect, LeapMotion
![Page 7: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/7.jpg)
WindowsWinForms, WPF
MacXamarin.Mac
Windows TabletWindows Store Application
Web ApplicationASP.NET MVC/WebAPI, OWIN
CloudMicrosoft Azure, AWS
C# Everywhere - Future
GameUnity, PlayStation Mobile SDK
MobileXamarin.iOS
Xamarin.Android
Windows Phone 8 SDK
EmbeddedWindows Embedded
.NET Micro Framework
NUIKinect, LeapMotion
![Page 8: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/8.jpg)
サーバーもクライアントもC#
同一ソリューションファイルに格納
言語依存を強みに変える
サーバーサイド(C#/ASP.NET)とクライアントサイド(Unity)でデータ共有
![Page 9: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/9.jpg)
Introduction to LINQ
![Page 10: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/10.jpg)
Java/Delphi
Generics
LINQ
dynamic
async
2002 C# 1.0
2005 C# 2.02008 C# 3.0
2010 C# 4.02012 C# 5.0LINQ
anonymous method
yield return
![Page 11: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/11.jpg)
Java/Delphi
Generics
LINQ
dynamic
async
2002 C# 1.0
2005 C# 2.02008 C# 3.0
2010 C# 4.02012 C# 5.0LINQ
anonymous method
yield returnC# 4.0以降Unity非対応の壁(本当にはよ対応して欲しい)
![Page 12: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/12.jpg)
LINQ...
![Page 13: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/13.jpg)
SQLみたいなもの?
![Page 14: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/14.jpg)
似てる、半分正しい。
var query = from x in source
where x % 2 == 0
select x * x;
![Page 15: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/15.jpg)
Language INtegrated Query
Unityでの実用上の場合は
![Page 16: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/16.jpg)
// クエリ構文
var query = from x in source
where x % 2 == 0
select x * x;
// メソッド構文
var query = source
.Where(x => x % 2 == 0)
.Select(x => x * x);
![Page 17: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/17.jpg)
// クエリ構文
var query = from x in source
where x % 2 == 0
select x * x;
// メソッド構文
var query = source
.Where(x => x % 2 == 0)
.Select(x => x * x);
メソッド構文のほうが機能が多い(使えるメソッド数が多い)&ラムダ式に慣れれば書きやすいので、基本的にはメソッド構文で書くのをお薦めします
(IntelliSenseでメソッドチェーン楽しい!)
![Page 18: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/18.jpg)
// 列挙してフィルタリングして詰め直すvar list = new List<int>();
foreach (var item in source){
if (item % 2 == 0){
list.Add(item);}
}
// 列挙してフィルタリングして詰め直すvar array = source.Where(x => x % 2 == 0).ToArray();
(source = IEnumerable<int>)・LINQは一行で済む・一時変数(List<T>)の宣言が不要・配列がカジュアルに使える
![Page 19: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/19.jpg)
フィルタして変形して詰め直しは頻出パターン(pythonでのリスト内包表記とかに相当)
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加えるvar list = new List<DateTime>();
foreach (var item in source){
if (item % 2 == 0){
list.Add(DateTime.Now.AddDays(item));}
}
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加えるvar array = source
.Where(x => x % 2 == 0)
.Select(x => DateTime.Now.AddDays(x))
.ToArray();
![Page 20: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/20.jpg)
Takeの他にSkipとか条件つきのTakeWhile/SkipWhileなどもあるよ
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加える// を、4個分だけ取得するvar list = new List<DateTime>();
foreach (var item in source){
if (item % 2 == 0){
list.Add(DateTime.Now.AddDays(item));}
if (list.Count == 4) break;}
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加える// を、4個分だけ取得するvar array = source
.Where(x => x % 2 == 0)
.Select(x => DateTime.Now.AddDays(x))
.Take(4)
.ToArray();
![Page 21: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/21.jpg)
値がかぶった時に別の値で比較する、というのもThenBy/ThenByDescendingでつなげるだけで
出来るのが普通のSortよりも強い
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加える// を、4個分だけ取得する// を、降順にソートするvar list = new List<DateTime>();
foreach (var item in source){
if (item % 2 == 0){
list.Add(DateTime.Now.AddDays(item));}
if (list.Count == 4) break;}list.Sort((x, y) => -x.CompareTo(y));
// 列挙してフィルタリングして詰め直す// + 日付を足したのを加える// を、4個分だけ取得する// を、降順にソートするvar array = source
.Where(x => x % 2 == 0)
.Select(x => DateTime.Now.AddDays(x))
.Take(4)
.OrderByDescending(x => x)
.ToArray();
![Page 22: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/22.jpg)
foreachでいい?
機能が弱ければ弱いほどコードが意図を伝える
単機能だからこそ、見ただけで何をやっているのか分かる。それに加えて短く書けるが故に書きやすいし、そのこと自
体が読みやすさにも寄与する
![Page 23: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/23.jpg)
合成可能であるということ
C#はIntelliSense指向言語 UnityもVisual Studioで書こう(少なくとも素エディタはやめよう)
![Page 24: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/24.jpg)
In Unity
![Page 25: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/25.jpg)
データソースを見つけよう
Unityにおいて、例えば?
![Page 26: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/26.jpg)
// Unityで活かせるLINQの底力!! (後編) からの引用// http://gamesonytablet.blogspot.jp/2013/01/unitylinq_24.html
// ターゲットに最も近い順で、5つのタグつきオブジェクトのレンダラのマテリアルを取得するvar materials = GameObject.FindGameObjectsWithTag("sometag")
.Select(r => r.renderer)
.Where(r => r != null)
.OrderBy(r => (r.transform.position - transform.position).sqrMagnitude)
.Take(5)
.SelectMany(r => r.materials)
.ToList();
配列っぽいもの見つけたら、必ず何か使える。LINQで考える事が習慣化されれば、LINQ firstで発想は浮かぶ。
![Page 27: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/27.jpg)
データソースを作り出す
http://neue.cc/2014/11/11_482.html
https://github.com/neuecc/LINQ-to-GameObject-for-Unity
// 子孫方向の全ゲームオブジェクトのうちtagが"foobar"のオブジェクトを破壊origin.Descendants().Where(x => x.tag == "foobar").Destroy();
// 自分を含む子ノードからBoxCollider2Dを抽出var colliders = origin.ChildrenAndSelf().OfComponent<BoxCollider2D>();
// 自分の真下にPrefabをCloneして追加origin.Add(new[] { prefab, prefab, prefab });
![Page 28: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/28.jpg)
死ぬ in iOS
http://neue.cc/2014/07/01_474.html
だが使うhttps://www.assetstore.unity3d.com/jp/#!/content/18131
![Page 29: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/29.jpg)
Deep Dive LINQ
![Page 30: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/30.jpg)
内部挙動気になる人へhttp://www.slideshare.net/neuecc/an-internal-of-linq-to-objects-29200657
Topics
![Page 31: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/31.jpg)
IObservable<T>
IEnumerable<T>
(LINQ to Objects)
DataSetXML IQueryable
SQL Twitter
![Page 32: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/32.jpg)
IEnumerable<T>
IObservable<T>
(Reactive Extensions)
UI
(ReactiveProperty)
OAuth
(ReactiveOAuth)IQbservable<T>
WMI
![Page 33: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/33.jpg)
時間軸を基盤にしたLINQの新しい形
Pushスタイル、IObservable<T>
time
間隔は不定期でも可
終わりがなくてもいい(無限に続く)
![Page 34: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/34.jpg)
DataSource
(IEnumerable<T>)
Action
値の要求(Pull)
DataSource
(IObservable<T>)
Action
値の送信(Push)
![Page 35: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/35.jpg)
LINQ to Asynchronous/Events
http://neue.cc/2014/08/23_476.html
https://github.com/neuecc/UniRx
http://u3d.as/content/7tT
Reactive Game Architecture
![Page 36: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/36.jpg)
Binding, ReactiveStateMachine, etc
https://www.assetstore.unity3d.com/jp/#!/content/14381
メジャーライブラリとなることでより安心して使える
よう前進
![Page 37: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/37.jpg)
Conclusion
![Page 38: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/38.jpg)
C#使うならLINQ使わなきゃmottainai
ところで性能は?
![Page 39: LINQ in Unity](https://reader031.vdocuments.site/reader031/viewer/2022013102/559440661a28abde5b8b4835/html5/thumbnails/39.jpg)