Tag Archives: C#

A better method to recalculate normals in Unity – Part 2

It’s been over 2 years since I posted about a method to recalculate normals in Unity that fixes on some of the issues of Unity’s default RecalculateNormals()  method. I’ve used this algorithm (and similar variations) myself in non-Unity projects and I’ve since made minor adjustments, but I never bothered to update the Unity version of the code. Someone recently reported to me that it fails when working with meshes with multiple materials, so I decided to go ahead and update it.

Continue reading

Unity vector swizzle operations

Swizzle operators are convenient syntactic sugar for creating new vectors based on the combination of elements of other vectors. They are used in shading languages quite a lot.

Example:

This is equal to:

They make more sense if you want to do some composition like so:

GLSL allows you to supply any amount of vectors or scalars in a vector constructor, as long as the count of all elements matches the dimension of the created vector.

However, swizzling isn’t allowed in most non-shading vector libraries. So, why not write simple extension methods that give that functionality to Unity?

Continue reading

Java is bad for games

I see the topic of choosing the “right language” when it comes to games brought up too many times in discussions all over the internet. The argument is usually between C++, C#, and Java. People do get religious about their favourite languages. At its heart this is an inherently flawed question, because the real decision should be on what framework or engine is best suited for a particular game, and that choice will usually dictate what language to use. Of course, the languages supported by a specific framework or engine can definitely be an important factor that may affect your decision.

And the truth is, assuming you have a choice, Java comes short in the race. When this is pointed out, some people erroneously claim that it’s a matter of “opinion”. That’d be true if it wasn’t so easy to prove that Java is ill-suited for games development. “You’d use the right tool for the right job” is very noble statement, and one that applies a lot in any kind of software development. But it’s also very abused and it gives the false impression that Java is sometimes the right tool.

It’s not.

Continue reading

Qute Configurer v0.2.2

I finally had some time to release a more complete version of Qute Configurer. This is a simple but extremely useful utility for anyone who wishes to develop in Unreal Engine using Qt Creator as their IDE.

The GitHub link can be found here, complete with source-code and binaries:  https://github.com/Zoodinger/QuteConfigurer/releases/tag/v0.2.2

This application is based on the guide that can be found here: https://wiki.unrealengine.com/Using_QtCreator_With_UnrealEngine4

A full tutorial is embedded in the executable which explains how to set up Qt Creator, complete with both text and pictures. After this is finished, the user can simply choose to generate the appropriate Qt project files required for editing, building, and running their projects.

The project is open-source, and I would be delighted if other people contributed.  Unfortunately, the current version only supports Windows. I don’t know if it’d be possible to make it work in Mac.

screenshot

Adventures with Qt

After a long time of silence, I decided to try out Unreal Engine. This experiment was long overdue, but I was happy using Unity so far. See, I do own the pro version of Unity 4.x and 5.x without paying (it was legally given to me to develop HandyMD), so not using it instead just seemed plain wrong. However, I wasn’t using it anyway. After HandyMD, the only Unity project I was involved with was not a game and is currently put on hold until further notice.

Continue reading

Serializing delegates in Unity

Delegates are one of the nicer features of C#. They’re essentially high-level and type-safe references to methods. But, like pretty much every fancy C# feature, delegate values are not serializable by default. If you hold a reference to a delegate, it will be lost during code hot-reload. I will show you a way to serialize them and explain how they work.

Note: This solution is inspired by the uFAction tool in Unity, which I am not affiliated with. Go check it out here.

Continue reading

Custom Serialization with ISerializationCallbackReceiver

Starting from Unity 4.5, any class can be made to inherit from ISerializationCallbackReceiver. This allows us to write methods that can be used to control seialization and deserialization, and it demands from the class to implement two public methods: OnBeforeSerialize() and OnAfterDeserialize(). These methods are automatically called when that class inherits from UnityEngine.Object or is marked with the [Serializable] attribute.

The idea behind this concept is both simple and powerful, but there are certain things you need to have in mind. Not only I will show you how to use this interface, I will also provide some information on things you should avoid or on things that won’t work as expected.

Continue reading

Controlling ScriptableObject instantiation

ScriptableObjects become (unfortunately) necessary when you start developing editor tools. That, and every other class that inherits from UnityEngine.Object, has the unique ability to maintain proper references after deserialization. Every other class creates a copy per reference (as if it were a struct) after deserialization, which can be very troublesome.

One of the least pleasant aspects of ScriptableObject is that you cannot control their instantiation. Using their constructor to create them does not work as expected. The only proper way to create a ScriptableObject from scratch is to call the ScriptableObject.CreateInstance<DerivedType>() static method, where DerivedType is a type which derives from ScriptableObject. Unfortunately, This method does not take any parameters. I will show you an easy way to help control their instantiation, so that the users of your ScriptableObject classes will be less prone to use them incorrectly.

Continue reading

Having fun with yield

I love C#. I’m not necessarily a fan of Microsoft or any company; I just think that, as a language, it’s far better than Java, its older cousin. Don’t get me wrong, I know that there is not one language to rule them all, and that it’s more about the framework and not the language. But, from a purely language feature point of view, C# is clearly much more expressive (unless you are biased against Microsoft) and it manages to combine multiple programming paradigms pretty well without being a mess (C++, I’m looking at you).

The LINQ library is one of its strongest features, but it wouldn’t be as amazing without the special keyword yield which it uses internally. I will show you how to use that to do fun stuff in C#, and how Unity takes advantage of its power to run co-routines.

Continue reading