Posts

Showing posts with the label ui

Changing Image in unity by C# script

 There are two case when you want to change image one is UI image and other is a sprite in scene so in this post we will take a look at both cases how to change images. Changing UI Image There are very few steps to change UI image scripts First create a UI image gameObject in scene whose image we want to change create a script and attach with an other empty gameObject and open script In script declare a public GameObject for referencing changing image and a declare a private Image variable for refencing Image component in this gameObject also declare a public Sprite for which image that we want in replacement. using   System . Collections ; using   System . Collections . Generic ; using   UnityEngine ; using   UnityEngine . UI ; public   class   test  :  MonoBehaviour {      public   GameObject  image_object;      public   Sprite  mySprite;      private   Ima...

how to pause game by script in unity ( with android back button or with computer esc key )

Image
In this post we will create a simple ui button to pause game and show paused UI and other button to resume game so let's get started. Create a Button UI gameObject and named as pause button. (you can name whatever you want).change its text component to pause. if you want to show a (  ||    )  icon to pause game then delete text child object of pause button and then drag your icon in source image of Image component in inspection window. Then create a Panel UI GameObject named as pause menu UI and create a child text ui gameObject (write paused it it text value and place it at top of pause menu panel) and one child button UI gameObject placed in the middle of pause menu panel and named as resume(Change button to resume). Make sure both text and button are child of pause menu UI. this is a simplest UI. You can design your own according to your need. You can add many UI buttons , UI Image, and text in pause menu ui panel and design in your own way like replay, get free ...

Creating count down timer in unity by c# script

Image
 In many cases in game we have to show count down timer. in this post i will explain how to create a timer which show a timer 3... 2.... 1.....  . so first create a simple basic gameObjects :- 1. Create a new scene in your unity project. 2. Create a UI text GameObject in your scene. as I have created and named as "timer text". and set text size, position, font, color of text etc according to you need. 3. Create a new C# script. I named it as "timer". always take care that your C# script name and class name should be same. 4. Create an other empty GameObject. I named it as "timer script". and attach C# timer script with it. 5. Now open timer C# script. Add UnityEngine.UI namespace so that we can use UI component and declare a text and float of name "timer_txt" and "t". You can use any name whatever you want. I just wrote these names so that you can understand which name i have used. 6.drag and drop timer text ui gameObject into it.   the...