Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. public static Sprite Generate_Healthbar(int width, int height, Color color, int totalHealth)
  2.         {
  3.             print(totalHealth);
  4.  
  5.             // Arrendontar valor.
  6.             float a = totalHealth / 100f;
  7.             int b = (int) ((a - (int)a) * 100);
  8.  
  9.             // Possui quebrados.
  10.             if (b > 0)
  11.             {
  12.                 // Maior que 50 arredonda pra mais.
  13.                 if (b > 50)
  14.                 {
  15.                     totalHealth -= (b - 100);
  16.                 }
  17.                 // Caso contrário arrendonda pra menos.
  18.                 else
  19.                 {
  20.                     totalHealth  -= b;
  21.                 }
  22.             }
  23.  
  24.  
  25.             // Criar Textura.
  26.             Texture2D geratedTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
  27.  
  28.             geratedTexture.wrapMode = TextureWrapMode.Clamp;
  29.             geratedTexture.filterMode = FilterMode.Point;
  30.  
  31.             // Numero de divisões da barra.
  32.             float partsNum = totalHealth / 100;
  33.  
  34.             // Tamanho das divisões da barra em pixels.
  35.             float partsSize = width / partsNum;
  36.  
  37.             // Gerar degrade.
  38.             for (int w = 0; w <= width; w++)
  39.             {
  40.                 for (int h = 0; h < height; h++)
  41.                 {
  42.                     float g = ((float) ((height - h) - 1) / height) / 2.5f;
  43.                     geratedTexture.SetPixel(w, h, (color - new Color(g, g, g, 0)));
  44.                 }
  45.             }
  46.  
  47.             // Dividir a barra.
  48.             for (int w = 1; w < geratedTexture.width; w++)
  49.             {
  50.                 for(int p = 0; p < partsNum; p++)
  51.                 {
  52.                     if (w == (int)(p * partsSize))
  53.                     {
  54.                         for (int h = 0; h < height; h++)
  55.                         {
  56.                             // Verficar se esta divisões marca uma margem de 1000hp.
  57.                             if (p % 10 == 0)
  58.                             {
  59.                                 // Dividir a barra de cima a baixo.
  60.                                 geratedTexture.SetPixel(w, h, new Color32(0, 0, 0, 0));
  61.                             }
  62.                             else
  63.                             {
  64.                                 // Divir a barra de cima a baixo até a metade.
  65.                                 geratedTexture.SetPixel(w, Mathf.Max(h, height / 2), new Color32(0, 0, 0, 0));
  66.                             }
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.             geratedTexture.Apply();
  72.            
  73.             // Aplicar a textura a um novo sprite.
  74.             Sprite geratedSprite = Sprite.Create(geratedTexture, new Rect(0, 0, width, height), Vector2.one / 2);
  75.  
  76.             return geratedSprite;
  77.         }
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement