Unity 2D Game Development


Unity 2D Game DevelopmentUnity 2D Game Development by Dave Calabrese
My rating: 2 of 5 stars

When I purchased this book that time I was not having experience in game development with Unity3D & C#. Coming from Actionscript/Flash background when Unity announced 2D features, I was among many of developers who felt blessed and made goal of learning unity by porting existing 2D games which were made in AS3 earlier.

For some reasons, I couldn’t start with this book earlier as got to work on some 3D project. Later when I picked this book last month, I felt that my decision/chance of first getting my hands on 3D project was good, because this book except you to have basic unity3d interface and programming knowledge.

I find it a fast paced book, which drive through a 2d platformer game step by step. Given the intended audience is who know Unity3D, author doesn’t spend too much time on stuff like where to find specific option. Book has a good space dedicated to State machine concept, which helps to understand concept in general itself a lot. Same goes for basic 2d physics and AI too. However, at same time with lacking information of some features which are used in project (e.g Spirte -> Pixtel To Unit option for asset).

In the eBook copy I found many typos which usually I never find in other Packt Pub books. Also the source code companied with book is little confusing due to its file/folder structure. However, since expectation for us as a reader is to be someone having experience of Unity3D thats not a very big issue, as we are gonna to type code ourself instead of copy paste while following the book.

However somewhere to maintain fast pace or some other reasons author had to skip certain elements like audio, ray casting, screen resolution handling, mobile platform specific quirks with unity2d tools etc, which I think very important and must have been included in book.

Since author shared real word problems and solutions which his team applied in the game studio, it makes book helpful in that context.

Overall this book should be a good & recommended as quick reference if you are working in Unity3D for 3D games already and want to develop a 2D game. You can go through this as a quick guide to start with 2D tools. However don’t expect that reading this book alone would be enough to get your 2d game released, as I mentioned that it lacks some pointers.

You can check book & TOC at http://bit.ly/1rrMS4D

Disclaimer: I got an offer for review copy of the book from Packt Pub, but since I had already purchased this one and was reading, I decided to share my review.

View all my reviews

My notes on Ruby on Rails – Day 3 (Riding Ruby)


So far I have explored basic concept of Ruby frameworks and commands to initiate a RoR web application. However, just like any other framework, rails also require basic knowledge of Ruby scripting language. Without that you can develop basic application but you can’t really understand generated code.
Keeping this in mind, I decided to enter in first R of RoR which is Ruby. I downloaded Ruby 1.9 and “The book of Ruby” pdf.

I completed chapter 1 today and in post I will summarize that.

  • Ruby is case sensitive.
  • Brackets are optional for methods in Ruby, however in some cases these are required.
  • Like perl, there isn’t any data type for variables. Also you don’t need to declare them in advance.
  • Global variables are defined with $ symbol, like $globalvar.
  • Semi colon after each statement is not required. However, if you add one, ruby won’t complain either.
  • To output line on console, we have 2 methods. puts and print. puts add a line feed after text while print doesn’t. That means, when we want to have cursor in same line, we should use print.
  • gets method is used to read user input from console. 
  • When we want to build a string with our variable in it, we can do this by adding #{varname}. For eg., lets say we have a variable name. And we want to say ‘Hello name‘. We can write like puts “Hello #{name}”.
  • Likewise PHP, single quoted strings having variables won’t be interpolated and will get printed as is. So if our previous statement is written like puts ‘Hello #{name} then out put will be Hello #{name} as opposed to Hello name.
  • #{ } can be used to embed non printing characters (\n, \t etc). Also we can even embed ruby methods or mathematical expression using this. In other words, anything enclosed with #{ } in a string will be executed by ruby interpreter as ruby code and will be replaced with output.
  • In ruby, we can write comments like perl starting with # sign. #this is a comment ^^
  • If we want to add multiline comments, then we need to use =begin and =end. And these should flush with left margin. 
  • Everything is an object in Ruby including numbers, strings. That said, we can call methods directly on these objects. For eg.,
    rate = 10.05
    amount = gets()   #this will read value from console
                                 #and store like a string.

    #this will convert string to float number (to_f is a method in String class).
    amount = amount.to_f    puts “Tax is Rs.#{(amount * rate) / 100)}”
  • Like Delphi/Pascal, we need to add then after if block. A typical if block looks like:
    #like wise methods, brackets are optional in
    #if statement too but, we should add
    if (amount < 0.0) then  
          #check if amount is negative and if so, change to 0.
          amount = 0.0
  • Sometime, we can find code which have entire if block as single line. In that case, then isn’t must. However at the same time, it doesn’t make code clean and readable too. So its not advisable.
  • Methods are defined with def statement. def methodname …. end.
  • +, – are operators in other languages. But in Ruby, these are methods. Consider this:
    x = 1 + 2
    Here + is a method of Fixnum (Integer) object 1. Value 2 is sent as argument to it.  Result 3 is returned and assigned to x.
  • To define a class in Ruby, we need to start a class .. end block. Class name should start with capital letter.
    class Animal
    #…. class body here.
  • To define an instance variable, we use @ prefix in classes. Like
    class Animal
       def set_name ( aName )
                @name = aName
  • To create an object of defined class, we use Class.new method. Then we can call methods using that object. Like:
    dog = Animal.new
    dog.set_name( “Tommy” )
  • If we want to define a constructor for our class, we can do it by defining initialize method in our class. 
  • Since like Java and most modern programming languages, Ruby has built in garbage collector. So we don’t need to define destructors.
  • To override a method, we just need to redefine that in our class. For example, if in our custom class, we define a method to_s then it will override to_s method which is used in standard Ruby object hierarchy.
  • To look inside an object, we can call another built-in method which is inspect and part of standard Ruby object hierarchy. For example, dog.inspect will print details about dog object.
  • There is a shortcut method p to inspect and print details about any object. Like:
    p ( dog )

My notes on Ruby on Rails – Day 2

Revising old stuff and few basics
  • Rails is a web application framework which follows MVC pattern and based on Ruby scripting language.
  • When we execute command rails project_name it creates a directory structure and set of classes and ruby files. Its basically a fully functional web application and web server. 
  • We can run ruby web server (webrick) by going to project_name folder and running ruby script/server command. It will start ruby webrick HTTP server on port 3000. If we want to use any other port then we need to add -p argument followed by port number.
  • Rails apps follows Convention over Configurations. That means, All rails application follow the basic file structure and consistent names for things. This enable Rails to understand how application works and that way framework can automate certain tasks.
  • To edit default page navigate, change HTML file public/index.html, public folder contains all the static content for the application. Like JS, CSS, HTML etc.
  • Most of the time, when dealing with database in a web application, we need CRUD operations, which is Create, Read, Update and Delete.
  • ruby script/generate scaffold book name:string isbn:string author:string price:decimal summary:text This will scaffold book entity and write basic code for manipulating books data. It creates controller, model and view for CRUD operations for books.
  • Rails follows DRY (Don’t Repeat Yourself) principal. That means if we tell something to rails once, we shouldn’t tell it again.
  • When rail generated scaffold code, it also created a script called a migration to create a table. In db/migrate folder, it created a file _create_books.rb
  • Using rake db:migrate we can ask rails to migrate database changes. Being smart, it will check and execute changes which are pending. In other words, if it execute a db migration script earlier, it won’t execute same.
  • rake = Ruby’s make
  • To add new fields in table, we need to generate a migration. For eg. if we want to add a new field publisher:string in our books table, then command will be: ruby script/generate migration AddPublisherToBooks publisher:string this will generate a new migration which will have alteration for table. Then we need to call rake db:migrate and it will make changes in database. However, in view we need to add new field manually.

Playing ‘Hide and Seek’ game with memory under linux


Some time you may have noticed that free -m command isn’t showing true stat.

             total    used   free  shared  buffers  cached
Mem:          4086   2302   1784       0        4      65
-/+ buffers/cache:   1232   2854
Swap:         2048      0  

In first sight you can say that Total 2302 MB memory is used. If you have some experience with linux memory system or free command results, then you answer will be total 1232 MB memory is used  and 1072 MB is cached.

Then, you may also want to execute ps awfux command to verify your answer and oh… you figure that only 400 MB memory should be used according to process table. So where is remaining memory ? Lets find that.

Linux page/buffer/inode/dentry caches are good and efficient mechanisms of caching and in most cases you want to leave them alone doing their job, which is speeding your computer by using unused RAM for caching.

Accessing /proc/meminfo in your shell should show you current memory allocation details. /proc/slabinfo may give you some inner-sight and clue about kernel memory management.

So now when you have located ‘hidden’ memory and you know that its there for good cause and increase speed of your system, you may want to pause this game, which is advised too.

However, if you just want to see your numbers back or doing some benchmarking/testing then you can ask linux kernel to free cached memory. Please note that these instruction works only under linux command 2.6.16 and above. Try at your own risk and don’t try at all if you are unsure.

To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

  • echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

  • echo 3 > /proc/sys/vm/drop_caches

Since this is a non-destructive operation, and some garbage but dirty objects are not freeable, you should run sync command first in order to make sure all cached objects are freed.

This may take 1-2 minutes and after that you will be back to shell. Try free -m command now. It should show almost same number in used memory as your process list is showing. Its a good idea to set drop_caches to 0 back, so that kernel can continue with its caching mechanism in future. For this:

  • echo 0 > /proc/sys/vm/drop_caches

Whoopee! You won the game!