C# 无法隐式转换类型';浮动';至';int';。是否存在显式转换?
当我试图做一个单词拼凑游戏时,它给了我一个错误 Assets\Scripts\WordScramble\WordScramble.cs(119,26):错误CS0266:无法隐式地将类型“float”转换为“int”。存在显式转换(是否缺少强制转换?) Assets\Scripts\WordScramble\WordScramble.cs(269,34):警告CS0642:可能错误的空语句 可能会出什么问题?这有点混乱,因为这是一个试用,但请帮助C# 无法隐式转换类型';浮动';至';int';。是否存在显式转换?,c#,unity3d,C#,Unity3d,当我试图做一个单词拼凑游戏时,它给了我一个错误 Assets\Scripts\WordScramble\WordScramble.cs(119,26):错误CS0266:无法隐式地将类型“float”转换为“int”。存在显式转换(是否缺少强制转换?) Assets\Scripts\WordScramble\WordScramble.cs(269,34):警告CS0642:可能错误的空语句 可能会出什么问题?这有点混乱,因为这是一个试用,但请帮助 using UnityEngine.UI; us
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;
using System.Reflection;
namespace GameCoreSystem
{
[System.Serializable]
public class Result
{
public int totalScore = 0;
[Header("REF UI")]
public Text textTime;
public Text textTotalScore;
[Header("REF RESET SCREEN")]
public Text textResultScore;
public Text textInfo;
public GameObject resultCanvas;
public void ShowResult ()
{
//show damage dealt
textResultScore.text = totalScore.ToString();
textInfo.text = "Your damage is " + WordScramble.main.words.Length + " damages";
int allTimeLimit = WordScramble.main.GetAllTimeLimit();
resultCanvas.SetActive(true);
}
}
[System.Serializable]
public class Word
{
public string word;
[Header("leave empty if you want to randomized")]
public string desiredRandom;
[Space(10)]
public int timeLimit;
public string GetString()
{
if (!string.IsNullOrEmpty(desiredRandom))
{
return desiredRandom;
}
string result = word;
while (result == word)
{
result = "";
List<char> characters = new List<char>(word.ToCharArray());
while (characters.Count > 0)
{
int indexChar = Random.Range(0, characters.Count - 1);
result += characters[indexChar];
characters.RemoveAt(indexChar);
}
}
return result;
}
}
public class WordScramble : MonoBehaviour
{
public Word[] words;
[Space(10)]
public Result result;
[Header("UI REFERENCE")]
public GameObject wordCanvas;
public CharObject prefab;
public Transform container;
public float space;
public float lerpSpeed = 5;
List<CharObject> charObjects = new List<CharObject>();
CharObject firstSelected;
public int currentWord;
public static WordScramble main;
public int totalScore;
void Awake()
{
main = this;
}
// Start is called before the first frame update
void Start()
{
ShowScramble(currentWord);
result.textTotalScore.text = result.totalScore.ToString();
}
// Update is called once per frame
void Update()
{
RepositionObject();
//Update Score and calculate
totalScore = Mathf.Lerp(totalScore, result.totalScore, Time.deltaTime * 5);
result.textTotalScore.text = Mathf.RoundToInt(totalScore).ToString();
}
public int GetAllTimeLimit ()
{
float result = 0;
foreach (Word w in words)
{
result += w.timeLimit / 2;
}
return Mathf.RoundToInt(result);
}
void RepositionObject()
{
if (charObjects.Count == 0)
{
return;
}
float center = (charObjects.Count - 1) / 2;
for (int i = 0; i < charObjects.Count; i++)
{
charObjects[i].rectTransform.anchoredPosition
= Vector2.Lerp(charObjects[i].rectTransform.anchoredPosition,
new Vector2((i - center) * space, 0), lerpSpeed * Time.deltaTime);
charObjects[i].index = i;
}
}
public void ShowScramble()
{
ShowScramble(Random.Range(0, words.Length - 1));
}
public void ShowScramble(int index)
{
charObjects.Clear();
foreach (Transform child in container)
{
Destroy(child.gameObject);
}
//FINISHED DEBUG
if (index > words.Length - 1)
{
result.ShowResult();
wordCanvas.SetActive(false);
//Debug.LogError("index out of range, please enter number between 0-" + (words.Length - 1).ToString());
return;
}
char[] chars = words[index].GetString().ToCharArray();
foreach (char c in chars)
{
CharObject clone = Instantiate(prefab.gameObject).GetComponent<CharObject>();
clone.transform.SetParent(container);
clone.Init(c);
charObjects.Add(clone);
}
currentWord = index;
StartCoroutine(TimeLimit());
}
public void Swap(int indexA, int indexB)
{
CharObject tmpA = charObjects[indexA];
charObjects[indexA] = charObjects[indexB];
charObjects[indexB] = tmpA;
charObjects[indexA].transform.SetAsLastSibling();
charObjects[indexB].transform.SetAsLastSibling();
CheckWord();
}
public void Select(CharObject charObject)
{
if (firstSelected)
{
Swap(firstSelected.index, charObject.index);
//Unselect
firstSelected.Select();
charObject.Select();
}
else
{
firstSelected = charObject;
}
}
public void UnSelect()
{
firstSelected = null;
}
public void CheckWord()
{
StartCoroutine(CoCheckWord());
}
//Check if right or wrong
IEnumerator CoCheckWord ()
{
yield return new WaitForSeconds(0.5f);
string word = "";
foreach (CharObject charObject in charObjects)
{
word += charObject.character;
}
if (timeLimit <= 0)
{
currentWord++;
ShowScramble(currentWord);
yield break;
}
if (word == words[currentWord].word)
{
currentWord++;
result.totalScore += Mathf.RoundToInt(timeLimit);
//StopCorontine(TimeLimit());
ShowScramble(currentWord);
}
}
//Change time limit here
public int timeLimit;
IEnumerator TimeLimit ()
{
float timeLimit = words[currentWord].timeLimit;
result.textTime.text = Mathf.RoundToInt(timeLimit).ToString();
int myWord = currentWord;
yield return new WaitForSeconds(1);
while (timeLimit > 0);
{
if (myWord != currentWord) { yield break; }
timeLimit -= Time.deltaTime;
result.textTime.text = Mathf.RoundToInt(timeLimit).ToString();
yield return null;
}
//score text
CheckWord();
}
}
}
使用UnityEngine.UI;
使用系统集合;
使用System.Collections.Generic;
使用系统线程;
利用制度全球化;
运用系统反思;
名称空间游戏系统
{
[系统可序列化]
公开课成绩
{
公共整数总分=0;
[标题(“参考用户界面”)]
公共文本时间;
公共文本文本总分;
[标题(“参考重置屏幕”)]
公共文本文本结果库;
公共文本信息;
公共游戏对象结果NVAS;
公开无效显示结果()
{
//显示造成的损害
textResultScore.text=totalScore.ToString();
textInfo.text=“你的伤害是”+WordScramble.main.words.Length+“伤害”;
int-allTimeLimit=WordScramble.main.GetAllTimeLimit();
resultCanvas.SetActive(true);
}
}
[系统可序列化]
公共类词
{
公共字符串;
[标题(“如果要随机化,请留空”)]
公共字符串所需的随机数;
[空间(10)]
公共时间限制;
公共字符串GetString()
{
如果(!string.IsNullOrEmpty(desiredRandom))
{
返回所需的随机数;
}
字符串结果=字;
while(结果=word)
{
结果=”;
列表字符=新列表(word.ToCharArray());
而(字符数>0)
{
int indexChar=Random.Range(0,字符数-1);
结果+=字符[indexChar];
字符.RemoveAt(indexChar);
}
}
返回结果;
}
}
公共类文字混乱:单一行为
{
公共词[]词;
[空间(10)]
公共结果;
[标题(“UI引用”)]
公共游戏对象;
公共物品预制;
公共转换容器;
公共浮动空间;
公共浮点数LERPSEED=5;
List charObjects=new List();
首先选择的对象为CharObject;
公共词;
公共静态字扰频主;
公共积分总分;
无效唤醒()
{
main=这个;
}
//在第一帧更新之前调用Start
void Start()
{
ShowScramble(currentWord);
result.textTotalScore.text=result.totalScore.ToString();
}
//每帧调用一次更新
无效更新()
{
重新定位对象();
//更新分数并计算
totalScore=Mathf.Lerp(totalScore、result.totalScore、Time.deltaTime*5);
result.textTotalScore.text=Mathf.RoundToInt(totalScore.ToString();
}
public int GetAllTimeLimit()
{
浮动结果=0;
foreach(单词中的w)
{
结果+=w.timeLimit/2;
}
返回Mathf.RoundToInt(结果);
}
void对象()
{
if(charObjects.Count==0)
{
返回;
}
浮动中心=(charObjects.Count-1)/2;
for(int i=0;i单词长度-1)
{
result.ShowResult();
SetActive(false);
//Debug.LogError(“索引超出范围,请输入0-”+(words.Length-1.ToString())之间的数字);
返回;
}
char[]chars=words[index].GetString().ToCharray();
foreach(字符中的字符c)
{
CharObject clone=实例化(prefab.gameObject.GetComponent();
clone.transform.SetParent(容器);
克隆Init(c);
charObjects.Add(克隆);
}
currentWord=索引;
start例程(TimeLimit());
}
公共无效掉期(int indexA、int indexB)
{
CharObject tmpA=charObjects[indexA];
charObjects[indexA]=charObjects[indexB];
charObjects[indexB]=tmpA;
charObjects[indexA].transform.SetAsLastSibling();
charObjects[indexB].transform.SetAsLastSibling();
校验字();
}
公共无效选择(CharObject CharObject)
{
如果(第一次选择)
{
交换(firstSelected.index、charObject.index);
//取消选择
firstSelected.Select();
charObject.Select();
}
其他的
{
第一选择