{"id":38840,"date":"2017-05-26T06:00:46","date_gmt":"2017-05-26T10:00:46","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=38840"},"modified":"2017-05-26T11:22:01","modified_gmt":"2017-05-26T15:22:01","slug":"unity-week-1","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=38840","title":{"rendered":"Unity: Week 1"},"content":{"rendered":"<p>Like I said <a href=\"?p=38757\">last week<\/a>, I&#8217;ve been dabbling in C# and Unity. Thanks so much to Riley Miller for suggesting <a href=\"http:\/\/catlikecoding.com\/unity\/tutorials\/\">these Unity Tutorials<\/a> in the comments. These are <b>exactly<\/b> what I was looking for. They&#8217;re some of the best tutorials I&#8217;ve ever read, actually. They&#8217;re text-based, they have code you can copy &#038; paste, they do visually interesting things so they&#8217;re fun to tinker with, they show all the steps you need, and they have little roll-out asides if you need some extra help. They begin with a simple base program and then modify it up to interesting levels of complexity rather than dumping four pages of inscrutable source on you and then trying to untangle it after the fact. Good stuff.<\/p>\n<p>I&#8217;m afraid I haven&#8217;t learned enough to explain anything useful yet. But I know some of you are curious how it&#8217;s going, so here are a bunch of random &#8220;first impressions&#8221; type thoughts.<\/p>\n<p><!--more--><\/p>\n<h3>Unity Is Faster Than I Expected<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_demo1.jpg' width=100% alt='Oooh! Shiny!' title='Oooh! Shiny!'\/><\/div><div class='mouseover-alt'>Oooh! Shiny!<\/div><\/p>\n<p>One of the tutorials I did was <a href=\"http:\/\/catlikecoding.com\/unity\/tutorials\/frames-per-second\/\">this one<\/a>, which is deliberately designed to tank your framerate. (The point of the exercise is actually to build a framerate counter.) The exercise has you create physics objects and have them smash into each other until your computer can&#8217;t keep up. You can see my version (pictured above) varies from the original, mostly because it was tremendous fun to play with. The project has a kind of sand-boxy appeal.<\/p>\n<p>It was shocking how well the engine held up under unreasonable circumstances. I had a couple thousand objects in the scene at once. All of them were active physics objects. I actually had some code that kept applying force to them to &#8220;stir&#8221; them around the central sphere. This meant that none of them were ever at rest, but were constantly moving, sliding, and bumping every frame. Every object was able to cast shadows on any other object, from three different lights in the scene. The camera was aimed right at the center of the action so very few objects could be ignored during rendering. The thing ran like a champ, keeping the FPS above 100 until the object count hit 5k or so.<\/p>\n<p>Yes, other game engines can do this. But I was getting this performance on a middle-range machine from interpreted code that hadn&#8217;t been optimized at all<span class='snote' title='1'>Because, not knowing the pitfalls of Unity, I wouldn&#8217;t know how to do that yet.<\/span>. I got this this wonderful simulation running with less than 100 lines of my own code.<\/p>\n<h3>MonoDevelop is Not Fun<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_demo2.jpg' width=100% alt='Okay, this stuff all seems pretty cool, but... WHERE DO I TYPE MY CODE?' title='Okay, this stuff all seems pretty cool, but... WHERE DO I TYPE MY CODE?'\/><\/div><div class='mouseover-alt'>Okay, this stuff all seems pretty cool, but... WHERE DO I TYPE MY CODE?<\/div><\/p>\n<p>The Unity workflow is strange. You spend a lot of time working in this editor (pictured above) with a 3D preview window. You can create game objects, construct hierarchies of game objects, set up your scene, position lights, build prefab objects, import models, and do all kinds of other cool stuff. The Unity editor can do everything&#8230; except edit code. Which is, you know, a pretty fundamental thing when developing software.<\/p>\n<p>For coding, it invokes a whole separate application called MonoDevelop. And it&#8217;s awful. It&#8217;s sluggish to start. That wouldn&#8217;t be a big deal except for a problem I&#8217;ll get to in the next paragraph. The two programs aren&#8217;t very well integrated, so I can&#8217;t just use a hotkey to run the program from the code editor. (There&#8217;s a hotkey to &#8220;run&#8221; but it doesn&#8217;t do anything. I imagine it works as intended for non-Unity programs, but it isn&#8217;t able to invoke Unity properly.) So I have to keep alt-tabbing between the two programs to compile and run. I have to compile in MonoDevelop so I can see my errors while the code is in front of me, and then I alt-tab over to Unity and it stalls for a split second while it does an additional compile of its own. <em>Then<\/em> I can test my program. It makes for a lousy workflow. <\/p>\n<p>Worse, MonoDevelop has a completely obnoxious bug where it will <b>lose the ability to paste text into your code<\/b>. This failure is absolute. You can&#8217;t paste with the hotkey, from the context menu, or from the main menu. Pasting text just stops working for no reason with no error message. <i>This is really annoying when you&#8217;re doing a lot of tutorials with blocks of boilerplate code and long mixed-case variable names. Which, yeah. Guess what I&#8217;ve been doing all week?<\/i><\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_monoeditor_sucks2.jpg' width=100% alt='On top of the copypasta bug, the code coloring sometimes gets totally confused and makes a mess of things. The code coloring in this image is way wrong.' title='On top of the copypasta bug, the code coloring sometimes gets totally confused and makes a mess of things. The code coloring in this image is way wrong.'\/><\/div><div class='mouseover-alt'>On top of the copypasta bug, the code coloring sometimes gets totally confused and makes a mess of things. The code coloring in this image is way wrong.<\/div><\/p>\n<p>That&#8217;s like a spreadsheet randomly losing the ability to <i>add things<\/i>. It&#8217;s so random and yet so fundamental to the use of the software I can&#8217;t believe the problem exists. The most common suggested remedy is to close MonoDevelop and re-start it. (And remember it&#8217;s sluggish to start.) <\/p>\n<p>I don&#8217;t know when the bug first appeared, but it goes back to <a href=\"http:\/\/answers.unity3d.com\/questions\/585997\/monodevelop-401-copy-paste-issue.html\">at least 2013<\/a>. While searching for answers, I did find this:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_monoeditor_sucks1.jpg' width=100% alt='' title=''\/><\/div><div class='mouseover-alt'><\/div><\/p>\n<p>Someone posted that the workaround was to (for some unfathomable reason) press CTRL LEFT ARROW then CTRL RIGHT ARROW, which would apparently make Mono Editor behave itself again. To which someone replied &#8220;WHY THE FCK IS THIS STILL NOT FIXED?!&#8221; That question was posted <i>a year and a half ago<\/i>.<\/p>\n<p>I don&#8217;t know what&#8217;s happened to Mono Editor in the year and a half since then, but whatever they&#8217;ve done:<\/p>\n<ol>\n<li>This bug is STILL NOT FIXED.\n<li>Ctrl+Arrow Keys thing no longer fixes the problem. (Assuming it ever did.) So you&#8217;re back to closing and re-opening the software.<\/ol>\n<p>Simply horrendous.<\/p>\n<p>I think Unity really ought to have a built-in editor. Barring that, the external editor shouldn&#8217;t have bugs in basic features. Barring that, I&#8217;d like some assurance that flagrant, well-documented bugs might be addressed in less time than it takes to earn a bachelor&#8217;s degree.<\/p>\n<h3>What About Visual Studio?<\/h3>\n<p>I switched over to Visual Studio for editing my Unity C# code. I&#8217;ve said in the past that I&#8217;m a fan of VS, and I think it&#8217;s the only truly excellent thing Microsoft has ever made. So doing my Unity coding in VS makes a lot of sense.<\/p>\n<p>Sadly, it&#8217;s awkward. Sometimes it works. Sometimes it doesn&#8217;t. For example if I try to use any of the editor-based classes &#8211; which is important for really getting the most out of Unity &#8211; the whole thing refuses to compile in VS and spews out linker errors. So I&#8217;d have to edit the code in VS and then have the Unity editor compile it. (And the way Unity reports compiler errors makes this kind of impractical anyway.) <\/p>\n<p>To be clear, I&#8217;m not really blaming either Microsoft or Unity about this. I understand why this kind of thing happens. This is not the native Unity environment, and both Unity and Visual Studio change often enough that it&#8217;s hard to maintain the makeshift bridge between the two.<\/p>\n<p>I&#8217;ve sunk an hour or so into trying to untangle these problems, but there are a lot of different pitfalls with similar-sounding error messages and honestly it&#8217;s really hard to know if a particular solution is for my particular problem, or if implementing it won&#8217;t simply create more problems.<\/p>\n<p>This wouldn&#8217;t piss me off so much, except this is exactly the sort of stupid bullshit C# is supposed to solve. People have been telling me for years how nice it is to use C# because you don&#8217;t have to worry about linker nonsense or library compatibility problems because it Just Works&trade;. And yet here we are. I&#8217;m barely a week into the language, I&#8217;m still working my way through the training wheels tutorials, and here I am reading obtuse compiler messages, searching forums for answers, fiddling with settings I barely understand, and generally spending time <b>not writing code<\/b>.<\/p>\n<p>And yes, this is technically a problem with Unity and not C# itself, but the point is that the ages-old C++ problems remain. Using amazing and complex tools makes for amazing and complex integration problems.<\/p>\n<p>In any case, the choice between Visual Studio and MonoDevelop comes down to figuring out which one is the lesser of two evils. I can use the development environment that becomes useless if you import the wrong packages, or the development environment that needs to be restarted every 20 minutes because it can&#8217;t paste. I think the VS problem is more debilitating, but the problem with MonoDevelop makes me more angry. <\/p>\n<h3>Despite All This, Unity is Pretty Fun<\/h3>\n<p><a href='http:\/\/catlikecoding.com\/unity\/tutorials\/object-pools\/'><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_demo3.jpg' width=100% alt='This program just flings random shapes at the sphere in the middle, where they slide or bounce off. Most fall into the abyss, but a few land on the little platform under the sphere. Click to see the tutorial this is based on.' title='This program just flings random shapes at the sphere in the middle, where they slide or bounce off. Most fall into the abyss, but a few land on the little platform under the sphere. Click to see the tutorial this is based on.'\/><\/div><\/a><div class='mouseover-alt'>This program just flings random shapes at the sphere in the middle, where they slide or bounce off. Most fall into the abyss, but a few land on the little platform under the sphere. Click to see the tutorial this is based on.<\/div><\/p>\n<p>Development environment agonies aside, Unity is really fun to play around with. It&#8217;s really good at making programs where generic primitives can smash into each other using physics and lighting, without you needing to do any of the legwork associated with that sort of business. <\/p>\n<p>It&#8217;s strange, because to a certain extent Unity makes very hard things very easy, but sometimes it makes easy things hard. On Wednesday I had three goals:<\/p>\n<ol>\n<li>Set up a first-person scene with realtime shadows.\n<li>Add 3D sound.\n<li>Make it possible to switch between walking around, and flying around.\n<\/ol>\n<p>The first two should have been a lot of work but were trivial, and the third should have been fairly easy but instead proved impossible at my current knowledge level. I gave up because it felt like I was still a few tutorials short of knowing how to implement this properly. <\/p>\n<p>I could nitpick lots of little confusing moments like this, but what I&#8217;m going through is basically par in terms of learning a new language \/ game engine. This is a hard thing to do and there are always confusing moments and headaches when you&#8217;re climbing a learning curve.<\/p>\n<p>So that&#8217;s my first week or so with C# and Unity. <\/p>\n<p>I <i>really<\/i> wish someone would fix that bug in MonoDevelop.<\/p>\n<p><strong>EDIT: I have found a workaround for the copypasta problem in MonoDevelop: Right-click the tab at the top of your source file and from the dropdown menu copy the path to the file. That will un-jam the MD clipboard and allow you to paste again.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like I said last week, I&#8217;ve been dabbling in C# and Unity. Thanks so much to Riley Miller for suggesting these Unity Tutorials in the comments. These are exactly what I was looking for. They&#8217;re some of the best tutorials I&#8217;ve ever read, actually. They&#8217;re text-based, they have code you can copy &#038; paste, they [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-38840","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/38840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38840"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/38840\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}