LINQ in Unity

39

description

LINQ in Unity LINQ to GameObject - https://github.com/neuecc/LINQ-to-GameObject-for-Unity/ UniRx - https://github.com/neuecc/UniRx

Transcript of LINQ in Unity

Page 1: LINQ in Unity
Page 2: LINQ in Unity

@仕事

C#

@個人活動

http://neue.cc/

@neuecc

https://github.com/neuecc/UniRx

Page 3: LINQ in Unity

神獄のヴァルハラゲート

モンスタハンターロアオブカード

http://gihyo.jp/dev/serial/01/grani/0001

http://grani.jp/recruit

Page 4: LINQ in Unity

using

Page 5: LINQ in Unity

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

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

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

サーバーもクライアントもC#

同一ソリューションファイルに格納

言語依存を強みに変える

サーバーサイド(C#/ASP.NET)とクライアントサイド(Unity)でデータ共有

Page 9: LINQ in Unity

Introduction to LINQ

Page 10: LINQ in Unity

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

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

LINQ...

Page 13: LINQ in Unity

SQLみたいなもの?

Page 14: LINQ in Unity

似てる、半分正しい。

var query = from x in source

where x % 2 == 0

select x * x;

Page 15: LINQ in Unity

Language INtegrated Query

Unityでの実用上の場合は

Page 16: LINQ in Unity

// クエリ構文

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

// クエリ構文

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

// 列挙してフィルタリングして詰め直す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

フィルタして変形して詰め直しは頻出パターン(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

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

値がかぶった時に別の値で比較する、というのも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

foreachでいい?

機能が弱ければ弱いほどコードが意図を伝える

単機能だからこそ、見ただけで何をやっているのか分かる。それに加えて短く書けるが故に書きやすいし、そのこと自

体が読みやすさにも寄与する

Page 23: LINQ in Unity

合成可能であるということ

C#はIntelliSense指向言語 UnityもVisual Studioで書こう(少なくとも素エディタはやめよう)

Page 24: LINQ in Unity

In Unity

Page 25: LINQ in Unity

データソースを見つけよう

Unityにおいて、例えば?

Page 26: LINQ in Unity

// 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

データソースを作り出す

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

死ぬ in iOS

http://neue.cc/2014/07/01_474.html

だが使うhttps://www.assetstore.unity3d.com/jp/#!/content/18131

Page 29: LINQ in Unity

Deep Dive LINQ

Page 30: LINQ in Unity

内部挙動気になる人へhttp://www.slideshare.net/neuecc/an-internal-of-linq-to-objects-29200657

Topics

Page 31: LINQ in Unity

IObservable<T>

IEnumerable<T>

(LINQ to Objects)

DataSetXML IQueryable

SQL Twitter

Page 32: LINQ in Unity

IEnumerable<T>

IObservable<T>

(Reactive Extensions)

UI

(ReactiveProperty)

OAuth

(ReactiveOAuth)IQbservable<T>

WMI

Page 33: LINQ in Unity

時間軸を基盤にしたLINQの新しい形

Pushスタイル、IObservable<T>

time

間隔は不定期でも可

終わりがなくてもいい(無限に続く)

Page 34: LINQ in Unity

DataSource

(IEnumerable<T>)

Action

値の要求(Pull)

DataSource

(IObservable<T>)

Action

値の送信(Push)

Page 35: LINQ in Unity

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

Binding, ReactiveStateMachine, etc

https://www.assetstore.unity3d.com/jp/#!/content/14381

メジャーライブラリとなることでより安心して使える

よう前進

Page 37: LINQ in Unity

Conclusion

Page 38: LINQ in Unity

C#使うならLINQ使わなきゃmottainai

ところで性能は?

Page 39: LINQ in Unity