我試圖制作一個 2d Unity 拉丁語翻譯器,但我遇到了問題。我怎樣才能讓它識別“Declinatio”中已經存在的 Text 和 Dropdown 變數。我需要檢查這個字串的最后 2 個字符,即 SGenTextStr,并且資料應該取自 SGenText InputField。我不知道你有沒有解決這個問題,我沒有在互聯網上找到任何有用的東西。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
public static void Declinatio()
{
string SGenTextStr;
string AnsTextStr;
int StrLength;
SGenTextStr = SGenText.text;
AnsTextStr = AnsText.text;
StrLength = SGenTextStr.Length();
switch (SGenTextStr.charAt(StrLength - 2) SGenTextStr.charAt(StrLength - 1))
{
default:
break;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
InputField SGenText;
Dropdown DeclDrop;
InputField AnsText;
}
}
uj5u.com熱心網友回復:
在 Unity 游戲中你可能會經常使用的東西之一就是Gameobject.GetComponent<>()方法。它允許您從 Unity 場景中的其他組件或腳本訪問值和函式。
宣告變數還有一個問題,Update()因為它會導致它們在每一幀都被擦除干凈,而不是你可能希望將它們一起移到外面。
因此,您也會遇到麻煩,Declinatio()因為它當前是靜態的,這不允許它參考它之外的任何非靜態變數。
最后一個問題是 C# 實際上沒有實作,String.charat()但是您可以使用它String[int]來實作相同的目標。
通過所有這些更改,它看起來像這樣:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
public InputField SGenText;
public Dropdown DeclDrop;
public InputField AnsText;
public void Declinatio()
{
string SGenTextStr;
string AnsTextStr;
int StrLength;
SGenTextStr = SGenText.GetComponent<InputField>().text;
AnsTextStr = AnsText.GetComponent<InputField>().text;
StrLength = SGenTextStr.Length;
switch (SGenTextStr[(StrLength - 2)] SGenTextStr[(StrLength - 1)])
{
default:
break;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/432323.html
