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”
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);
}
}
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();
}
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();
}
}
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.