Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Wednesday, August 12, 2015

Those damn carets!! Unity's Input Field Caret Position issues and how to fix it.

Ahhh the joys of struggling with something for a few hours, and then all of a sudden finding a very simple solution for it.

From version 4.6, Unity added a new set of UI elements you can use to easily make user interfaces for your games. Now, I've only really been using Unity since version 5, so I don't know most of the struggles a lot of you guys out there know.

In Delta Quadrant we used nGUI, because when development for that started there wasn't UI yet...

Anyway, with Catch a Falling Star, the task for the menu system has fallen on my shoulders, and I'm glad it did. I've learned a huge amount by building the menu and settings screens for the game. And, as with learning anything new, it included some hairs going grey or being pulled out out of frustration.

Today, for example, those damn carets drove me crazy! One of the UI elements I am using in the game is the Input Field. This is basically a text box allowing the player to type in something. Like any decent text box, it should have a caret as well - you know, that blinking thing showing you where you're going to be typing the next letter. As I'm writing this, it's here as well, and it's a handy thing for sure - I would be sad to not have it in my life.

Except, for some reason, the Unity UI Input Field's caret is NOT AT THE RIGHT POSITION!! And it drove me semi-insane today! I mean, just look at this:


I tried everything just to move that damn caret up - I did figure it out in the end, thank goodness, but hell, why does it do that Unity people??

It turns out that it has to do with the position of the pivot of the Text component. Here's the default values:



It turns out the pivot determines where the caret will be placed. Changing the Y value to 1.5 instead of 0.5 actually puts the pivot outside the text box, but that doesn't really matter in this case. It does, however, fix this extremely annoying bug :)


Now, isn't that a beautiful site to behold? I hope this little trick can save you some of the headaches I experienced. You might have to tweak your values slightly, but this helped me a great deal!

Sunday, June 7, 2015

Simple Sorting Algorithm for a Poker Hand

Last week I played around and made a very simple Unity app that deals 2x 5 card Poker hands. The main reason for the exercise was to familiarize myself a bit more with stuff like enums, structs, arrays, lists, etc. in C# in Unity.

I know all those things, but I needed some practice to get it to do what I want in C# (was really easy in PHP haha!).

Anyway, Jayson helped me a bit, and I got it right in the end. The one thing I want to share though, is the simple sorting algorithm I used to put your hand in order from low card to high card. There are many sorting algorithms out there, some way more efficient than others. A very simple one is the Selection Sort. It's not the best one to use if you have huge lists of items that needs sorting, but for a small list, like 5 cards, it's a great and simple way to get the job done.

Check out the explanation for it here: http://en.wikipedia.org/wiki/Selection_sort

And, here's the loop structure I used in my C# script:

for (int i = 0; i < playerHand.Count; i++) 
{
    int j = i;

    for (int k = i+1; k < playerHand.Count; k++)
    {
        if (playerHand[k].value < playerHand[i].value)
{
            j = k;

            Card tempHand = playerHand[i];
            playerHand[i] = playerHand[j];
            playerHand[j] = tempHand;
        }
    }
}

playerHand is the list containing the five cards (and each card has a value property that I use - for example, a two's value is 2, a Jack's value is 11, etc).

As you can see, if the list gets huge, then this will take forever because the loop is essentially running through the whole list over and over again, with each new run being one item less...

Basically, here's how it works. I take the first card, and run through the remaining cards and check their values against the first card. If I find a lower valued card, I swap the two around. Now I move on to the second card, and check again on the remaining cards to see if I can find one with a lower value, and if so, swap them around. I carry on like this until I reach the end. For a Poker hand, you don't need anything more in my opinion. Sorting them in this fashion also makes it a lot easier to analyze the hands!

And see, it works:





I've also made the script analyze the hand and display what it is. This was actually a lot easier than I thought - you basically start at the best Poker hand, which is a Straight Flush - test for that - if it's not one, test for the next best hand, a Straight Flush. Then Four-of-a-Kind. The Full House, Flush, Straight, Three-of-a-Kind, Two Pairs, Single Pair and lastly nothing. If you do it this way around, you don't have to worry about over complicating things when you test for the weaker hands... :)

Anyway, hope you find this helpful!