> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/overview-branchlogic/ui-triggered-animations.md).

# UI-Triggered Animations

Animate UI Panels, Configure UI-Button Action Listeners

### Example:  Book of Spells

In this example, we have a BookPanel that allows the user to select buttons in the menu to view different text-sections.  We also want this panel to be hidden. \
The tab-key animates the visibility with a slide-in from the left-side canvas border. &#x20;

Step 1:  Create A UI Panel with desired configuration<br>

![UI Panels, Buttons, Text - Vertical Layout  - With Animation](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FS3M5yL9czbxJeZpNWTuQ%2FScreen%20Shot%202022-04-14%20at%2011.33.21%20AM.png?alt=media\&token=18c10395-85b9-46d4-821f-7334c086d80a) ![Inspector Shows Nested UI-Elements](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FGrbdnjpC8DWKxQcI7e8C%2FScreen%20Shot%202022-04-14%20at%2011.38.15%20AM.png?alt=media\&token=0f22f1b8-2974-412d-9d55-8ae51e82a02f)

To create an animated UI element, such as a menu, etc.  You will want all components nested as children within a top-level UI container such as a UI-panel.  Animation scripts will be attached to this top-level panel - the **'BookPanel'** in the example above.

### UI Basic 2 State-Animation: &#x20;

Given a UI Panel such as the one listed above:&#x20;

With the top-level panel selected in the inspector, open the animator window and you are prompted to 'To begin animating BookPanel, create an Animator and an Animation Clip': \
Select the 'Create' button and save as 'BookPanel\_Visible' animation

![Select the Top-Level Panel GameObject when the Animation Window is Visible](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FbmHKlS4sTf2zW0GfoLKA%2FScreen%20Shot%202022-04-15%20at%2012.03.11%20PM.png?alt=media\&token=b2d75361-59d5-45c7-aed2-bef3c2293894) ![Prompt for creating new Animation and Animator Component](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FuQNvbx1OQoYdKmauQ0I6%2FScreen%20Shot%202022-04-15%20at%2012.05.54%20PM.png?alt=media\&token=581f9f35-74f7-48d2-9763-16dc795d7ea1)

Animator Component - Add Bool Parameter using the left side 'Parameters' tab as shown below.

![Animator Component Auto-Generated on BookPanel  - Creating a Bool Parameter](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FHrX6ZmAxrrZxXeyVbJjE%2FScreen%20Shot%202022-04-15%20at%2012.06.46%20PM.png?alt=media\&token=20a99274-101e-4f1e-a149-be3eeaf9a37d) ![Animator on BookPanel](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FHRcDhd5PuV2OlnbEjRqG%2FScreen%20Shot%202022-04-15%20at%2012.13.40%20PM.png?alt=media\&token=5bf474c2-533c-4e02-ab5e-d56a2d77265a)

### Configure Animation Properties

Rect Transform - Position - As shown below - When Selecting the 'Add Property' Button, select the Rect-Transform: Anchored Position using the '+' symbol at the right side of the menu

![Add Anchored Position as a Property](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FtAjdIL2zE7iFJeBPfjta%2FScreen%20Shot%202022-04-15%20at%2012.27.57%20PM.png?alt=media\&token=b756c030-9e16-4011-8aaa-10953a852e8d)

Set the **Anchored Position.x** to have a value that makes the panel visible within the camera viewport. - Remove other Keyframes - only 1 keyframe time-point is required for each animation clip.

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FTm1JcB6mbfDshpLZwziY%2FScreen%20Shot%202022-04-15%20at%2012.32.21%20PM.png?alt=media\&token=f2d83c74-35a9-4647-8493-d86a21c7c07f) ![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FYvtXWxXtR1izb3uj0U8c%2FScreen%20Shot%202022-04-15%20at%2012.34.55%20PM.png?alt=media\&token=fcdcf383-63e9-4561-8a0d-7b387839f725)

### **Create: BookPanel\_InVisible** Animation&#x20;

Selecting 'Create New Clip...'

Set the **Anchored Position.x** to have a value that makes the panel **not visible** within the camera viewport. - Remove other Keyframes - only 1 keyframe time-point is required for each animation clip.

![Set the Anchored Position.x to a value outside the Camera View Port](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2F4aTsJE58IX4Rxbscwl1N%2FScreen%20Shot%202022-04-15%20at%2012.36.56%20PM.png?alt=media\&token=67ec977b-7df1-4d61-b747-e0196be79657)

Setup Animator Controller - Set BookPanel\_InVisible as 'Layer Default State'

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FXs1pR6VvbbDE77y1P0B7%2FScreen%20Shot%202022-04-15%20at%203.15.25%20PM.png?alt=media\&token=65983d3a-cfb9-4db7-ad12-37d332944f58)

![Create Transition Arrows Between States](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FzbgqbDvnvGWay48fvseG%2FScreen%20Shot%202022-04-15%20at%203.17.01%20PM.png?alt=media\&token=3c6fcb63-02f0-46d3-8be0-684fc1d5d75b)

![Bool Parameter:  IsVisible](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FZuFB6hJyippiDwNWjrnI%2FScreen%20Shot%202022-04-15%20at%203.19.40%20PM.png?alt=media\&token=0a3123a1-3d9a-4231-b5e4-ee1b55132da5)

![Set State Transition - Conditional Logic using Parameter: InVisible](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FfafAwKgiat6m1rwRITHr%2FScreen%20Shot%202022-04-15%20at%203.25.42%20PM.png?alt=media\&token=a3b009f6-e270-43b4-8628-ab82c2e59dce)

### Configure Buttons to Execute MagicSpells Methods

![In the Inspector:  Button: Spell1 - Configure OnClick to Execute MagicSpells.Spell1()](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0-KLgOacVpbicrqehO-887967055%2Fuploads%2FmcqIT54ZogVaOsA4y2St%2FScreen%20Shot%202022-04-15%20at%203.33.22%20PM.png?alt=media\&token=18b8886a-1e91-436f-86b7-285f879aa668)

### Script:  MagicSpells.cs:&#x20;

Change Book-Text with Button onClick:

Add this script to the Top-Level Panel - BookPanel

```
uusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MagicSpells : MonoBehaviour
{
    [SerializeField]
    Text spellText;
    Animator animator;

    bool InVisible = true;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();

        spellText.text = "Magic Spells";
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            InVisible = !InVisible;
            animator.SetBool("IsVisible", InVisible);
            Debug.Log("Tab Pressed: isVisible= " + InVisible);
            if (!InVisible) { //reset when it becomes visible
                spellText.text = "Magic Spells";
            }
            
        }
    }

    public void Spell1()
    {
        spellText.text = "Spell 1";
    }

    public void Spell2()
    {
        spellText.text = "Spell 2";
    }

    public void Spell3()
    {
        spellText.text = "Spell 3";
    }

    public void Spell4()
    {
        spellText.text = "Spell 4";
    }

    public void Spell5()
    {
        spellText.text = "Spell 5";
    }
}
```
