如何在Unity中单击时更改游戏对象的颜色?
How to Change Color of Game Object When Clicked in Unity?
我正在尝试创建棋盘游戏 Hex。底部的玩家一是黄色的,顶部的玩家二是蓝色的。当玩家一点击一个六角形时,它应该变成黄色,而当玩家二点击一个六角形时,它应该变成蓝色。
我使用预制件创建了这个 Hex Map,现在我希望能够在单击它时更改每个图块的颜色(您看到的黄色 hexes 将是透明的,但我导入的 sprite 是黄色的这就是为什么 Sprite Renderer 上的颜色是白色的,即使六边形看起来是黄色的)。
顺便说一句,现在更改 Sprite 渲染器中的颜色会更改所有六边形的颜色。
我按照 quill18creates 的教程制作了六边形地图,只是我是在 2D 而不是 3D 中制作的。
https://www.youtube.com/watch?v=j-rCuN7uMR8
截至撰写本文时,我的颜色更改脚本根本不起作用。我试图拥有它,所以当它收到一次点击时,它会变成黄色。然后下一步点击蓝色,点击黄色,等等。因为每个玩家只能点击一次。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorChange : MonoBehaviour {
public Color[]colors; // allows input of material colors in a set sized array
public SpriteRenderer rend; // what are we rendering? the hex
private int index = 1; //initialize at 1, otherwise you have to press the ball twice to change color
// Use this for initialization
void Start () {
rend = GetComponent<SpriteRenderer> (); // gives functionality for the renderer
}
// Update is called once per frame
void onMouseDown () {
// if there are no colors present nothing happens
if (colors.Length == 0){
return;
}
if (Input.GetMouseButtonDown(0)){
index += 1; // when mouse is pressed down we increment up to the next index location
// when it reaches the end of the colors it stars over
if (index == colors.Length +1){
index = 1;
}
print (index); // used for debugging
rend.color = colors [index - 1]; // this sets the material color values inside the index
}
} //onMouseDown
}
我应该如何实施呢?任何帮助将不胜感激!
为您的预制件添加对撞机和刚体
然后在 OnMouseDown 中大写 O 并删除 "if (Input.GetMouseButtonDown(0)){"
Input.GetMouseButtonDown(0) return 当玩家点击任何你不想要的地方时为真
public Color[] colors; // allows input of material colors in a set sized array
public SpriteRenderer rend; // what are we rendering? the hex
private int index = 1; //initialize at 1, otherwise you have to press the ball twice to change color
// Use this for initialization
void Start()
{
rend = GetComponent<SpriteRenderer>(); // gives functionality for the renderer
}
// Update is called once per frame
void OnMouseDown()
{
// if there are no colors present nothing happens
if (colors.Length == 0)
{
return;
}
index += 1; // when mouse is pressed down we increment up to the next index location
// when it reaches the end of the colors it stars over
if (index == colors.Length + 1)
{
index = 1;
}
print(index); // used for debugging
rend.color = colors[index - 1]; // this sets the material color values inside the index
} //onMouseDown
首先,您需要正确地大写 OnMouseDown()
才能被调用。
之后,由于您使用的是 SpriteRenderer
,因此您需要添加一个碰撞器来检测鼠标点击事件。如 OnMouseDown()
文档中所述:
This event is sent to all scripts of the Collider or GUIElement.
因为你两者都没有,在你的预制件上,点击 Add Component > Polygon Collider 2D 它会自动为你的精灵创建合适的几何体(假设六边形之外的所有东西是透明的)。
最后,取消对 Input.GetMouseButtonDown(0)
的检查。 OnMouseDown 已经捕获了鼠标被单击的事实,并且特定实例 运行 OnMouseDown()
是被单击的实例。
我正在尝试创建棋盘游戏 Hex。底部的玩家一是黄色的,顶部的玩家二是蓝色的。当玩家一点击一个六角形时,它应该变成黄色,而当玩家二点击一个六角形时,它应该变成蓝色。
我使用预制件创建了这个 Hex Map,现在我希望能够在单击它时更改每个图块的颜色(您看到的黄色 hexes 将是透明的,但我导入的 sprite 是黄色的这就是为什么 Sprite Renderer 上的颜色是白色的,即使六边形看起来是黄色的)。
顺便说一句,现在更改 Sprite 渲染器中的颜色会更改所有六边形的颜色。
我按照 quill18creates 的教程制作了六边形地图,只是我是在 2D 而不是 3D 中制作的。
https://www.youtube.com/watch?v=j-rCuN7uMR8
截至撰写本文时,我的颜色更改脚本根本不起作用。我试图拥有它,所以当它收到一次点击时,它会变成黄色。然后下一步点击蓝色,点击黄色,等等。因为每个玩家只能点击一次。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorChange : MonoBehaviour {
public Color[]colors; // allows input of material colors in a set sized array
public SpriteRenderer rend; // what are we rendering? the hex
private int index = 1; //initialize at 1, otherwise you have to press the ball twice to change color
// Use this for initialization
void Start () {
rend = GetComponent<SpriteRenderer> (); // gives functionality for the renderer
}
// Update is called once per frame
void onMouseDown () {
// if there are no colors present nothing happens
if (colors.Length == 0){
return;
}
if (Input.GetMouseButtonDown(0)){
index += 1; // when mouse is pressed down we increment up to the next index location
// when it reaches the end of the colors it stars over
if (index == colors.Length +1){
index = 1;
}
print (index); // used for debugging
rend.color = colors [index - 1]; // this sets the material color values inside the index
}
} //onMouseDown
}
我应该如何实施呢?任何帮助将不胜感激!
为您的预制件添加对撞机和刚体 然后在 OnMouseDown 中大写 O 并删除 "if (Input.GetMouseButtonDown(0)){" Input.GetMouseButtonDown(0) return 当玩家点击任何你不想要的地方时为真
public Color[] colors; // allows input of material colors in a set sized array
public SpriteRenderer rend; // what are we rendering? the hex
private int index = 1; //initialize at 1, otherwise you have to press the ball twice to change color
// Use this for initialization
void Start()
{
rend = GetComponent<SpriteRenderer>(); // gives functionality for the renderer
}
// Update is called once per frame
void OnMouseDown()
{
// if there are no colors present nothing happens
if (colors.Length == 0)
{
return;
}
index += 1; // when mouse is pressed down we increment up to the next index location
// when it reaches the end of the colors it stars over
if (index == colors.Length + 1)
{
index = 1;
}
print(index); // used for debugging
rend.color = colors[index - 1]; // this sets the material color values inside the index
} //onMouseDown
首先,您需要正确地大写 OnMouseDown()
才能被调用。
之后,由于您使用的是 SpriteRenderer
,因此您需要添加一个碰撞器来检测鼠标点击事件。如 OnMouseDown()
文档中所述:
This event is sent to all scripts of the Collider or GUIElement.
因为你两者都没有,在你的预制件上,点击 Add Component > Polygon Collider 2D 它会自动为你的精灵创建合适的几何体(假设六边形之外的所有东西是透明的)。
最后,取消对 Input.GetMouseButtonDown(0)
的检查。 OnMouseDown 已经捕获了鼠标被单击的事实,并且特定实例 运行 OnMouseDown()
是被单击的实例。