Posts Tagged ‘Code’

Posts that have some sort of code.

Unity: Scaling the GUI based on the screen resolution

Click here to read Unity: Scaling the GUI based on the screen resolution

As hinted by other posts, here, you will find how to properly scale the GUI elements based on screen resolution. As one may have noticed, Unity doesn’t scale the GUI elements based on the screen resolution, requiring a script to do the job, which is explained in this post. I will assume that the reader already knows how to create and render GUI elements in Unity using the MonoBehaviour’s OnGUI() function and GUISkin objects.

The best way to explain how to properly scale a GUI element is through an example. That said, for this post, let’s assume that we wanted a yellow rectangle to be rendered at the top left and bottom right corners of the screen, like this: (more…)

Android: take a picture without displaying a preview

Click here to read Android: take a picture without displaying a preview

Accessing hardware functionality when programming for an Android device is generally quite straightforward. The same can be said about writing an Activity that takes a picture, but Android requires a preview of what the camera will capture to be displayed prior to capturing an image. This post explains how to “cheat” this requirement imposed by the OS, and how to write an application that takes a picture and displays it.

An Eclipse project with all the code explained here is available for download at the end of the post.

Before going into the Activity code, the interface layout (the main.xml file) must be edited to add a Surface View and an Image View to the interface. To add an element, just drag and drop it from the list inside your layout, like this: (more…)

Android: Acessing the gyroscope sensor for simple applications

Click here to read Android: Acessing the gyroscope sensor for simple applications

This post explains how to get values from the gyroscope (or any sensor that returns the device’s relative angle) to create simple application. The reason why I’m stating ‘for simple applications’ is because the code featured here is already deprecated. I’m just explaining how to do it because it still works, and it’s very clean and short to explain, as opposed to the new method, which is much more accurate but more complex to implement.

Still, it’s possible to use it for simple applications, although, if the application requires accuracy from the sensors, such as augmented reality applications or even games, it’s recommended to use the getRotationMatrix() method from the Sensor Manger class instead.

With that said, the following code just prints the rotation values from the gyroscope on the screen: (more…)

Android: How to return RGB values from an image file

Click here to read Android: How to return RGB values from an image file

Surprisingly, it’s quite easy to get the RGB value from an image file in Android. It’s certainly a lot easier than retrieving the pixel values from the Camera Preview. All that is required is to load the image file into a Bitmap object and them call the getPixel() method from the loaded bitmap.

It’s also possible to call this method to create an array of alpha and RGB values (ARGB) without calling the copyPixelsToBuffer() method, avoiding the use of buffers in Android which make things more complicated than it should be. As an example, the following code prints the color values of the pixels from the 4 corners of the image in LogCat and creates a RGBA array that stores each pixel color value:

(more…)

Unity: How to use a GUI Texture to play fullscreen videos

Click here to read Unity: How to use a GUI Texture to play fullscreen videos

Warning: this tutorial only works with Unity Pro because the free version doesn’t come with video playback support. The code was created and tested with Unity version 3.3. It won’t work for versions 2.6 and below (requires some adaptation).

This Unity post explains how to set up a GUI Texture at the Unity editor and the code necessary to play a fullscreen video, that can be used for the studio logo animation at the beginning of the game, the game’s intro or any other video that needs to take the whole screen. It also explains how to properly scale the video based on the screen dimensions. At the end of the post, a Unity project featuring all the code explained here is available for download.

(more…)