-2

I'm making my own android game with unity, I want to make quit confirmation screen just like this one:

enter image description here

How is it possible to make it using c#?

6

2 Answers 2

0

You can access to java.lang.Class and the java.lang.Object like this.

AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<  AndroidJavaObject >  ("currentActivity");

from this you should be able to set GUI components, buttons, and click listeners to imitate an alert dialog

See this post for further understanding

4
  • I copied the code, still the same problem gyazo.com/3d9a782501ceaeebf5b09ba894713f72 Jan 29, 2016 at 14:13
  • I guess it could means you're missing a reference dont you ? "if you want to say create a custom dialog you can create a jar library which has the required code and access the classes in the jar by using the same technique. " Have you referenced a jar with the code needed ? Jan 29, 2016 at 14:15
  • I'm not sure, I'm using visual studio that came with unity Jan 29, 2016 at 14:18
  • You have to make a jar with the AlertDialog java code in it, add the jar as a reference to your project, and then you'll be able to manipulate it. What is a JAR ? Jan 29, 2016 at 14:19
0

I made a small example for you

  1. attach the following script on a gameobject (the canvas for example)
  2. add to the root of your normal ui (a panel for example) a CanvasGroup component
  3. add to the root of your quit confirmation ui (a panel for example) a CanvasGroup component
  4. Link the two canvas group to the gameobject where you put the script
  5. For each button (Quit, Confirm yes, Confirm No) add in the Inspector a onclick event to the script corresponding method
  6. That is all i think (I tried to comment a bit the code for reference), Reference

    public class QuitHandler : MonoBehaviour { 
    
     public CanvasGroup uiCanvasGroup;
     public CanvasGroup confirmQuitCanvasGroup;
    
     // Use this for initialization
     private void Awake()
     {
         //disable the quit confirmation panel
         DoConfirmQuitNo();
     }
    
     /// <summary>
     /// Called if clicked on No (confirmation)
     /// </summary>
     public void DoConfirmQuitNo()
     {
         Debug.Log("Back to the game");
    
         //enable the normal ui
         uiCanvasGroup.alpha = 1;
         uiCanvasGroup.interactable = true;
         uiCanvasGroup.blocksRaycasts = true;
    
         //disable the confirmation quit ui
         confirmQuitCanvasGroup.alpha = 0;
         confirmQuitCanvasGroup.interactable = false;
         confirmQuitCanvasGroup.blocksRaycasts = false;
     }
    
     /// <summary>
     /// Called if clicked on Yes (confirmation)
     /// </summary>
     public void DoConfirmQuitYes()
     {
         Debug.Log("Ok bye bye");
         Application.Quit();
     }
    
     /// <summary>
     /// Called if clicked on Quit
     /// </summary>
     public void DoQuit()
     {
         Debug.Log("Check form quit confirmation");
    
         //reduce the visibility of normal UI, and disable all interraction
         uiCanvasGroup.alpha = 0.5f;
         uiCanvasGroup.interactable = false;
         uiCanvasGroup.blocksRaycasts = false;
    
         //enable interraction with confirmation gui and make visible
         confirmQuitCanvasGroup.alpha = 1;
         confirmQuitCanvasGroup.interactable = true;
         confirmQuitCanvasGroup.blocksRaycasts = true;
     }
    
     /// <summary>
     /// Called if clicked on new game (example)
     /// </summary>
     public void DoNewGame()
     {
         Debug.Log("Launch a new game");
     }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.