Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yUtopist/94226e2e909f0889aaa5459b46b7f6d9 to your computer and use it in GitHub Desktop.
Save yUtopist/94226e2e909f0889aaa5459b46b7f6d9 to your computer and use it in GitHub Desktop.
// THIS IS EXAMPLE 2
// Resize with MenuSizeChangerCoroutine() coroutine, using Animation Curve Editor
// create UI Panel in Unity Editor with size 2200, 1100, call it "menuInventoryPanel".
// creat script for this UI Panel called "inventoryMenuScript" and copy this code there.
// in Inspector for UI Panel change "Animation Curve" AnimationCurve to 5ft from left, which looks like letter "S", this cuurve called logistic curve.
// create Button in Unity Editor, in "On Click()" editor add new element and atache "menuInventoryPanel" and choose "inventoryMenuScript > InventoryInteraction0" as function.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class inventoryMenuScript : MonoBehaviour
{
public AnimationCurve animationCurve;
public Vector2 desiredMenuSize;
public float lerpTimer = 0f;
public bool inventoryMenuSizeChanger = false;
void Start()
{
this.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
this.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
this.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);
this.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, 0f);
this.GetComponent<RectTransform>().sizeDelta = new Vector2(1760f, 880f);
}
public void InventoryInteraction()
{
lerpTimer = 0; // reset lerpTimer which is referenced as 3rd variable of Lerp
MenuSizeVariable(); // if clicked then change desired deltaSize variable
StartCoroutine(MenuSizeChangerCoroutine(this.GetComponent<RectTransform>().sizeDelta, desiredMenuSize));
}
void MenuSizeVariable() // function to change desired target size on button click
{
if (!inventoryMenuSizeChanger)
{
desiredMenuSize = new Vector2(2200f, 1100f);
}
else
{
desiredMenuSize = new Vector2(1760f, 880f);
}
inventoryMenuSizeChanger = !inventoryMenuSizeChanger;
}
IEnumerator MenuSizeChangerCoroutine(Vector2 sizeCurrent, Vector2 sizeNew) // coroutine to change size of the element with Vector2.Lerpd using animationCurve as 3rd variable
{
float animationCurveValue = animationCurve.Evaluate(lerpTimer);
while (lerpTimer < 1)
{
lerpTimer += Time.deltaTime * 10;
animationCurveValue = animationCurve.Evaluate(lerpTimer);
this.GetComponent<RectTransform>().sizeDelta = Vector2.Lerp(sizeCurrent, sizeNew, animationCurveValue);
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment