Skip to content

Instantly share code, notes, and snippets.

@yUtopist
Last active February 10, 2019 10:07
Show Gist options
  • Save yUtopist/c71989df423272ac3c402fefb9ae22fa to your computer and use it in GitHub Desktop.
Save yUtopist/c71989df423272ac3c402fefb9ae22fa to your computer and use it in GitHub Desktop.
// THIS IS EXAMPLE 1
// Resize with MenuSizeChangerFunction() function in Update();
// 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.
// create Button in Unity Editor, in "On Click()" editor add new element and atache "menuInventoryPanel" and choose "inventoryMenuScript > InventoryInteraction0" as function.
using UnityEngine;
using UnityEngine.UI;
public class inventoryMenuScript : MonoBehaviour
{
public Vector2 desiredMenuSize;
public float lerpTimer = 0f;
public bool lerpTimerChecker = false;
public bool inventoryMenuSizeChanger = false;
public bool inventoryMenuResizingInProgress = 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);
}
void Update()
{
if (inventoryMenuResizingInProgress) // if true then resize this element
{
MenuSizeChangerFunction(this.GetComponent<RectTransform>().sizeDelta, desiredMenuSize);
}
}
public void InventoryInteraction()
{
lerpTimer = 0; // reset lerpTimer which is referenced as 3rd variable of Lerp
MenuSizeVariable(); // if clicked then change desired deltaSize variable
inventoryMenuResizingInProgress = true; // if clicked then start process of resizing at Update();
}
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;
}
void MenuSizeChangerFunction(Vector2 sizeCurrent, Vector2 sizeNew) // function to change size of the element with Vector2.Lerpd
{
if (lerpTimer < 1)
{
lerpTimer += Time.deltaTime;
this.GetComponent<RectTransform>().sizeDelta = Vector2.Lerp(sizeCurrent, sizeNew, lerpTimer);
}
else
{
inventoryMenuResizingInProgress = false;
lerpTimer = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment