Need help with C# (Unity)(Damage Numbers with Image)

Started by MarkHest, December 24, 2015, 03:28:05 pm

Previous topic - Next topic

MarkHest

December 24, 2015, 03:28:05 pm Last Edit: December 24, 2015, 03:30:56 pm by MarkHest
Hiya!

There are some pretty reliable coders here and I don't know where else to ask, so maybe one of you can help me with a problem I'm facing in C# coding.
I'm currently making a game in the Unity Engine, it's an RPG/platforming game. If anyone of you is familiar with Unity that would be a great help as well.

Ok, now to the point!
When I hit an enemy in-game I want the damage inflicted to be displayed as an image and jump out of the enemy then fade out.

I have this example image:

Image Width: 500 Pixels
Image Height: 50 Pixels
(I will be using these dimensions in game)


What I need help with is making a Method that takes an Int and then cuts the image into each number in the Int, then displays it. Let's say in this case that the Int is 560 so that means the image would have to be sliced three times and put next to each other. I'm not sure if each number should be its own object, that would probably be best...

(The Damage Images script made by G_G does exactly what I want, but that's in Ruby and I haven't learned it yet so I can't read it... i think :p)

Help me improve on programming! :haha:
   

winkio

You shouldn't have to cut the image into smaller images, you should be able to only draw only a specified region of the large image.  I'm not familiar with Unity, so I can't give you an exact method call, but look for a draw method with an argument named 'source' or something similar.

So you should make a new object that gets drawn like this:
The image width will be the digit width times the number of digits (you can get number of digits and the actual digits themselves by recursively dividing by 10 and taking the remainder)
The image height will be the height of a digit.
And then in the draw method, draw each digit using a source rectangle and add an offset to the X position so that it gets drawn to the right of the last digit.

MarkHest

So how do I get each individual digit in the Int?  :^_^':
   

winkio

Quote from: winkio on December 24, 2015, 04:01:18 pm
The image width will be the digit width times the number of digits (you can get number of digits and the actual digits themselves by recursively dividing by 10 and taking the remainder)


List<int> GetDigits(int num)
{
    List<int> result = new List<int>();
    int remainder = num;
    // I'm just ignoring 0 and negatives, you could easily change the code to work for them as well
    if (remainder <= 0)
    {
        result.add(0);
        return result;
    }
    // loop through each digit until there is no remainder left
    while (remainder != 0)
    {
        // next digit is remainder % 10
        result.add(remainder % 10);
        // divide remainder by 10.  This will truncate the last digit and move the next digit into the 1s place
        remainder = remainder / 10;
    }
    // the digits are in reverse order, so reverse the list (we got the 1s digit first, then the 10s, then the 100s, etc.)
    return result.reverse();
}

R.A.V.S.O

well, I kinda remember this old post from the unity3d forums:

http://answers.unity3d.com/questions/318337/replacing-letters-with-images.html

but the concept is still the same, use a bitmap font to basically replace any numbers (or any set of letters for the matter)
with your desired text,

just as a heads up, if you're using unity 4.6 or higher it's most likely that these forms of GUI texts are now handled as
UI components, therefore you gotta handle them in the UI layers, mostly through the text objects.

here's a page containing most of Unity's UI functions, you'll probably find the snippet of code required under UI.text

https://unity3d.com/learn/tutorials/topics/user-interface-ui

good luck :D
Personality Test results
Spoiler: ShowHide




"Life is unfair, so make it unfair in your favor" -Sesilou

winkio

Bitmap fonts also have the advantage of not being fixed width, so the spacing will be much better than if you used the image in your example.  You can find some good tools for making the base of a bitmap font from a standard font where you can pick the font size and style.  Makes it easy if you just want to manipulate the colors/outlines in something like photoshop.

MarkHest

December 24, 2015, 05:44:19 pm #6 Last Edit: December 25, 2015, 12:37:13 pm by MarkHest
I did this:



public void DamageNumber(int sndDamage, Vector3 sndImpactPosition)
    {

        string tmpString = sndDamage.ToString();
        float tmpRange = 0;

        for(int i = 0; i < tmpString.Length; ++i)
        {
            //int tmpInt = int.Parse(tmpString.Substring(i));

            int tmpInt = int.Parse(tmpString.Substring(i, 1));

            GameObject tmpObject = (GameObject)Instantiate(pDigitObject, new Vector3(sndImpactPosition.x + tmpRange, sndImpactPosition.y, sndImpactPosition.z), new Quaternion(0, 0, 0, 0));
            tmpRange += 0.4f;

            if(tmpInt == 0)
            {
                tmpObject.GetComponent<SpriteRenderer>().sprite = pDamageZero;
            }
            else if(tmpInt == 1)
            {
                tmpObject.GetComponent<SpriteRenderer>().sprite = pDamageOne;
            }
           
        }

    }



it turned out like this:

Spoiler: ShowHide



I used individual sprites instead of the number sheet, each being 50pixels width and height. Then changing the sprite of the newly instantiated game object to the number in the Int. I found it much easier that way.

Mission accomplished!  :haha:
Thanks for the guidance you two! :D



Next up is making Health Bars above enemies that aren't part of the GUI. Any ideas on how to trim/scale the health bar sprite without using the .fillamount option in the GUI?  :roll:
(This is something I've googled a lot and can't find a good solution)
(Nevermind, It's fixed now!)
   

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

MarkHest

   

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.