首页 > 解决方案 > Unity-如何在一个对话树脚本上创建两个Button事件

问题描述

所以我一直在用一个简单的对话树组合一个测试程序,在那里你会得到一个问题和两个选项按钮,当你按下一个按钮时,你会进入树的另一个层次。所以我做了一个列表,添加了所有的树级别,包含问题的参数和两个选项。不过,我让自己感到困惑。如果我按下某个按钮,我不确定如何将级别添加到列表中。除了初始化脚本之外,我想保留一个脚本。我能想到的最好的方法是为按下按钮创建布尔函数,但我真的想不出如何区分一个按钮和另一个按钮。如果有人对如何解决这个问题有任何更好的想法。我会很感激。谢谢你。

 public class Level : MonoBehaviour {

 bool button1Pressed;
 bool button2Pressed;

private void Start()
{

    Level Level1 = new Level("Hello", "Hi", "Shut Up");
    Level leaf1 = new Level("Don't be Rude");

    Level Level2 = new Level("What you Doing?", "Not Much", "None of your Business");
    Level leaf2 = new Level("Well Excuuuuse Me");

    Level Level3 = new Level("Can I do that too?", "Sure", "Go Away");
    Level leaf3 = new Level("Fine. Be a Jerk");

    Level Level4 = new Level("This is boring, can we do something else?", "Why not?", "You're boring");
    Level leaf4 = new Level("I'll go be boring somewhere else");

    Level Level5 = new Level("You want ice cream?", "Sounds Good", "I'm allergic");
    Level leaf5 = new Level("ok.......");
    Level leaf = new Level("I Want Chocolate");      

    Level1.add(Level1);
    Level1.add(leaf1);

    Level2.add(Level3);
    Level2.add(leaf2);

    Level3.add(Level4);
    Level3.add(leaf3);

    Level4.add(Level5);
    Level4.add(leaf4);

    Level5.add(leaf5);
    Level5.add(leaf);

    levels.Add(Level1);
    levels.Add(Level2);
    levels.Add(Level3);
    levels.Add(Level4);
    levels.Add(Level5);

}



public static Text Textbox;
public static Button Button1;
public static Button Button2;

    public string OptionA;
    public string OptionB;
    public string Question;

    public string Leaf;

    private List<Level> levels;

    public Level(string question, string optionA, string optionB)
    {
        this.Question = question;
        this.OptionA = optionA;
        this.OptionB = optionB;

        Textbox.text = Question;
        Button1.GetComponentInChildren<Text>().text = OptionA;
        Button2.GetComponentInChildren<Text>().text = OptionB;

        levels = new List<Level>();

    }

    public Level(string leaf)
    {
        this.Leaf = leaf;
        Textbox.text = leaf;
    }

    public void add(Level lvl)
    {
        levels.Add(lvl);
    }

    public List<Level> getLevels()
    {
        return levels;
    }

 void OnPointerDown()
{
    button1Pressed = true;
}
void OnPointerUp()
{
    button1Pressed = false;
}

}

初始化器

public class Initializer : MonoBehaviour {

public Text Textbox;
public Button Button1;
public Button Button2;

void Awake()
{
    Level.Textbox = this.Textbox;
    Level.Button1 = this.Button1;
    Level.Button2 = this.Button2;
}
 }

标签: unity3ddialogcomposite

解决方案


您可以像这样在脚本中创建两个公共方法

public void Button1Pressed()
{
    // Place Button 1 Logic here
}

public void Button2Pressed()
{
    // Place Button 2 Logic here
}

然后

  1. 转到场景中的每个按钮
  2. 查找On Click () 部分
  3. 点击+
  4. 将包含带有上述代码的脚本的对象拖到它说的位置None (Object)
  5. 单击下拉菜单No Function,然后选择您的脚本以及要调用的适当方法,例如Button1Pressed

在此处输入图像描述


推荐阅读