Create Dialog Box/pop up windows - c#

have such code
bool b = EditorUtility.DisplayDialog("Test",
"Reset or continue?", "Reset", "Continue");
if (b)
{
ResetGame();
}
but it works only in Editor and not in Game. How to replace the EditorUtility.DisplayDialog with something that works for game?

Any Unity class that includes the word "Editor" or came from the UnityEditor namespace means that the class is designed to used in the Editor only and will only work in the Editor. So EditorUtility is for Unity Editor only.
You need to implement your own Modal Window and to be able to this, you must understand basic Unity UI such as creating buttons, panels, texts. So learn the Unity basic UI first. All you need to do is to put the UI Objects in a panel then acivate/deactivate them when needed.
For example, this is your dialogue panle:
public GameObject dialoguePanel;
to show a dialogue of the UI Panel
dialoguePanel.SetActive(true);
To hide it:
dialoguePanel.SetActive(false);
You can subscribe to the dialogue's button or UI controls events dynamically with onClick.AddListener. See this post for more information on how to subscribe to UI events.
If you still can't implement your Modal Window, then follow the tutorials below as that's exactly what you are looking for.
Unity Tutorial for a generic modal Window:
MAKING A GENERIC MODAL WINDOW Part 1
MAKING A GENERIC MODAL WINDOW Part 2
MAKING A GENERIC MODAL WINDOW Part 3

Related

How to Change MenuItems in Unity in One Scene

I need to change the items placed in UI on basis of items clicked on.
User have to click on Name of the game then it will unable the existing item and enable the chapters item to show chapters.
Note: I don't need to change scenes, I know how to change scene with buttons.
I have attached the screenshot of the main menu.
Just create a reference of the gameObjects you want to Activate/Deactivate.
Create a button and use GameObject.SetActive to activate/deactivated the objects you want when the user press it.
You can make the button invisible so the user thinks he's clicking the title but actually he clicks a button.
I Hope this helps. :D
I suggest creating a button for every menu interaction in general.
To handle your buttons OnClickEvents you need to create an empty gameObject on your scene and attach to it a script that your buttoms will use to do whatever you want when you click them.
For example:
//You can name your method however you like.
public void ButtomClicked(){
//Hide the UI on the screen expect the Back button.
//Show chapters to the player
}
Create a button and from the inspector select the Empty Gameobject this script is attached to and then select the ButtomClicked method. When you press the buttom the code in the method will run.
To avoid activating/deactivating all this buttons one by one, you can attach them to a panel(UI element) and activate/deactivate the panel istead. So, lets say you have 3 panels.
The Main menu panel, the Chapters panel and the Options panel.
When the player wants to see the chapters, you diactivate the main menu panel and activate the chapters panel. To make this feel really polished you can add transition animations later on.
This is how i handle my UI without never changing scenes. It gives a really smooth and polished feel to the user.
If you have more quastions about UI, plz watch this turtorial, it helped me a lot to understand the basics.

In C# WPF window, adding button for topmost beside standard window buttons

I'd like add a button for changing window state to topmost to the built-in window buttons such as maximize, minimize, and close [please refer to this pic.]
However, I'm having hard time finding the way out as WPF seems not to provide such an API. I even thought using the icon next to the window title functioning as the button for topmost, but looks not feasible.
Is there anyway like using .dll or could I inherit the window class and add the button and corresponding event handler anyhow?
Thanks.
It will be very difficult as you can see below link:
How to add an extra button to the window's title bar?
To make things easy, you should consider implementing a custom window for that. This window will have custom buttons including Close,Minimize,Maximize along with any other buttons as well.

Switching between forms (closing one form, then opening another)

So I'm trying to learn a thing or two about coding with c# and something i find quite annoying is the way to switch between forms.
Lets say for a game you want to go to the options panel and when you click the button to get there it closes that window(form1) and opens a new window(form2) for my app.
It doesn't look very nice having windows opening and closing like that so I'm wondering what i can do in order to make it switch from form1 to form2 without closing form1 and not open form2 in a new window (Everything switched on the main window(form1).
Might sound a bit confusing but hopefully you understand what i mean.
The code I'm using so far to switch between forms:
ChangeOptions optionchanger = new ChangeOptions ();
this.Hide();
optionchanger.Show();
You could add two panels to a single form, each of which contains the controls you would otherwise have added to one of the two forms. Then switch between the panels by changing their visibility or Z-order. This is slightly tricky in the Windows Forms Designer because you'll have to design the two panels, then position them in the same spot on the containing form.
As #ryanyuyu points out, you can set the Dock property to DockStyle.Fill and switch which panel is on top using Control.BringToFront or Control.SendToBack(). This is also a decent way to interact with the two panels in the designer, as you can switch which is on top from a context menu option.
To truly have two forms, your only option is to show a dialog. Hiding your current window is of course optional.
However, you can:
Group all the controls on a given "form" into a Panel or GroupBox, then show/hide the container control.
Put all the controls into UserControls and have an instance of each UserControl on the main form. You can then show/hide the control.
I prefer the second method as it keeps the encapsulation tighter. Since you already have two forms, its easy to convert to user controls.

Mono Gtk Window require focus for input

I haven't been able to find an example containing this functionality, and either i missed it in the documentation or it's not there.
I have a fullscreen GUI program, and when a user is required to type in a number, a calculator window popup has to appear in the center of the screen. The user types a number and clicks on enter, or hits cancel to continue past the window.
The problem is clicking on the fullscreen window behind the calculator brings that window to the front and hides the calculator without the intended entry being completed, which could get annoying for the user.
I guess the functionality I'm trying to create is what happens in most text editors/IDE's when you press the Open File button. Let me know if you want to see code, it's just two separate Window classes at the moment.
The Present() function of the Window object brings that particular window to the front. So adding a FocusOutEvent listener on the window you wish to keep in front like:
windowObj.FocusOutEvent += (obj, args) => windowObj.Present();
will work.
The alternative to this (and the better way) is to set the Modal property of the window to true.
If you've stumbled across this and you were looking for a popup window that suspends whatever called it to wait for input, see this question:
gtk# thread for window
You basically use the Dialog class instead of Window and add your elements to the ActionArea of the Dialog.
Hope this helps.

How do I open a window as a modal dialog in MonoMac using C#?

I am developing a Cocoa application with MonoMac (C#).
I have a class MyWindowController that inherits MonoMac.AppKit.NSWindowController, and open a new instance of this window like this:
MyWindowController mwc = new MyWindowController();
mwc.Window.MakeKeyAndOrderFront(this);
But how do I open it as a modal dialog? It is imperative that nothing else in my application is executed while the dialog is open, so I cannot use a window sheet (which only blocks the current window). And I can't find anything else that seems to do what I want on my controller. On Windows, I would have done this simply by calling:
mwc.ShowDialog();
So what I want is the MonoMac equivalent of ShowDialog(), I believe.
I spent almost three hours trying to figure this out before posting the question, but of course I found the solution right after asking.
It looks like I need to use the NSApplication object:
NSApplication.SharedApplication.RunModalForWindow(ewc.Window);
I had success with the answer above, but then had trouble dismissing the modal in the case that the 'close' button was pressed. The solution was to add the code
[Export ("windowWillClose:")]
public void WindowWillClose(NSNotification notification)
{
Console.WriteLine("windowWillClose:");
NSApplication.SharedApplication.StopModal ();
}
to the window controller, then set the window controller to be the window's delegate by, in Interface Builder, right-click the window and drag a line from "delegate" to the "file's owner" block.

Categories

Resources