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

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

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

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

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

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

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

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;
	}
}
unity button 通过事件改变物体颜色_unity_04

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

a)添加组件event trigger

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

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

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

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

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

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

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

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

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

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