- Home /
How to make a GUI Button shrink when pressed
Is there any way to make it so when you click the gui button it gets a little smaller and then when you let go it goes back the the original size?
You should run through these tutorials provided by Unity. They are exceptional and will help you jumpstart your UI development!
Answer by Kiwasi · Dec 15, 2014 at 07:13 AM
If you use the UI you can fully animate your transitions. This allows whatever effect you want for mouse down.
Answer by zharik86 · Dec 14, 2014 at 07:58 PM
If you use old GUI, than you need to create style for button. Also create two textures of one size (for example, 64x64), but the picture of the second texture will be a little less than the first as it is necessary for you. Further, in new style you add to the fields "Normal" and "Hover" the first texture, and in the field of "Active" the second texture. Also don't forget to specify your style in the button. I hope that it will help you.
How would I go about this if I'm using Screen.width and Screen.height in my rect?
@shay4545 What difference as you set Rect. So, I will try once again. There are two ways of the solution of your task.
The first - program. For this purpose you have to define, whether your button is press, to do it slightly less. But it is a bad method.
The second - graphic. You draw, for example in Photoshop, texture for the button of 64x64 in size. You save it by the name of NormalTex. Further, in Photpshop, you scale your texture (layer!), doing it slightly less. Also you save by the name of ActiveTex (size picture is 64x64). Further you create GUIStyle in Unity (Right Click On Assets-> Create GUIStyle). In new style you add to the fields "Normal" and "Hover" the NormalTex, and in the field of "Active" the ActiveTex. In program:
public GUIStyle myStyle = null; //reference for your style for button
void OnGUI() {
//Add your style at button
if (GUI.Button(new Rect(0, 0, 200, 100), "btn", myStyle)) {
//Do something
}
}
Answer by ookk47oo · Apr 08, 2019 at 07:54 AM
using System;
using UnityEngine;
using UnityEngine.UI;
namespace GameApp
{
[Serializable]
class ScaleButton : Button
{
[SerializeField]
private float ClickScale = 0.75f;
public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerDown(eventData);
transform.localScale = transform.localScale * ClickScale;
}
public override void OnPointerUp(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerUp(eventData);
transform.localScale = Vector3.one;
}
void OnApplicationFocus(bool isFocus)
{
if (!isFocus)
{
transform.localScale = Vector3.one;
}
}
}
}
Answer by C8H11N02 · Apr 11, 2019 at 12:18 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Buttons : MonoBehaviour {
void OnMouseDown () {
transform.localScale = new Vector3(0.97f, 0.97f, 0.97f);
}
void OnMouseUp () {
transform.localScale = new Vector3(1f, 1f, 1f);
}
}
Your answer
Follow this Question
Related Questions
GUI.Button not showing up 1 Answer
GUI.Toolbar - Custom on/off button states 2 Answers
Create a Button Scrollview 0 Answers