# Message Popups

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2Fh3yyP7xsXfudGoXrTyMU%2FMessage%20Popups.unitypackage?alt=media&token=eecef5fe-a6dc-4d3c-be85-ebc097b5b16e>" %}
Package with DOTween support
{% endfile %}

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FDujZ8PuveBQw5mdrGWII%2FMessage%20Popups%20(PrimeTween).unitypackage?alt=media&token=20ab8dab-955d-43c2-a37e-b043abf3346b>" %}
Package with PrimeTween support
{% endfile %}

## What for

When player clicks on locked button or we want to send player tips it's good to make it with message popups. Message will be shown for N seconds (+-2s by default) and then automaticaly disappear.

Here is examples of default popups (you can change its visuals with prefabs), 3 types:

<div><figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FuPzT1uulQv4okjuxe8tz%2Ftip.gif?alt=media&#x26;token=9e471bc2-3f52-4558-83fc-e103ab73688a" alt=""><figcaption><p>Tip (pool of 3 in action)</p></figcaption></figure> <figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FwWdkSsN3NczbLJXVM8GO%2Ferror.gif?alt=media&#x26;token=b288e91b-41fc-4dde-b363-0515d10e201a" alt=""><figcaption><p>Error</p></figcaption></figure> <figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FYr51kQRTuZJhwVQXYNJu%2Finfo.gif?alt=media&#x26;token=32775d8e-4b84-4105-afb2-8cda54a660ad" alt=""><figcaption><p>Info</p></figcaption></figure></div>

## How to use

{% hint style="danger" %} <mark style="color:red;">**It works only in canvas with screen space mode =**</mark><mark style="color:red;">**&#x20;**</mark>*<mark style="color:red;">**Overlay**</mark>*\
\&#xNAN;*<mark style="color:green;">Create separate canvas with this mode if you dont have one.</mark>*
{% endhint %}

1. Add MessagePopups.prefab under your scene UI canvas
2. Reference to MessagePopups somewhere to call message popups later
3. Use these methods in MessagePopups.cs to call popup:

```csharp
public void Show(string text, MessageTypes messageType);
public void Show(string text, MessageTypes messageType, Vector2 atScreenPosition);
```

{% hint style="info" %}
You also can use MessagePopup.cs directly. Add target type prefab to your UI and call by controller with Show method, but make sure to call `Init()` once before calling `Setup(text)` + `Show(text)`
{% endhint %}

### Settings of base implementation

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FbbpBOyTgbfwVrZjtINHx%2Fimage.png?alt=media&#x26;token=a575bc85-859a-4fc8-be50-6597e76c65b8" alt=""><figcaption><p>In inspector BaseMessage.prefab</p></figcaption></figure>

* Fit In Screen boolean used by MessagePopups manager to fit popup in user resolution even if target position out of screen (rect size delta of message will be used)
* Fit Screen Padding is offset from screen border to fit in
* Text is reference to default text (replace it with TMP if you need in MessagePopup.cs)
* Animation is implementation of PopupAnimation abstract class. PopupDefaultAnimation is implemented in this package, you can add your own and setup correspond fields

### How add more popups:

1. Add new popup type in enum MessageTypes.cs
2. Create prefab variant from BaseMessage.prefab or completely new and add MessagePopup script to it
3. Setup visuals for it in prefab
4. *Optional* if you need special animation for this type then make a class that derives from **PopupAnimation** and implement it's methods. See PopupDefaultAnimation implementation for reference how it can be. Your PopupAnimation implementation should support replacement in target screen position (I set rectTransform.position of message with argument value and recalculate animation offsets)
