我在Unity中建立一个对话系统,玩家可以与NPC交谈并为每个NPC提供独特的响应选择(想想Elder Scrolls)。我有一个用于播放器的可用响应选项的字符串数组,也可以使用foreach循环将它们打印到GUI按钮上。
然而,我坚持认为如何跟踪按下了什么按钮。
void OnGUI()
{
int i = 0;
if(choicesReceived)
{
foreach (string s in playerChoices)
{
i++;
if (GUI.Button(new Rect(850,(50 * i) + 275, (s.Length * 4), 50), s))
{
Debug.Log("Chose an option");
}
}
}
}
对于硬编码并根据字符串值检测所选按钮似乎不是一个好主意,但是现在我无法更好地处理更好的事情。提前谢谢,人们。
答案 0 :(得分:2)
我强烈建议使用Unity的新UI系统,而不是使用OnGui。 Starter Tutorials
这更易于开发,并且内置事件系统可用于您尝试执行的操作。
在OnClick事件中,您可以调用特定的脚本方法并传入您按下的按钮。
文档=> Unity UI Manual
此外,在接近对话系统时,您必须在课堂上编写的文本字符串数量可能会失控。因此,您可能希望采用将对话框反序列化为JSON或XML文件的方法。
例如,这里有一些JSON:
`{
"id": 001,
"dialogue":"I used to be an adventurer like you...",
"responses":[002, 003]
},
{
"id": 002,
"dialogue":"Wait.I know you...",
"responses":[]
},
{
"id": 003,
"dialogue":"Be careful out there.",
"responses":[]
}`
然后,所有回复都会保留在您的代码之外,您只需要参考您需要的ID。
答案 1 :(得分:0)
i
就是答案。
此变量保存按钮的索引。从1,2,3 ...... N开始
尝试此日志消息:
Debug.Log("Chose an option : " + i);
更新:
根据您的comment,您需要将uGUI作为当前方案的最佳选择,以及以后使用。
我相信这就是您所寻找的:https://stackoverflow.com/a/39615711/4366237
答案 2 :(得分:0)
您正在使用旧的Unity UI系统(IMGUI)。使用推荐的新UI(uGUI)。
<强> 1 强>的游戏物体强> - &GT;的 UI 强> - &GT; 按钮强>
2 。将新创建的按钮设为预制件,然后删除原始按钮。 (不要删除画布。只需删除按钮)
3 。使用Instantiate
函数实例化Button。
4 。将新Button设为Canvas的子项。
5 。使用onClick.AddListener
功能提供按钮,为其提供回调功能。单击Button
时将调用此函数。
创建按钮/画布预制示例:
示例脚本(代码已注释):
确保包含using UnityEngine.UI;
。
public GameObject canvas;
public GameObject buttonPrefab;
string[] playerChoices = new string[3];
void Start()
{
playerChoices[0] = "Message 1";
playerChoices[1] = "Message 2";
playerChoices[2] = "Message 3";
createButtons();
}
void createButtons()
{
int i = 0;
const float yPosOffset = 40f;
float offsetCounter = 0;
foreach (string s in playerChoices)
{
//Create new Button
GameObject tempObj = Instantiate(buttonPrefab, Vector3.zero, Quaternion.identity) as GameObject;
//Rename Button
tempObj.name = "button: " + i;
//Make the Button child of the Canvas
tempObj.transform.SetParent(canvas.transform, false);
Button tempButton = tempObj.GetComponent<Button>();
//Set Button Text
tempButton.GetComponentInChildren<Text>().text = playerChoices[i];
//Set Button Position
Vector2 pos = Vector2.zero;
pos.y = offsetCounter;
Debug.Log(pos.y);
tempButton.GetComponent<RectTransform>().anchoredPosition = pos;
tempButton.onClick.AddListener(() => clickAction(tempButton));
offsetCounter += yPosOffset; //Increment Position
i++;
}
}
//This function will be called when a Button is clicked
void clickAction(Button buttonClicked)
{
//Debug.Log("Clicked Button: " + buttonClicked.name);
Debug.Log("Clicked Button: " + buttonClicked.GetComponentInChildren<Text>().text);
}
答案 3 :(得分:0)
infilename = "path/data.log"
outfilename = "path/OutputData.csv"
with open(infilename, 'r') as infile,\
open(outfilename, "w") as outfile:
lineCounter = 0
for line in infile:
lineCounter += 1
if lineCounter % 1000000 == 0:
print lineCounter
data = line.split("|")
if len(data) < 4:
continue
bidsplit = data[3].split("bidTheo:")
namebid = data[3].split("BidPrice:")
if len(bidsplit) == 2:
bid = float(bidsplit[1].strip().split()[0])
bidname = namebid[0].strip().split(",")[0]
#print "bidTheo," + data[0] + "," + str(bid)
outfile.write("bidTheo," + data[0] + "," + bidname + "," + str(bid) + "\n")
offersplit = data[3].split("offerTheo:")
nameoffer = data[3].split("AskPrice:")
if len(offersplit) == 2:
offer = float(offersplit[1].strip().split()[0])
offername = nameoffer[0].strip().split(",")[0]
#print "offerTheo," + data[0] + "," + str(offer)
outfile.write("offerTheo," + data[0] + "," + offername + "," + str(offer) + "\n")
print "Done"