亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 256 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號