我正在尝试从单个阵列中生成两个随机颜色,这些颜色应该是相同的。其中一种颜色是标签的bg颜色,另一种颜色是文本顶部的文本颜色。但奇怪的是我失败了。这是代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ColorText : MonoBehaviour {
public GUISkin mySkin;
public GameObject obj;
private Color clr;
private Color txtClr;
private string clrName;
Color[] colorList = new Color[13]{
new Color(0,0,255,255), // mavi
new Color(165,42,42,255), // kahverengi
new Color(192,192,192,255), // gri (192,192,192 gümüş)
new Color(0,128,0,255), // yeşil
new Color(128,0,0,255), // bordo
new Color(255,165,0,255), // turuncu
new Color(255,192,203,255), // pembe
new Color(128,0,128,255), // mor
new Color(255,0,0,255), // kırmızı
new Color(64,224,208,255), // turkuaz
new Color(255,255,0,255), // sarı
new Color(255,255,255,255), // beyaz
new Color(0,0,0,255), // siyah
};
string[] colorNames = new string[13]{
"Mavi",
"Kahverengi",
"Gri", // gri (192,192,192 gümüş)
"Yeşil", // yeşil
"Bordo", // bordo
"Turuncu", // turuncu
"Pembe", // pembe
"Mor", // mor
"Kırmızı", // kırmızı
"Turkuaz", // turkuaz
"Sarı", // sarı
"Beyaz", // beyaz
"Siyah", // siyah
};
// Use this for initialization
void Start () {
InvokeRepeating ("changeColors", 0, 1f);
}
void changeColors(){
int a, b;
a = (int)Random.Range (0, colorList.Length);
b = (int)Random.Range (0, colorList.Length);
if (a == b) {
do{
a = (int)Random.Range (0, colorList.Length);
b = (int)Random.Range (0, colorList.Length);
}while(a == b);
clr = colorList [a];
txtClr = colorList [b];
clrName = colorNames [Random.Range (0, colorNames.Length)];
} else {
clr = colorList [a];
txtClr = colorList [b];
clrName = colorNames [Random.Range (0, colorNames.Length)];
}
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
GUI.skin = mySkin;
mySkin.box.fontStyle = FontStyle.Bold;
mySkin.box.normal.textColor = txtClr;
//GUI.backgroundColor = Color.blue;
Texture2D texture = new Texture2D(1, 1);
//Set new texture as background
mySkin.box.normal.background = texture;
//Set the texture color
texture.SetPixel(1,1,clr);
// Apply all SetPixel calls
texture.Apply();
/*float scalex = (float)(Screen.width) / 480.0f;
float scaley = (float)(Screen.height) / 800.0f;
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1));*/
GUI.Box(new Rect(Screen.width/2 - 400,10,800,200), clrName, "box");
//GUI.Label(new Rect(Screen.width/2, 15, 100, 20), "Hello World!");
}
}
我失踪了吗?感谢您的帮助。 编辑:我检查了指数进行比较。他们不一样。
答案 0 :(得分:1)
我的方法效率稍差。首先将您的颜色数组克隆到临时列表中,您可以根据需要进行更改。然后删除第一个随机颜色的索引。这是一个例子:
void changeColors()
{
List<Color> tempColors = new List<Color>(colorList);
// Get the first random index.
int a = (int)Random.Range (0, tempColors.Length);
// Assign the colour.
clr = tempColors[a];
// Remove the colour from the list so that it is impossible to select the same again.
tempColors.RemoveAt(a);
// Get second index.
int b = (int)Random.Range (0, tempColors.Length);
// Assign second colour.
txtClr = tempColors[b];
}
我希望这可以帮到你。
编辑: 如果问题证明是其他问题,我认为代码清理在这里是合适的。只是为了向您展示一种不同的写作方式:
void changeColors(){
int a,b;
a = b = 0;
while (a == b)
{
a = (int)Random.Range (0, colorList.Length);
b = (int)Random.Range (0, colorList.Length);
}
clr = colorList [a];
txtClr = colorList [b];
clrName = colorNames [Random.Range (0, colorNames.Length)];
}
如果我没弄错的话,这应该与你当前的代码完全相同。
答案 1 :(得分:0)
如果你只需要一次只需要两种颜色,那么你为什么把它推到invokerepeating
中?如果你希望它立即开始,那么invokerepeating
开始时间的旁注不会为零开始时间值较小
void Start () {
Changecolors();
}
答案 2 :(得分:0)
“Color使用0到1之间的浮点值,Color32使用0到255之间的字节值。
http://docs.unity3d.com/ScriptReference/Color-ctor.html
http://docs.unity3d.com/ScriptReference/Color32.html
alucardj解决了它。