- Home /
Need help converting unityscript to c#
Can someone help me convert this script to C#? It's a health bar script
var healthTexture : Texture2D;
var healthBorder : Texture2D;
var curhealth : int = 100;
var maxhealth : int = 100;
function OnGUI () {
GUI.DrawTexture(Rect(43,Screen.height - 65,314,40), healthBorder);
var adjust : int = health * 3;
GUI.BeginGroup(Rect(40,Screen.height - 67,adjust,30));
GUI.DrawTexture(Rect(0,0,323,46), healthTexture);
GUI.EndGroup();
}
And one more thing, I'm trying to insert something like that so that the "healthTexture" will move slide across (so it would look like it's being depleted as any normal rpg game due to the percentage) I used the code I posted above, it worked but when the health is at like 50% the "healthTexture" is below halfway point on the "healthBorder"
`adjust = (Screen.width / 4) * (curhealth / (float)maxhealth);`
Thanks in advance!
Here's some links I found useful in converting between C# and JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Answer by phodges · Oct 24, 2012 at 09:00 PM
How about something like this?
using UnityEngine;
public class SimpleHealthBar : MonoBehaviour {
#region Editable properties
public Texture2D healthTexture = null;
public Texture2D healthBorder = null;
// Pick a convenient parameterisation for background coordinates
public Vector2 borderLocation = new Vector2(40f, 65f);
public Vector2 borderSize = new Vector2(314f, 40f);
// How far horizontally and vertically to offset the resizeable bar
public Vector2 barInset = new Vector2(3f, 2f);
// The largest permitted health Value
public int maxHealth = 100;
#endregion
Rect _borderBox;
Rect _barBox;
int _currentHealth;
// Read and change recorded health
public int Health {
get { return _currentHealth; }
set { _currentHealth = (int)Mathf.Clamp(value, 0f, maxHealth); }
}
// Use this for initialization
void Start () {
// Fixed rectangle used to display the background texture
_borderBox = new Rect(borderLocation.x, Screen.height - borderLocation.y,
borderSize.x, borderSize.y);
// Basis for drawing the health bar
_barBox = new Rect(_borderBox.x + barInset.x, _borderBox.y + barInset.y,
borderSize.x - 2f * barInset.x, borderSize.y - 2f * barInset.y);
_currentHealth = maxHealth;
}
void OnGUI() {
GUI.DrawTexture(_borderBox, healthBorder);
// Draw less of the bar as health declines, trimming texture coordinates to match
float f = Mathf.Clamp01(_currentHealth / (float)maxHealth);
Rect texCoords = new Rect(0f,0f,f,1f);
Rect trimmedPos = new Rect(_barBox.x, _barBox.y, f * _barBox.width, _barBox.height);
GUI.DrawTextureWithTexCoords(trimmedPos, healthTexture, texCoords);
}
}
Wow thanks you did a lot of typing there. Thanks! But at line 24 I get "Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)"
Thanks for your help but when increased the life bar gets lower and when decreased it gets higher and creates copies of itself :/
Surprised to hear this, is it worked alright in the test I made. For test purposes, you might add something like this to OnGUI:
GUI.Label(new Rect(10f,10f,40f,40f), _currentHealth.ToString());
I also added a temporary Update function to move the Health value up and down over time, so as to be able to check on presentation. If you take a couple of screengrabs and post them somewhere then I'll take a look and see if I can figure it out. Also, let me know about any changes made, dimensions of textures, etc.
Answer by Rati · Oct 24, 2012 at 08:23 PM
For the script :
using UnityEngine;
using System.Collections;
public class MyLifeBarClassName : MonoBehaviour {
public Texture2D healthTexture;
public Texture2D healthBorder;
public int curhealth = 100;
public int maxhealth = 100;
void OnGUI()
{
GUI.DrawTexture(new Rect(43,Screen.height - 65,314,40), healthBorder);
int adjust = curhealth*3;
GUI.BeginGroup(new Rect(40,Screen.height - 67,adjust,30));
GUI.DrawTexture(new Rect(0,0,323,46), healthTexture);
GUI.EndGroup();
}
}
Thanks a lot for your help but, what about
var healthTexture : Texture2D;
var healthBorder : Texture2D;
Answer by Kayden007 · Oct 25, 2012 at 08:12 PM
Ok here are the pics
Here with you code I placed the health to 100 alt text
http://answers.unity3d.com/storage/temp/4428-full.jpg
http://answers.unity3d.com/storage/temp/4429-sent+down.jpg
Open images in new tab if too small Other pic, when increased the bar goes down(Couldn't upload since two uploads were the limit.
Got it like 90% working by combining your code and Rati's code. It just won't display the texture due to the health percentage.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public Texture2D healthTexture;
public Texture2D healthBorder;
public int curHealth = 100;
public int maxHealth = 100;
public float healthBarLength;
private float percent;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 4;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI()
{
GUI.DrawTexture(new Rect(43,Screen.height - 65,314,40), healthBorder);
GUI.BeginGroup(new Rect(40,Screen.height - 67,healthBarLength,60));
GUI.DrawTexture(new Rect(31,10,242,15), healthTexture);
GUI.EndGroup();
}
public void AdjustCurrentHealth(int adj){
curHealth += adj;
if(curHealth < 1)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
percent = (curHealth / (float)maxHealth);
healthBarLength = (Screen.width *percent);
}
}
[1]: /storage/temp/4432-magic+full.png
[2]: /storage/temp/4433-magic+empty.png
Textures
What are the dimensions of the textures you are using? Better still, can you share them? Easier for me to try to reproduce the problem that way.
Actually, forget that. Squinting at your screen captures I can see exactly what is happening. It's a misunderstanding about what each parameter is for. So here's a rundown:
maxHealth: the maximum health that the object can have. Generally, this would be some fixed value.
Health: this property is publicly exposed and it is this that you adjust to make an object more or less healthy. Typically another script would access the HealthBar component and set or decrease its value.
In any event, I've added a clamp to the OnGUI code to ensure that the bar can't overspill in the way in which you shown.
Oh sorry about that forgot to switch off my GUITexture object(the green health bar) It's actually the blue one. Uploading the image files(ignore that I named them "magic")
Just tested with your textures and the code I wrote is working as expected. Take another look over the notes I wrote above. If you wanted to experiment in editor with different health values, you could make _currentHealth public temporarily.
Sorry for annoying you with this noobish question but is there a way to resize the texture without re-sizing the border and let the whole health bar size change due to the size of the game window? Thanks again!
Answer by 90DegreeDesigns · Nov 18, 2013 at 03:49 PM
using UnityEngine;
using System.Collections;
public class HEALTHBAR : MonoBehaviour {
Texture2D healthTexture;
Texture2D healthBorder;
int curhealth = 100;
int maxhealth = 100;
void OnGUI (){
GUI.DrawTexture( new Rect(43,Screen.height - 65,314,40), healthBorder);
int adjust = health * 3;
GUI.BeginGroup( new Rect(40,Screen.height - 67,adjust,30));
GUI.DrawTexture( new Rect(0,0,323,46), healthTexture);
GUI.EndGroup();
}
}
Answer by Conect11 · Nov 18, 2013 at 03:56 PM
also, in the future you might find this site helpful: Convert Unityscript to C# The output usually requires a touchup or two, but is generally pretty accurate.
Your answer
Follow this Question
Related Questions
The Game Over creating with audio? 1 Answer
Health Bar Centering 4 Answers
Trubble with my hit counter. 0 Answers
Restart with GUIText 2 Answers