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

来自:网络
时间:2021-07-15
阅读:

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()
{
 
}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

返回顶部
顶部