为了账号安全,请及时绑定邮箱和手机立即绑定

unity 点击改变按钮图片

unity 点击改变按钮图片

C#
慕斯王 2022-10-15 15:08:18
好吧,我已经知道 Unity 本身具有 Sprite Swap 功能,但我希望它在我更改图像时单击并仅在它关闭打开的窗口时返回旧的,或者如果我再次单击它图标,我尝试了几种方法,所以我求助于
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

视频链接:- https://youtu.be/-H7j3vdKl8A

如果您在这里不理解,请访问 github。 https://github.com/NareshBisht/Unity-UI-Shop-Select-2-UI-object-at-same-time/blob/master/ButtonAnimationHold.cs

谢谢你。

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.EventSystems;


//*************************Script to select one "Category(say Weapon)" UI Button till anathor category button(say Vehicles) is pressed.********************************//  


/*

 * Steps to be followed to make this script work

 * Select All the Category Buttons (for example:- Suppose u r builiding a shop UI then Select all Category button Like Helics, Weapons  -- refer to video link above;

 * Now tag all these category button as same (say UI_CategoryButtons).

 * Attach this script to all the UI_category button.

 * the script will aotomatically add Button, Animator, Image component to the Ui Gameobjects.

 * now select Animation in Transition tab.

 * now press the Auto genrate Animation in Button component tab.

 * now, set Transition from Animation to none in button Tab.

 * Now, Do the above step for Sub category UI element too (for eg do for Apache, Tiger, f-22 etc which is the sub category of Helics --- refer video)

 * open Animation window and Animate ur Ui elements.

 * ur good to go now.

 */


[RequireComponent(typeof(Button))]

[RequireComponent(typeof(Animator))]

[RequireComponent(typeof(Image))]

public class ButtonAnimationHold : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler

{

    public float TransitionTime = 0.3f;  //Time taken to transition from one animation state to anathor.


    private Animator MyAnim;      //reference to animator attacted to the UI gameObject.


    private void Start()

    {

        MyAnim = GetComponent<Animator>();

        PlayNoramlState();      //Playing normal state at beginning.

    }


    public void HighlightThisButton_UnhighlightRest()    //called when button is pressed (Called by OnPointerClick() defined below)

    {

        string TagOfPressedButton = gameObject.tag;  //the tag of the button pressed by user.


        foreach (Button button in gameObject.GetComponentInParent<Transform>().root.gameObject.GetComponentsInChildren<Button>())   //searching the gameobject with tag as in TagOfPressedButton inside the canvas which contain all UI element.

        {                                                                                                                           //we search inside Canvas to improve perf.

            if(button.gameObject.tag == TagOfPressedButton && button.gameObject != this.gameObject)     //selecting gameobjects with same tag(tag of button which was pressed) execpt the one which was pressed and then playing normal state in all selected GO.  

            {

                button.gameObject.GetComponent<ButtonAnimationHold>().PlayNoramlState();       //Playing normal state in all UI gameObject with same tag except the one which called it.

            }

        }


        MyAnim.Play("Pressed");  //Playing Pressed state in the UI element that was pressed.


    }


    public void PlayNoramlState()

    {

        MyAnim.CrossFadeInFixedTime("Normal", TransitionTime);      //smoothly transitioning to Normal state; 

    }


    public void OnPointerEnter(PointerEventData eventData)      //Called when pointer hover over the Ui elemnt;

    {

        if(MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Normal"))      //Playing highlighted state only when it is in normal state and not when it is in Pressed state.

        {

            MyAnim.CrossFadeInFixedTime("Highlighted", TransitionTime);    //smoothly transitioning to Highlighted state; 

        }

    }



    public void OnPointerExit(PointerEventData eventData)  

    {

        if (MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Highlighted") || MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Normal"))  //Playing Normal state only when it is in Highlighted state and not when it is in Pressed state

        {

            MyAnim.CrossFadeInFixedTime("Normal", TransitionTime);  //smoothly transitioning to Normal state; 

        }

    }


    public void OnPointerClick(PointerEventData eventData)   //called when button is pressed that is pointer down and pointer up both have to be done over button

    {

        HighlightThisButton_UnhighlightRest();

    }

}



//video link 

//https://youtu.be/-H7j3vdKl8A


查看完整回答
反对 回复 2022-10-15
  • 1 回答
  • 0 关注
  • 224 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号