unity 点击改变按钮图片

好吧,我已经知道 Unity 本身具有 Sprite Swap 功能,但我希望它在我更改图像时单击并仅在它关闭打开的窗口时返回旧的,或者如果我再次单击它图标,我尝试了几种方法,所以我求助于



慕斯王
浏览 225回答 1
1回答

翻过高山走不出你

视频链接:-&nbsp;https://youtu.be/-H7j3vdKl8A。如果您在这里不理解,请访问 github。&nbsp;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.********************************//&nbsp;&nbsp;/*&nbsp;* Steps to be followed to make this script work&nbsp;* Select All the Category Buttons (for example:- Suppose u r builiding a shop UI then Select all Category button Like Helics, Weapons&nbsp; -- refer to video link above;&nbsp;* Now tag all these category button as same (say UI_CategoryButtons).&nbsp;* Attach this script to all the UI_category button.&nbsp;* the script will aotomatically add Button, Animator, Image component to the Ui Gameobjects.&nbsp;* now select Animation in Transition tab.&nbsp;* now press the Auto genrate Animation in Button component tab.&nbsp;* now, set Transition from Animation to none in button Tab.&nbsp;* 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)&nbsp;* open Animation window and Animate ur Ui elements.&nbsp;* ur good to go now.&nbsp;*/[RequireComponent(typeof(Button))][RequireComponent(typeof(Animator))][RequireComponent(typeof(Image))]public class ButtonAnimationHold : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler{&nbsp; &nbsp; public float TransitionTime = 0.3f;&nbsp; //Time taken to transition from one animation state to anathor.&nbsp; &nbsp; private Animator MyAnim;&nbsp; &nbsp; &nbsp; //reference to animator attacted to the UI gameObject.&nbsp; &nbsp; private void Start()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MyAnim = GetComponent<Animator>();&nbsp; &nbsp; &nbsp; &nbsp; PlayNoramlState();&nbsp; &nbsp; &nbsp; //Playing normal state at beginning.&nbsp; &nbsp; }&nbsp; &nbsp; public void HighlightThisButton_UnhighlightRest()&nbsp; &nbsp; //called when button is pressed (Called by OnPointerClick() defined below)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string TagOfPressedButton = gameObject.tag;&nbsp; //the tag of the button pressed by user.&nbsp; &nbsp; &nbsp; &nbsp; foreach (Button button in gameObject.GetComponentInParent<Transform>().root.gameObject.GetComponentsInChildren<Button>())&nbsp; &nbsp;//searching the gameobject with tag as in TagOfPressedButton inside the canvas which contain all UI element.&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//we search inside Canvas to improve perf.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(button.gameObject.tag == TagOfPressedButton && button.gameObject != this.gameObject)&nbsp; &nbsp; &nbsp;//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.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.gameObject.GetComponent<ButtonAnimationHold>().PlayNoramlState();&nbsp; &nbsp; &nbsp; &nbsp;//Playing normal state in all UI gameObject with same tag except the one which called it.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; MyAnim.Play("Pressed");&nbsp; //Playing Pressed state in the UI element that was pressed.&nbsp; &nbsp; }&nbsp; &nbsp; public void PlayNoramlState()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MyAnim.CrossFadeInFixedTime("Normal", TransitionTime);&nbsp; &nbsp; &nbsp; //smoothly transitioning to Normal state;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public void OnPointerEnter(PointerEventData eventData)&nbsp; &nbsp; &nbsp; //Called when pointer hover over the Ui elemnt;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Normal"))&nbsp; &nbsp; &nbsp; //Playing highlighted state only when it is in normal state and not when it is in Pressed state.&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyAnim.CrossFadeInFixedTime("Highlighted", TransitionTime);&nbsp; &nbsp; //smoothly transitioning to Highlighted state;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void OnPointerExit(PointerEventData eventData)&nbsp;&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Highlighted") || MyAnim.GetCurrentAnimatorStateInfo(0).IsName("Normal"))&nbsp; //Playing Normal state only when it is in Highlighted state and not when it is in Pressed state&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyAnim.CrossFadeInFixedTime("Normal", TransitionTime);&nbsp; //smoothly transitioning to Normal state;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void OnPointerClick(PointerEventData eventData)&nbsp; &nbsp;//called when button is pressed that is pointer down and pointer up both have to be done over button&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HighlightThisButton_UnhighlightRest();&nbsp; &nbsp; }}//video link&nbsp;//https://youtu.be/-H7j3vdKl8A
打开App,查看更多内容
随时随地看视频慕课网APP