Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Sprite Generate_Healthbar(int width, int height, Color color, int totalHealth)
- {
- print(totalHealth);
- // Arrendontar valor.
- float a = totalHealth / 100f;
- int b = (int) ((a - (int)a) * 100);
- // Possui quebrados.
- if (b > 0)
- {
- // Maior que 50 arredonda pra mais.
- if (b > 50)
- {
- totalHealth -= (b - 100);
- }
- // Caso contrário arrendonda pra menos.
- else
- {
- totalHealth -= b;
- }
- }
- // Criar Textura.
- Texture2D geratedTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
- geratedTexture.wrapMode = TextureWrapMode.Clamp;
- geratedTexture.filterMode = FilterMode.Point;
- // Numero de divisões da barra.
- float partsNum = totalHealth / 100;
- // Tamanho das divisões da barra em pixels.
- float partsSize = width / partsNum;
- // Gerar degrade.
- for (int w = 0; w <= width; w++)
- {
- for (int h = 0; h < height; h++)
- {
- float g = ((float) ((height - h) - 1) / height) / 2.5f;
- geratedTexture.SetPixel(w, h, (color - new Color(g, g, g, 0)));
- }
- }
- // Dividir a barra.
- for (int w = 1; w < geratedTexture.width; w++)
- {
- for(int p = 0; p < partsNum; p++)
- {
- if (w == (int)(p * partsSize))
- {
- for (int h = 0; h < height; h++)
- {
- // Verficar se esta divisões marca uma margem de 1000hp.
- if (p % 10 == 0)
- {
- // Dividir a barra de cima a baixo.
- geratedTexture.SetPixel(w, h, new Color32(0, 0, 0, 0));
- }
- else
- {
- // Divir a barra de cima a baixo até a metade.
- geratedTexture.SetPixel(w, Mathf.Max(h, height / 2), new Color32(0, 0, 0, 0));
- }
- }
- }
- }
- }
- geratedTexture.Apply();
- // Aplicar a textura a um novo sprite.
- Sprite geratedSprite = Sprite.Create(geratedTexture, new Rect(0, 0, width, height), Vector2.one / 2);
- return geratedSprite;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement