unity button 通过事件改变物体颜色

1,Canvas不能移动,理解为Game窗口。


2, button在UI层,不知为什么它在相机视野里面前后移动时不会远小近大。(有点像小说三体3中云天明给程心讲的故事中那个深水王子的感觉。)

Pos X = 0, Pos Y = 0 表示btton在Canvas中间


通过点击脚本改变颜色的事件设置,button添加上后会自动加一个可以用的事件。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColor : MonoBehaviour {

	public GameObject cube;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void ChangeColorRed()
	{
		cube.GetComponent<MeshRenderer> ().material.color = Color.red;
	}
}

如果不用它自带的可以自己加

a)添加组件event trigger


b) 学button的设置做一些你期望的设置



c) 脚本和其它相关设置也一样,button要拖到事件那儿,脚本要挂在button上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColor : MonoBehaviour {

	public GameObject cube;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void ChangeColorRed()
	{
		cube.GetComponent<MeshRenderer> ().material.color = Color.red;
	}

	public void ChangeColorWhite()
	{
		cube.GetComponent<MeshRenderer> ().material.color = Color.white;
	}
}

运行如你所期。

3 EventSystem是button的事件对应的东西,不能删除,不然会按键的作用和触发事件效果就没了。

4,比较好看的按键

添加图片,修改为2DUI


选复合性裁剪你要的部分,sprite editor修改或查看其属性大小


按钮变化的一些设置,其他的东西查看unity的官方文档。



  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值