我正在嘗試比較 if(string = "") 中的字串,但顯然不一樣
字串是“thisColor”,它由檔案的內容定義。我嘗試使用 Debug.Log(thisColor),結果是“rouge”,但在 if(thisColor == "rouge") 中無法識別。我也試過 switch(thisColor) 然后 case() ...
也許是編碼..?
這是代碼片段:
thisColor = "";
pixelSetup = GameObject.Find("Pixel" setupPixelNumber.ToString());
setupPixelNumber = 1;
if (File.Exists("C:/PekmiIndustries/MVPlace/Pixel" pixell ".txt"))
{
thisColor = File.ReadAllText("C:/PekmiIndustries/MVPlace/Pixel" pixell ".txt");
}
else
{
File.Create("C:/PekmiIndustries/MVPlace/Pixel" pixell ".txt").Dispose();
}
string[] retourSuppr = new string[] { "\n" };
foreach(var c in retourSuppr)
{
thisColor = thisColor.Replace(c, string.Empty);
}
pixell = pixelSetup;
pixell.GetComponent<Renderer>().enabled = true;
Debug.Log("thisColor = |" thisColor "|");
if (thisColor != "")
{
Debug.Log(thisColor);
if (thisColor == "rouge")
{
Debug.Log("done.");
pixell.GetComponent<Renderer>().material.color = new Color(255f / 255f, 0, 0, 1);
thisColor = "";
}
else if (thisColor == "orangeF")
{
pixell.GetComponent<Renderer>().material.color = new Color(255f / 255f, 70f / 255f, 0 / 255f, 1);
thisColor = "";
}
else if (thisColor == "orange")
{
pixell.GetComponent<Renderer>().material.color = new Color(255f / 255f, 128f / 255f, 0 / 255f, 1);
thisColor = "";
}
else
{
Debug.Log("Passed");
}
謝謝 :)
uj5u.com熱心網友回復:
這是因為您的檔案使用不同于 UTF-8 的方式編碼,這是 UTF-8 使用的默認讀取方法ReadAllText
正如在這個答案中所說,您可以告訴ReadAllText
使用 unicode 代替。
File.ReadAllText("yourTextFile.txt", Encoding.Unicode);
uj5u.com熱心網友回復:
我不確定你“thisColor = File.ReadAllText("C:/PekmiIndustries/MVPlace/Pixel" pixell ".txt");" 結果,但我認為結果包含空格,所以你可以試試這個:
thisColor=thisColor.Replace(" ","")
uj5u.com熱心網友回復:
如果您確定“胭脂”是其中之一,只需使用Contains
:
if (thisColor.Contains("rouge"))
{
// do something
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/471712.html
下一篇:返回列表