Unity-如何在一个对话树脚本上创建两个按钮事件

时间:2018-07-13 12:02:11

标签: unity3d dialog composite

因此,我一直在将一个测试程序与一个简单的对话树放在一起,在对话树中您会看到一个问题和两个选项按钮,当您按下一个对话树时,您将进入树的另一层。因此,我做了一个添加所有树级别的列表,其中包含问题的参数和两个选项。我让自己感到困惑。我真的不确定如果按下某个按钮如何将级别添加到列表中。除了初始化脚本外,我还要保留一个脚本。我能想到的最好的办法就是为按下按钮创建bool函数,但是我真的想不出如何区分一个按钮和另一个按钮。如果有人对此有更好的想法。我会很感激。谢谢。

 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;
}
 }

1 个答案:

答案 0 :(得分:1)

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

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. 的位置
  6. 单击下拉菜单中的No Function,然后选择脚本和适当的调用方法,例如Button1Pressed

enter image description here

相关问题