Unity 2018 Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How it works...

You have created two UI Panels, each panel contains a background Image of a playing card and a UI Button whose action will make its parent panel move to the front. You set the Alpha (transparency) setting of the background image's Color to 255 (no transparency).

You added an OnClick event action to the button of each UI Panel. The action sends a SetAsLastSibling message to the Button's Panel parent. When the OnClick message is received, the clicked Panel is moved to the bottom (end) of the sequence of GameObjects in the Canvas, and therefore this Panel is drawn last of the Canvas objects, and so appears visually in front of all other GameObjects.

The button's action illustrates how the OnClick function does not have to be the calling of a public method of a scripted component of an object, but it can be sending a message to one of the non-scripted components of the targeted GameObject. In this recipe, we send the SetAsLastSibling message to the Rect Transform of the Panel where the Button is located.