C# 如何在unity中单击调用类

C# 如何在unity中单击调用类,c#,unity3d,C#,Unity3d,我想创建一个带有yes和no按钮的对话框,所以我使用gui.window创建了一个脚本,但现在我想在单击back按钮时调用这个脚本 toast.cs void OnGUI() { if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) { } } public class dialog : MonoBehaviour { public GUIStyle mystyle; public Rect

我想创建一个带有yes和no按钮的对话框,所以我使用gui.window创建了一个脚本,但现在我想在单击back按钮时调用这个脚本

toast.cs

void OnGUI()
{
    if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
    { 

    }
}
public class dialog : MonoBehaviour
{
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI ()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) 
    {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
           Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
           Application.LoadLevel("Settings");   
       }
    }
}
void OnGUI() {
       if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
       { 
            new dialog().OpenGUI();
       }
}
public class dialog : MonoBehaviour {
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI () {
       OpenGUI();
    }

    public void OpenGUI(){
       windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
         Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
         Application.LoadLevel("Settings");   
       }
    }

 }
dialog.cs

void OnGUI()
{
    if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
    { 

    }
}
public class dialog : MonoBehaviour
{
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI ()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) 
    {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
           Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
           Application.LoadLevel("Settings");   
       }
    }
}
void OnGUI() {
       if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
       { 
            new dialog().OpenGUI();
       }
}
public class dialog : MonoBehaviour {
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI () {
       OpenGUI();
    }

    public void OpenGUI(){
       windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
         Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
         Application.LoadLevel("Settings");   
       }
    }

 }

你可以试试这个:

toast.cs

void OnGUI()
{
    if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
    { 

    }
}
public class dialog : MonoBehaviour
{
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI ()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) 
    {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
           Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
           Application.LoadLevel("Settings");   
       }
    }
}
void OnGUI() {
       if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
       { 
            new dialog().OpenGUI();
       }
}
public class dialog : MonoBehaviour {
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI () {
       OpenGUI();
    }

    public void OpenGUI(){
       windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
         Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
         Application.LoadLevel("Settings");   
       }
    }

 }
dialog.cs

void OnGUI()
{
    if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
    { 

    }
}
public class dialog : MonoBehaviour
{
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI ()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) 
    {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
           Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
           Application.LoadLevel("Settings");   
       }
    }
}
void OnGUI() {
       if (GUI.Button (new Rect (10, 350, 70, 20), "Back ")) 
       { 
            new dialog().OpenGUI();
       }
}
public class dialog : MonoBehaviour {
    public GUIStyle mystyle;
    public  Rect windowRect = new Rect (150, 80, 200, 100) ;

    public  void OnGUI () {
       OpenGUI();
    }

    public void OpenGUI(){
       windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
    }

    public  void WindowFunction (int windowID) {
         GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle  );
       if (GUI.Button (new Rect (10, 70, 70, 20), "Yes ")) 
       { 
         Application.LoadLevel("Settings");
       }

       if (GUI.Button (new Rect (120, 70, 70, 20), "No ")) 
       { 
         Application.LoadLevel("Settings");   
       }
    }

 }

不过,这只是常识,您可能想了解一下,他们将能够提供比StackOverflow更多的帮助

//This will add the dialog, so, And now the window will show up!
gameObject.AddComponent<dialog>();
//这将添加对话框,因此,现在将显示窗口!
gameObject.AddComponent();
由于对话框脚本是单行为,因此必须将其添加到对象中才能显示

而且,你必须勾选“否”选项,它给出的结果与“是”相同:p

你可以做一个

//This will remove the dialog, so, I will no longer see the window =)
gameObject.RemoveComponent<dialog>();
//这将删除该对话框,因此,我将不再看到窗口=)
gameObject.RemoveComponent();

这就是你想要的吗?:)

@Tzah-Mama感谢您的建议和sry,但是这个新对话框();不能使用。我认为这里是各种各样的查询的地方,这就是为什么我要求你更详细地解释你想做什么。我有一个想法,但除非我确定你的问题是什么,否则我不想回答。@functor on button click我想创建一个对话框,其中有两个按钮“yes”和“no”。在“yes”上,用户可以保存游戏状态。在“no”上,用户可以退出主菜单。所以我在GUI窗口的帮助下创建了一个对话框,它在seprate脚本中运行得非常好,但我想在单击按钮时调用它。