It’s Time To Think Linq

动机

如果你有以下迷惑,你应该看看这篇文章

  • 你想办法找到所有与GameObject.FindGameObjectsWithTag的变换(),而不是游戏本身的对象
  • 你需要操作,排序和更改列表和数组的类型,但似乎总是写太多的代码
  • 你听说过LINQ,但不知道它是如何工作
  • 你不相信我,当我说我可以写一行代码返回所有的材料上最接近目标的五个标记对象的渲染器!(或其他任何看似复杂)

虽然我设置的谜题,你是担心写一行代码来完成所有这些- ?为轻松了一口气你为什么不也考虑这个问题你能想到的英文句子是语法正确,并且包含单词“和”连续5次?   的完全不相干的答案是在年底(不偷看)

介绍

LINQ的代表语言集成原生查询和NET中它写的东西,看起来像数据库查询,但对对象的工作的一种方式。这是非常强大的,当你得到你的头周围,很容易上手。

LINQ是可以比写出所有的下一次循环更慢。它最多可以有一半的速度,所以应该在它发生的每一帧更新功能慎重考虑。这是伟大的缓存,它是伟大的处理。

The Power Of Enumerations

 

Enabling LINQ

To use Linq you need to add an import or a using directive to your source code.

//C#

using System.Collections.Generic; //Always a good idea

using System.Linq;

//JavaScript

import System.Linq;

A First Linq Statement

  • Find all the game objects
  • Select the transform from the game object
  • Turn the result into an array

//C#

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(go => go.transform)

.ToArray();

//Javascript

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(function (go) go.transform)

.ToArray();

Performing a Quer

Ok so the Q is Query - let's do something that asks a question.  Let's get all of the transforms for the objects within 10 world units of the current position.

//C#

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(go => go.transform)

.Where(t => Vector3.Distance(t.position - transform.position) < 10

.ToArray();

//Javascript

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(function (go) go.transform)

.Where(function (t) Vector3.Distance(t.position - transform.position) < 10)

.ToArray();

Performing a Sort

//C#

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(go => go.transform)

.Where(t => Vector3.Distance(t.position - transform.position) < 10

.OrderByDescending(t => {

var danger = t.GetComponent<Danger>();

return danger ? danger.dangerLevel : 0;

})

.ToArray();

//Javascript

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.Select(function (go) go.transform)

.Where(function (t) Vector3.Distance(t.position - transform.position) < 10)

.OrderByDescending(function (t) {

var danger = t.GetComponent(Danger);

return danger ? danger.dangerLevel : 0;

})

.ToArray();

Drilling Down

 

//C#

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.SelectMany(go => go.GetComponentsInChildren<Renderer>())

.ToArray();

//Javascript

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.SelectMany(function (go) go.GetComponentsInChildren(Renderer))

.ToArray();

 

Linq inside Linq

 

//C#

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.SelectMany(go => go.transform.Cast<Transform>()

.Select(t=>t.renderer)

.Concat(new [] { go.renderer })

.Where(r=>r!=null)

)

.ToArray();

//Javascript

var transformArray = GameObject.FindGameObjectsWithTag("MyTag")

.SelectMany(function (go) go.transform.Cast.<Transform>()

.Select(function (t) t.renderer)

.Concat([go.renderer])

.Where(function (r) r != null)

)

.ToArray();

Finding the Closest Object

//C#

var closestGameObject = GameObject.FindGameObjectsWithTag("MyTag")

.OrderBy(go => Vector3.Distance(go.transform.position, transform.position)

.FirstOrDefault();

//Javascript

var closestGameObject = GameObject.FindGameObjectsWithTag("MyTag")

.OrderBy(function (go) Vector3.Distance(go.transform.position, transform.position))

.FirstOrDefault();

Lists and Dictionaries

 

//C#

var lookupByTag = GameObject.FindObjectsOfType(typeof(GameObject))

.Cast<GameObject>()

.Where(go=>!string.IsNullOrEmpty(go.tag))

.ToLookup(go => go.tag);

//JavaScript

var lookupByTag = GameObject.FindObjectsOfType(GameObject)

.Cast.<GameObject>()

.Where(function (go) go.tag != "")

.ToLookup( function (go) go.tag );

 

文档资料

英文出处:http://unitygems.com/linq-1-time-linq/

posted @ 2014-07-22 16:06  赵青青  阅读(395)  评论(0编辑  收藏  举报