- Home /
[Solved] How do you Scale 0 to 1 by a button and reverse back when btn is pressed again?
I wanted to make a shop UI . So when I pressed " I " , it should activate shop UI and scale it from 0 to 1. And when I close it, it should scale back down to 0. I searched in youtube and googled it but couldn't find a satisfying one.
Answer by sacredgeometry · Aug 24, 2020 at 12:45 PM
So you could use a boolean to store the state and every time the button click event gets triggered you switch the state of bool i.e. myBool = !myBool
.
This will swap it to true if false and false if true.
From there you can either cast it directly to an integer (which will result in 1 for true and 0 for false), use the bool to determine or set the integer value or simply switch on the bool. It's really up to you.
lol, that's exactly what I did. I actually solved this problem then made this post so that others can later on use it if needed.
Great $$anonymous$$ds think alike. Thanks for accepting the answer.
Answer by Raikir-i-sh · Aug 24, 2020 at 12:47 PM
So after hours I made this code. I hope its helpful for others. You can use this for any other thing not just UI. I am in no way an expert coder so If you have better way of doing this , plz feel free to share.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScalerZeroToOne : MonoBehaviour
{
// Start is called before the first frame update
public RectTransform rect;
public Transform trans;
public GameObject shopUi;
public float speed = 2f;
bool openShop = false;
bool shopHold = false;
float t = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.I)) openShop = !openShop;
ShopUIcheck();
}
private void ShopUIcheck()
{
if (!openShop)
{
if (t <= 0)
{
shopUi.SetActive(false);
t = 0f;
return;
}
t -= speed * Time.deltaTime;
}
else if (openShop && t < 1.0f)
{
t += speed * Time.deltaTime;
shopHold = true;
}
else if (shopHold && openShop)
{
return;
}
print(t);
shopHold = true;
shopUi.SetActive(true);
float scal = Mathf.Lerp(0, 1 , t);
if(rect ==null) { trans.localScale = new Vector3(scal, scal); }
rect.localScale = new Vector3( scal, scal);
}
}
As you used my answer can you mark it as correct please?
Your answer
Follow this Question
Related Questions
Display upscale filtering? 1 Answer
Scaling of instantiated ParticleSystem 0 Answers
trees in terrain sizing 3 Answers
Child object changes scale 1 Answer
how to change pitch of sound without changing speed? wav file 1 Answer