Hi readers!
This is the first post tagged with #ft tag, so I will do a little explanation about it before start with the tutorial: This tag will be in tutorials that they explains little tips or fast things, so they will be short and useful! Amazing!
OK! Now, we can start with the tutorial so let's go! How to do a gradient bar? A gradient bar is used in games for score, health... currently I am using the following code in the development of a game, so if you have any problem, post a comment and I will answer you as soon I can.
Our objetive is do this:
- Create a plane and scale it to seem like a progress bar
- Create a new script called GradientBar ,paste the code posted at the end of the post, save the file and add this script to the plane
- Press Play Button to start debug, and press Space Key to show our amazing Gradient Bar!
Here is the code of GradientBar script:
//Code created by @Mr_Nauta and posted in http://codenauta.blogspot.com //If you have any question please ask me in a comment or ask me in my Twitter: @Mr_Nauta //You can use this code in your project but I like that I will appear in credits or thanks :) using UnityEngine; using System.Collections; public class GradientBar : MonoBehaviour { private Texture2D bar_tex;//This is the texture of the progress bar int c=100;//This variable is only for testing, while is pressed space key it will decreased by 1 (see Update() function) void Start () { //Create a new texture (100 width, 1 height) bar_tex = new Texture2D (100, 1); //With these options the texture will be automatically resized without code bar_tex.filterMode = FilterMode.Bilinear; bar_tex.wrapMode = TextureWrapMode.Clamp; //Apply changes in the texture bar_tex.Apply(); //And set this texture as the main texture of our progress bar (a basic plane object) renderer.material.mainTexture = bar_tex; } //This function is only used for testing and can be deleted void Update () { //If is pressed the space key... if (Input.GetKey("space")) { //If c is more than 100... if(c>100) //We set it to 100 c=100; //If c is between 100 and 0... if((c<=100)&&(c>-1)){ //Set pixel at coordinates c,1 (c is the width and 1 the height (height is constant because our texture is only 1 of height)) bar_tex.SetPixel(c,1,GetBlendedColor(c));//GetBlendedColor is our main function and only need the percentage of the progress bar in base 100 like 9% or 21% bar_tex.Apply(); //Apply changes c--;//And decrese by 1 c variable } } } public Color GetBlendedColor(int percentage){ //If percentage is less than 50 if (percentage < 50) { //Call interpolate function between colors green and yellow, and parsing the total percentage of the progress bar to a custom percentage (see our TransfromPercentage function for more info) return Interpolate(Color.green,Color.yellow,TransformPercentage(percentage)); } else { //If is more than 50 call interpolate function with colors yellow and red return Interpolate(Color.red,Color.yellow,TransformPercentage(100-percentage)); } } public float TransformPercentage(int p){ //This function divide our 100 per 100 progress bar in 2 "virtual" progress bar: //-> 1 progress bar of 100 point between 0 and 50 //-> Other progress bar of 100 point between 50 and 100 //And also transform that value in a float value, transforming the value from base 100 to base 1, for example: //In our 100 per 100 progress bar we have this percentage: 23% //Our final value will be: (23/100.0)*2.0 //Other example, in our 100 per 100 progress bar we have this percentage: 86% //Our final value will be: ((100-86)/100.0)*2.0 return (float)((p / 100f) * 2f); } public Color Interpolate(Color c1,Color c2,float p){ //This function use a Unity function called color lerp that need three arguments; //c1 and c2 (interpolated colors) //p The percentage of interpolation in our situation return Color.Lerp(c1,c2,p); } }
If you prefer, I have done a Unity Package with an example scene and this script that you can download in the following link and test it:
Posted by Unknown in ft, tutorial, unity
- Twitter: @Mr_Nauta (or you can follow me clicking the Follow Button and the right of this page)
- Google+: https://plus.google.com/+AlbertoAH
- Facebook: Not yet!
- LinkedIn: Not yet!
- Unity (I will use Unity Pro v.4.5.1f3
- Paper (yes, a normal paper where I will draw and design, but you can use for example Paint)
- Photoshop (or Gimp, for design graphics and textures)
- And the most important thing: a brain
Once you have all of these "tools" we can start to develop this awesome and amazing project :P. So open Unity and create a new project importing the assets that you want to use (at the begining I won't import any asset) and remember to set defaults for 2D!
Now, open Photoshop or Gimp and start to design these textures:
- One texture for the spaceship
- One texture for the common block, for example a grey spaceship
- Seven textures for the special weapons that you can obtain for your spaceship on each level
- One texture for the ball
- Programming scripts
- Programming games (in 2D or 3D for all the platforms that Unity supports) with its graphics, sounds and other stuff...
- Teaching about how to use Unity
- Teaching about how to use whatever Unity asset
- Etc...
If you liked it, please share in Twitter and Facebook!
Posted by Unknown in asset, fiverr, gig, learn, teach, tutorial, unity
It is posible that you updated your Windows 8 to your fantastic new version of Windows (Windows 8.1) and then open Unity to continue working in your game and in that moment...
Crash! Yes, Unity crash and you say: "This should be a problem, but let's go to try again...", then you try the compatibilty mode, but nothing... Unity doesn't want to work!
Okay, and what is the solution? Downgrade my Windows? No! It is not necessary, you only need to follow these three steps and the problem was gone to the past:
- Right-click over the shorcout to Unity and click in Properties
- Search for a textbox which has text like this: "C:\Program Files (x86)\Unity\Editor\Unity.exe"
- Replace that text for that: "C:\Program Files (x86)\Unity\Editor\Unity.exe" -force-d3d11 and close