赞
踩
程序实现效果:
using UnityEngine.EventSystems; void Update() { //鼠标按下的时候认为是退出状态 if (Input.GetKey(KeyCode.Mouse0) || Input.GetKey(KeyCode.Mouse1)) { //除了leftAlt键盘之外 if (Input.GetKey(KeyCode.LeftControl)) { return; } mouse_Hover_Exit(null); } } void Start() { WindowCtrl_Click(); } private void WindowCtrl_Click() { //添加鼠标悬浮事件 EventTrigger trigger = GetComponent<EventTrigger>(); if (null == trigger) { trigger = gameObject.AddComponent<EventTrigger>(); } //鼠标悬停进入 EventTrigger.Entry hoverOnEntry = new EventTrigger.Entry(); //如果时鼠标悬浮进入 hoverOnEntry.eventID = EventTriggerType.PointerEnter; hoverOnEntry.callback.AddListener((data) => { mouse_Hover_On((PointerEventData)data); }); trigger.triggers.Add(hoverOnEntry); //鼠标悬浮退出 EventTrigger.Entry hoverExitentry = new EventTrigger.Entry(); //如果时鼠标悬浮退出 hoverExitentry.eventID = EventTriggerType.PointerExit; hoverExitentry.callback.AddListener((data) => { mouse_Hover_Exit((PointerEventData)data); }); trigger.triggers.Add(hoverExitentry); //鼠标悬浮退出 EventTrigger.Entry pressEntry = new EventTrigger.Entry(); //如果时鼠标悬浮进入 pressEntry.eventID = EventTriggerType.PointerDown; pressEntry.callback.AddListener(delegate { mouse_Press_Down(); }); trigger.triggers.Add(pressEntry); } private void mouse_Press_Down() { } /// <summary> /// 鼠标悬停退出 /// </summary> /// <param name="data"></param> private void mouse_Hover_Exit(PointerEventData data) { this.gameObject.GetComponent<DOTweenAnimation>().DOPlayBackwards(); this.gameObject.GetComponent<ImageFlashEffect_Drone>().UpdateParam(false); } /// <summary> /// 鼠标悬停进入 /// </summary> /// <param name="data"></param> private void mouse_Hover_On(PointerEventData data) { this.gameObject.GetComponent<DOTweenAnimation>().DOPlayForward(); this.gameObject.GetComponent<ImageFlashEffect_Drone>().UpdateParam(true); }
解释一下: 本人写的是当鼠标悬停在该物体上时正序播放动画,离开时则倒序播放。可以根据需求修改mouse_Hover_Exit,mouse_Hover_On,mouse_Press_Down方法即可。
本作品由A_AiTing采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。