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来获取

  1. GameObject objcub = GameObject.CreatePrimitive(PrimitiveType.Cube);  
  2. objcub.AddComponent<Rigidbody>();  
  3. objcub.name = "Cube";  
  4. //设置color 使用这个来获取material  
  5. objcub.GetComponent<Renderer>().material.color = Color.blue;  



相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2021-12-12
  • 2021-12-11
  • 2021-08-04
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
相关资源
相似解决方案