Mostrando entradas con la etiqueta Unity3D. Mostrar todas las entradas
Mostrando entradas con la etiqueta Unity3D. Mostrar todas las entradas

lunes, 21 de abril de 2014

Events, delegates and functions as arguments in C#

Last year I learnt a bit of Scala and I realized that functional languages have, I have to admit, a lot of pros. I am not saying that the functional paradigm is one of my favorites; it is not, in fact I think that it is one of the most difficult to interiorize. However, some of the ideas of this paradigm are really useful and they would be very useful in other languages.

And suddenly, learning C# I discovered the delegates and how to pass functions as arguments in other functions. So simple, so easy, so clean, so beautiful that I had to share it with you.

Example of delegates in C#:
You can also use an anonymous method or a lambda expression instead of an actual method:
In this example with multiple classes subscribing to a delegate, we have a class that allows the user to make movements with the arrow keys (for example, to escape from a maze) and another class that constraints the number of movements to escape:
Instead of defining a new delegate each time, you can take advantage of the delegates that come with the .Net library which are Action and Func. The first one is used when the delegate returns void and the other when a value is returned (thus not common with events). Both are overloaded for different number of arguments, from 0 up to 16 (I think it's enough), with the form: Action(T1 arg1, T2 arg2, ...).
Passing function as arguments to other functions is also very easy. Here I modify the example to prompt the user to retry the game when the max movements are reached: Note that when you use delegates in Unity3D and you apply them to a MonoBehaviour, you must be careful with their lifetime and prevent the delegate from calling methods that are not anymore available. Be aware that changing the scene or removing the component from the game object, removes the script from memory but doesn't deallocate the callback, so you should remove it explicitly in the onDestroy method.

Enjoy!

jueves, 17 de abril de 2014

New challenges

New job, new developing tools, new projects, new versions of frameworks, ... What does it mean? More fun... well, sometimes ;-)

Since November I am working in a game company where we work with Unity3D, C# and all the surrounding tools and languages around such big project. It was completely new for me to change to a three dimensional world, but it's really worth it and I discovered that C# is a great language. I will definitely write about it.

In addition, I decided to start my own project with my sister, a 2d casual game. However, the new 2d Unity implementation does not suit all my needs and it only gives me headaches. Thus, I decided to start the project using Cocos2d v3.
I haven't progressed too much with the project but up to this point I am very happy with the improvements I've already seen in the framework. But it is quite recent, which means no documentation, no questions in stackoverflow, not many places to look at when problems arise... but this is an interesting challenge and I hope I can contribute with my findings in this blog.

This is plainly an update post, and a way to push myself into writing about these new topics.

viernes, 5 de abril de 2013

'Kamcording' your app

When you are a programmer, you get used to searching for new tools, fighting with poorly documented frameworks, and searching for solutions of problems that come with new staff. You also end up learning when to discard a tool. Tools may be bad, may not adjust to your problem, may give you more problems than solutions... or may be exactly what you are looking for... But what makes a tool great is the people behind it, a technical support that turns problems into solutions. And that's exactly how I would describe Kamcord.

The moment I saw this tool I knew it could be perfect for the new game we are designing, so I decided to make a project to test it. I had two problems: one with the rotation and the other was causing a crash with certain iOS version. Thus, I wrote to the supporting team. The answer was really fast and solved the orientation issue. They needed more information about my devices and project in order to solve the crashing problem so we exchanged several mails, projects, framework's updates... In just 5 days I had the whole project perfectly working. I didn't have to insist, but they were working on my problem till they got to the solution. And every mail was really polite. I'm very happy with the support I've received :)

Kamcord, this amazing tool, is a framework that allows your app's users to record the flow of the app and share it with friends. Easy and well documented.

Available for Cocos2d and Unity. Download from the web and follow the tutorial, it's well explained.
  1. Copy the Kamcord folder into your project.
  2. Add the framework and other supporting frameworks (twitter, mediaplayer, ... ).
  3. Adjust the settings for iOS < 4 (turn to optional some of the just added frameworks).
  4. Add to 'other linker flags': -ObjC -all_load -lxml2

At this point, it should compile, if not, check all the steps. At my first attempt it failed but I realized I  had skipped the third step.

On the other hand, Kamcord uses their own glview, therefore you have to do some changes in your AppDelegate.
  1. Make the root view controller extend from KCViewController
  2. Instantiate the glview as a KCGLView instead of an eaglview
  3. Set the root view controller to the window
  4. Configure Kamcord and set your developer key, secret and app name.
Notation for the comments:
Indented // comments: cocos2d autogenerated;
Non-indented // comments: code not anymore needed;
Indented //* comments: Kamcord explanations;
Non-indented //* comments: mine

Now, adding the functionality is really easy:
  1. Import the Kamcord framework on each scene you want.
  2. Call startRecording when you want to start the video
  3. Call stopRecording when you want to stop the recording
  4. Call showView (normally after you have record) to show the replay and share options of Kamcord

It will look like this









And you can see the video here :)