Author Topic: Need help with C# (Unity)(Damage Numbers with Image)  (Read 1272 times)

Offline MarkHest

  • Video Game Music Composer
  • Astral Trancist
  • *****
  • Posts: 700
  • LV: 16
  • Gender: Male
  • Smile, Laugh, Live
    • View Profile
    • My Youtube Site
Need help with C# (Unity)(Damage Numbers with Image)
« on: December 24, 2015, 10:28:05 PM »
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:
« Last Edit: December 24, 2015, 10:30:56 PM by MarkHest »
   

Offline winkio

  • Epiq
  • Administrator
  • Guardian of Chaos
  • *****
  • Posts: 4506
  • LV: 199
  • Gender: Male
  • I am lying.
    • View Profile
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #1 on: December 24, 2015, 11:01:18 PM »
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.

Offline MarkHest

  • Video Game Music Composer
  • Astral Trancist
  • *****
  • Posts: 700
  • LV: 16
  • Gender: Male
  • Smile, Laugh, Live
    • View Profile
    • My Youtube Site
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #2 on: December 24, 2015, 11:10:12 PM »
So how do I get each individual digit in the Int?  :^_^':
   

Offline winkio

  • Epiq
  • Administrator
  • Guardian of Chaos
  • *****
  • Posts: 4506
  • LV: 199
  • Gender: Male
  • I am lying.
    • View Profile
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #3 on: December 24, 2015, 11:31:03 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)

Code: [Select]
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();
}

Offline R.A.V.S.O

  • Wandering D-Soldier
  • Ethereal Devastator
  • ****
  • Posts: 268
  • LV: 7
  • Gender: Male
  • "D-Soldiers! On my mark!"
    • View Profile
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #4 on: December 24, 2015, 11:38:53 PM »
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
(click to show/hide)



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

Offline winkio

  • Epiq
  • Administrator
  • Guardian of Chaos
  • *****
  • Posts: 4506
  • LV: 199
  • Gender: Male
  • I am lying.
    • View Profile
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #5 on: December 24, 2015, 11:44:07 PM »
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.

Offline MarkHest

  • Video Game Music Composer
  • Astral Trancist
  • *****
  • Posts: 700
  • LV: 16
  • Gender: Male
  • Smile, Laugh, Live
    • View Profile
    • My Youtube Site
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #6 on: December 25, 2015, 12:44:19 AM »
I did this:


Code: [Select]
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:

(click to show/hide)


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!)
« Last Edit: December 25, 2015, 07:37:13 PM by MarkHest »
   

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19953
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #7 on: December 25, 2015, 01:17:09 PM »
That looks pretty sweet. :)
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline MarkHest

  • Video Game Music Composer
  • Astral Trancist
  • *****
  • Posts: 700
  • LV: 16
  • Gender: Male
  • Smile, Laugh, Live
    • View Profile
    • My Youtube Site
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #8 on: December 25, 2015, 06:02:26 PM »
Have you ever worked in Unity, Blizzy?  :D
   

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19953
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Need help with C# (Unity)(Damage Numbers with Image)
« Reply #9 on: December 25, 2015, 11:05:03 PM »
Yup.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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