Revealing icons for multiple object pickups by changing the size of a tiled image

Another approach that could be taken to show increasing numbers of images is to make use of tiled images. The same visual effect as in the previous recipe can also be achieved by making use of a tiled grey star image of width 400 (showing four copies of the grey star icon), behind a tiled yellow star image, whose width is 100 times the number of stars collected. We'll adapt the previous recipe to illustrate this technique.

Getting ready

This recipe follows on from the previous recipe in this chapter.

How to do it...

To display grey and yellow star icons for multiple object pickups using tiled images, follow these steps:

  1. Make a copy of your work for the previous recipe.
  2. In the Hierarchy panel, remove the four Image-star0/1/2/3 UI Images in the Canvas.
  3. Select the Canvas in the Hierarchy panel and add a new UI Image object (Create | UI | Image). Rename it Image-stars-grey.
  4. Select Image-stars-grey in the Hierarchy panel.
  5. From the Project panel, drag sprite icon_star_grey_100 (folder Sprites) into the Source Image field in the Inspector (in the Image (Script) component).
  6. Click on the Set Native Size button for this for the Image component. This will resize the UI Image to fit the physical pixel width and height of sprite file star_empty_icon.
  7. Now we will position our icon at the top and left of the Game panel. Edit the UI Image's Rect Transform component, and while holding down SHIFT and ALT (to set pivot and position), choose the top-left box. The UI Image should now be positioned at the top left of the Game panel.
  8. In the Inspector panel, change the Width (in the Rect Transform component) of Image-stars-grey to 400. Also, set the Image Type (in the Image (Script) component) to Tiled, as shown in the following screenshot:
    How to do it...
  9. Make a copy of Image-stars-grey in the Hierarchy panel, naming the copy Image-stars-yellow.
  10. With Image-stars-yellow selected in Hierarchy panel, from the Project panel, drag the sprite icon_star_100 (folder Sprites) into the Source Image field in the Inspector (in the Image (Script) component).
  11. Set the width of Image-stars-yellow to 0 (in the Rect Transform component). So, now we have the yellow stars tiled image above the grey tiled image, but since its width is zero, we don't see any of the yellow stars yet.
  12. Replace the existing C# Script PlayerInventoryDisplay with the following code:
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class PlayerInventoryDisplay : MonoBehaviour
    {
      public Image iconStarsYellow;
    
      public void OnChangeStarTotal(int starTotal){
        float newWidth = 100 * starTotal;
        iconStarsYellow.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, newWidth);
      }
    }
  13. From the Hierarchy view, select the GameObject player-SpaceGirl. Then, from the Inspector, access the Player Inventory Display (Script) component and populate the Icons Stars Yellow public field with UI Image object Image-stars-yellow.

How it works...

UI Image Image-stars-grey is a tiled image, wide enough (400px) for grey sprite icon_star_grey_100 to be shown four times. UI Image Image-stars-yellow is a tiled image, above the grey one, initially with width set to zero, so no yellow stars can be seen.

Each time a star is picked up, a call is made to the OnChangeStarTotal(…)method of the script class PlayerInventoryDisplay , passing the new integer number of stars collected. By multiplying this by the width of the yellow sprite image (100px), we get the correct width to set for UI Image Image-stars-yellow so that the corresponding number of yellow stars will now be seen by the user. Any stars that remain to be collected will still be seen as the grey stars that are not yet covered up.

The actual task of changing the width of UI Image Image-stars-yellow is completed by calling the SetSizeWithCurrentAnchors(…) method. The first parameter is the axis, so we pass constant RectTransform.Axis.Horizontal so that it will be the width that is changed. The second parameter is the new size for that axis—so we pass a value that is 100 times the number of stars collected so far (variable newWidth).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.131.255