服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - unity实现鼠标经过时ui及物体的变色操作

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

2022-11-11 14:22qq_41168330 C#

这篇文章主要介绍了unity实现鼠标经过时ui及物体的变色操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1、实现UI的变色

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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来获取

?
1
2
3
4
5
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、给需要有动画的物体添加脚本。脚本如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.人物控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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()
{
 
}
}

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

原文链接:https://blog.csdn.net/qq_41168330/article/details/79704448

延伸 · 阅读

精彩推荐
  • C#轻松学习C#的读写操作

    轻松学习C#的读写操作

    轻松学习C#的读写操作,小编也是第一次接触C#的读写操作,感兴趣的小伙伴们可以参考一下,大家一起学习。...

    丿木呈广予口贝8742021-11-03
  • C#C#如何自定义线性节点链表集合

    C#如何自定义线性节点链表集合

    C#如何自定义线性节点链表集合,这篇文章主要为大家详细介绍了C#基于泛型的自定义线性节点链表集合示例,具有一定的参考价值,感兴趣的小伙伴们可以...

    cnc9782022-01-17
  • C#C#如何遍历Dictionary

    C#如何遍历Dictionary

    这篇文章主要为大家详细介绍了C#遍历Dictionary的方法,.NET中的Dictionary是键/值对的集合,使用起来比较方便,Dictionary也可以用KeyValuePair来迭代遍历,感兴趣...

    一个人的长征10512021-11-19
  • C#详解C#对XML、JSON等格式的解析

    详解C#对XML、JSON等格式的解析

    这篇文章主要介绍了详解C#对XML、JSON等格式的解析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。...

    极简10012021-12-13
  • C#C# 设计模式系列教程-装饰模式

    C# 设计模式系列教程-装饰模式

    每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。它是由Decorator的SetComponent方法来实现的,因而它们的职责是单一的。...

    Wang Juqiang7872021-11-23
  • C#C# 使用Proxy代理请求资源的方法步骤

    C# 使用Proxy代理请求资源的方法步骤

    这篇文章主要介绍了C# 使用Proxy代理请求资源的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    BUTTERAPPLE3772022-07-14
  • C#C# 实例化接口对象的方法

    C# 实例化接口对象的方法

    下面小编就为大家带来一篇C# 实例化接口对象的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网11642021-12-27
  • C#C#中的自动类型转换和强制类型转换

    C#中的自动类型转换和强制类型转换

    这篇文章主要介绍了C#中的自动类型转换和强制类型转换,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下...

    怡红公子05264652022-08-03