Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Gems: LINQ tutorial

Discussion in 'Made With Unity' started by whydoidoit, Nov 24, 2012.

  1. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    If you want to know more about how LINQ can help you right amazingly powerful list manipulations, sorts, closest object calculations in a tiny number of lines of code - then this new tutorial will be a good introduction to a great, under utilized feature of .NET and Unity.

    Available On
    Unity Gems
     
    EliasMasche likes this.
  2. LucasDaltro

    LucasDaltro

    Joined:
    Oct 31, 2010
    Posts:
    236
    Congratz for this amazing website bro!!!
    Your tutorials are helping me a lot on getting more "advanced" at unity3d
     
  3. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    Thank you very very much Sir,

    Before visit your site i never heard about "whydoidoit", but now i feel that i missed lots of things regarding unity. Your tutorial regarding LINQ is very helpful for us. I also liked your Serializer package.

    Again Thank you very much for all unity packages.
     
  4. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    You are very welcome :)
     
  5. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    I'm really glad they are helpful - we weren't sure that we would have a large audience when we started, but it seems that a lot of people are interested in going further with Unity!
     
  6. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    Excellent,
    I didn't know about this site
     
  7. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    That's no surprise, we're pretty new - started last month... Feel free to spread the word ;)
     
  8. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Please let me know if you need any help reviewing your linq tutorials as you publish them, while i'm new to the realm of unity (6month ish) i'm fairly expert in LINQ in all its forms.
     
  9. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    That would be great!! There's always a cool way to do something in LINQ and as it gets more complicated a reviewer would be helpful. Actually ideas of how to use LINQ with Unity would also be helpful - I use it a lot, but I'm sure I haven't thought of a million uses ;)
     
  10. marcaurelio74

    marcaurelio74

    Joined:
    Sep 2, 2012
    Posts:
    60
    Yours tutorial are amazing, clearly written, well organized.
    Really a good and useful work.
    Thanks
     
  11. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    Ronan, I may say as well, that if you want to contribute, you may provide your own tutorials as well. You are more than welcome.

    If you think you do not master English to the point you could write an article (though you seem to be ok), the website is starting a French section.

    Fafase
     
  12. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Love reading all these articles, keep it up!
     
  13. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,204
    My only comment is that LINQ is notorious for being a speed bottleneck. Just be careful where/when/how you use it - like all things of course. :)
     
    Callski likes this.
  14. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    It's not notorious for that, this is FUD that is being spread by people who don't know linq.
    It's no more true than most of the "this high level feature just has to be slow" that has been plaguing software developement since the days we moved away from raw assembler programming. In most cases linq will be between 0.0000000000000000001% slower (very very slight near empty method overhead) and a "few bytes" more per operator than the best you could do without linq, in practice however it will almost always be much faster as the deffered partial execution model of linq is way way better than what you'd usually type without it. And if you'd really want to get back those tiny microseconds by typing it, you'd end up with an unmaintainble mess instead.
    I've been routinely writing linq queries 30 to 1000 lines long (yes really) when "rewriting" components for speed and getting 10-100X speedups.
     
  15. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    Yes - as mentioned in the tutorial it can both allocate memory and run at about 1/2 the speed of the equivalent for next loops so should rarely be used every frame in an Update loop.
     
  16. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    In some direct tests I did - it was certainly slower than an equivalent for..next and allocated memory during execution on the heap (which requires garbage collection).

    I do agree that you can frequently massively improve efficiency and memory usage using LINQ when the situation is complex and gets manual approaches get out of hand.
     
  17. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,204
    Wow, Ronan, you obviously feel strongly about it. It was just a warning. Too many times we've seen poor decisions because LINQ was easy, and doing things right was longer and harder. I'm sure all your code is very fast, and can be no faster or better. But for those that aren't superhuman, LINQ can certainly be misused, and everyone should always consider what is appropriate when coding. I would hate to look at a 30 line LINQ query. A 1000 line query would be hell to debug. But, if you aren't working on a team, and you have a perfect memory, then I suppose it does not matter.

    @whydoidoit:

    This wasn't meant to be a criticism of your work, thanks for making tutorials, and your serialization system is very nice. Your work is appreciated.
     
  18. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722

    The 1000 line query is part of an international project worked on by a multilingual team and has proven to be trivial to debug.
    It's easy to produce slow or incorrect code for a begginer with "any" API. My take is that anyone working with linq normally (including begginers) will usually produce much faster and robust code than by not using linq (talking linq to objects here, it's a bit diferent when talking about iqueryables)
    So no. I bet any begginer will produce better and more maintainable with linq than without for anything non-trivial. However i simpy felt it was important to bring an educated viewpoint on linq as there seems to be a thought that it's inherently slow while, by design, it's inherently faster wherever possible than any (sane and maintainable) handcrafted solution.
     
  19. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    A free quick bump to say that i really appreciate your tutorials on surface shaders!
     
  20. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    Many thanks :)

    On the Linq front - I think that as soon as you "get" Linq it is very easy to read - it's much harder to debug with MonoDevelop than it is in Visual Studio running on real .NET which is a shame, but it's not that bad if you ask me.
     
  21. DavidDebnar

    DavidDebnar

    Joined:
    Mar 19, 2011
    Posts:
    115
  22. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    I would strongly recommend AGAINST using LINQ, especially if you plan to deploy to iOS. LINQ works great on Android, pc, and web. But for iOS it does not perform as expected. To the point that it won't properly sort items as expected and can cause crashes do to memory allocation. Be very very careful. Do not use LINQ if you are deploying to iOS
     
  23. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Don't see how that's possible at all, it's much more likely that you have a bug/missuse in your code than that you found a bug in mono on ios.
     
  24. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
  25. MXAdd

    MXAdd

    Joined:
    Apr 23, 2015
    Posts:
    74
    I also strongly recommend AGAINST using LINQ in the context of consoles (PS3, XBOX360, PS4, XBOX-ONE) as all of them use AOT and have HUGE problems with LINQ code, If you plan to port your game into consoles, better thing twice before using LINQ and test your code immediatelly on target platform before You regret in future.