Unity实现鼠标滑过UI时触发动画的变色设置及解决方案

Unity实现鼠标滑过UI时触发动画的变色设置及解决方案

1、实现UI的变色

设置Highlighted Color为鼠标经过时变的颜色(Normal为常态,Pressed为按下时的颜色,Disabled为禁止的颜色)

unity怎么按乘以_unity鼠标长按事件_unity按钮鼠标移入事件

2、通过代码实现物体的颜色改变

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cube_change : MonoBehaviour 
{    private Color CubeColor;
    private Texture CubeTexture;
    public GameObject objCube;
 // Use this for initialization
 void Start () 
       {             objCube = GameObject.Find("Cube");
             objCube.GetComponent().material.color = Color.blue;
 }
       public void OnMouseEnter()
       {
            objCube.GetComponent().material.color = Color.red;
       }
      public void OnMouseExit()
      {
            objCube.GetComponent().material.color = Color.blue;
       }
       // Update is called once per frame
      void Update ()
      { 
 }

//+++++++++++++++++++++++++++

unity5.0之后renderer就不能使用material,需要使用GetComponent来获取

GameObject objcub = GameObject.CreatePrimitive(PrimitiveType.Cube);  
objcub.AddComponent();  
objcub.name = "Cube";  
//设置color 使用这个来获取material  
objcub.GetComponent().material.color = Color.blue;  

补充:Unity 实现鼠标滑过UI时触发动画

在有些需求中会遇到,当鼠标滑过某个UI物体上方时,为了提醒用户该物体是可以交互时游戏图片素材,我们需要添加一个动效和提示音。这样可以提高产品的体验感。

解决方案

1、给需要有动画的物体制作相应的Animation动画。(相同动效可以使用同一动画复用)

2、给需要有动画的物体添加脚本。脚本如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
    //鼠标进入按钮触发音效和动画
    public void OnPointerEnter(PointerEventData eventData)
    {
      //  AudioManager.audioManager.PlayEnterAudio();//这里可以将播放触发提示音放在这里,没有可以提示音可以将该行注释掉
        if (gameObject.GetComponent()!=null) {
            if ( gameObject.GetComponent() .isPlaying) {
                return;
            }
            gameObject.GetComponent().wrapMode = WrapMode.Loop;
            gameObject.GetComponent().Play();
        }
    }
//鼠标离开时关闭动画
    public void OnPointerExit(PointerEventData eventData)
    {
        if ( gameObject.GetComponent() != null )
        {
            if ( gameObject.GetComponent().isPlaying )
            {
                gameObject.GetComponent().wrapMode = WrapMode.Once;
                return;               
            }
            gameObject.GetComponent().Stop();
        }
    }
}

补充:unity人物接近时触发事件或动画demo

定义物体GameObject o;

效果:当人物接近物体时unity鼠标长按事件,物体触发动画,比如位移

1.创建o的动画km和gm

2.创建空物体 Empty,大小稍微比o大一点,拖入o,用来接受触发判定,防止物体移动过后触发器跟着移动,勾选 is trigger

2.人物控制器

using System.Collections;
using System.Collections.Generic;
using UnityEngine; 
public class DoorController : MonoBehaviour
{
private Animation ani;
 
void Start() {
//获取子组件下的第一个组件,再获取子组件animation,
//如果是获取自身组件,直接GetComponent()
ani = transform.GetChild(0).GetComponent();
}
 
private void OnTriggerEnter(Collider other){
//当物体接触到时则播放animation中的km动画
ani.Play("km");
}
 
private void OnTriggerExit(Collider other){
//当物体接触到时则播放animation中的gm动画
ani.Play("gm");
}
 
void Update()
{
 
}
}

以上为个人经验材质材料,希望能给大家一个参考unity鼠标长按事件,也希望大家多多支持鸿网互联。如有错误或未考虑完全的地方,望不吝赐教。

文章来源:https://mnews.68idc.cn/makewebs/ASPNET/20210922704887.html