Limit Max Width of a Layout Component in Unity

Original post of the problem: http://forum.unity3d.com/threads/limit-max-width-of-layout-component.316625/

Demand

A textbox with a child text component. The textbox width would increase with the length of the text up to this maximum value at which point the width would be fixed forcing the text to wrap to the next line now increasing the height of the button.

A common needed of UI. I was surprised that Unity didn’t have max width for their vertical layout component so far… But never mind, using combinations of vertical and horizontal layouts could achieve what we want.

Continue reading

Bitfields for multiple flags in Unity

Demand

Supposed we’re going to develop a shooting game. Characters in our game have several flags such as IsMoving, IsShooting, IsLoading, IsHiding… All these behaviors might happen simultaneously (or, of course, individually), say, the character can shoot while moving, can load his gun while hiding. How are we going to set these flags?

The simplest way might be allocating bools for them respectively. However, bools in C# are stored as ints and the compiler might not be able to combines/consolidates them. So if there’re 16 bools, that’s an allocation of 16 ints – wasteful. In addition, if one of those combinations (eg. IsMoving + IsShooting) are frequently used, the codes might be burden.

Following are some tips for such situation.

Continue reading

[Quote] C#中的委托和事件

委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易。它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每次见到委托和事件就觉得心里别(biè)得慌,混身不自在。本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

Continue reading