如何使用unity实现鼠标经过时ui及物体的变色操作

发布时间:2021-04-12 11:40:02 作者:小新
来源:亿速云 阅读:1311

这篇文章主要介绍了如何使用unity实现鼠标经过时ui及物体的变色操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1、实现UI的变色

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

如何使用unity实现鼠标经过时ui及物体的变色操作

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<Renderer>().material.color = Color.blue;
 }
       public void OnMouseEnter()
       {
            objCube.GetComponent<Renderer>().material.color = Color.red;
       }
      public void OnMouseExit()
      {
            objCube.GetComponent<Renderer>().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<Rigidbody>();  
objcub.name = "Cube";  
//设置color 使用这个来获取material  
objcub.GetComponent<Renderer>().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<Animation>()!=null) {
            if ( gameObject.GetComponent<Animation>() .isPlaying) {
                return;
            }
            gameObject.GetComponent<Animation>().wrapMode = WrapMode.Loop;
            gameObject.GetComponent<Animation>().Play();
        }
    }
//鼠标离开时关闭动画
    public void OnPointerExit(PointerEventData eventData)
    {
        if ( gameObject.GetComponent<Animation>() != null )
        {
            if ( gameObject.GetComponent<Animation>().isPlaying )
            {
                gameObject.GetComponent<Animation>().wrapMode = WrapMode.Once;
                return;               
            }
            gameObject.GetComponent<Animation>().Stop();
        }
    }
}

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

定义物体GameObject o;

效果:当人物接近物体时,物体触发动画,比如位移

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<XXX>()
ani = transform.GetChild(0).GetComponent<Animation>();
}
 
private void OnTriggerEnter(Collider other){
//当物体接触到时则播放animation中的km动画
ani.Play("km");
}
 
private void OnTriggerExit(Collider other){
//当物体接触到时则播放animation中的gm动画
ani.Play("gm");
}
 
void Update()
{
 
}
}

感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用unity实现鼠标经过时ui及物体的变色操作”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. unity3D 鼠标滚轮实现物体的大小缩放
  2. CSS鼠标经过时显示

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity

上一篇:C#实现Eval的方法有哪些

下一篇:Unity实现换装系统的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》