【问题标题】:Quit confirmation screen for android game with unity统一退出安卓游戏的确认屏幕
【发布时间】:2016-01-29 13:31:57
【问题描述】:

我正在用unity制作自己的android游戏,我想制作这样的退出确认屏幕:

如何使用 c# 实现它?

【问题讨论】:

  • 您必须使用警告对话框。见this
  • 找不到类型或命名空间名称“AlertDialog”。您是否缺少 using 指令或程序集引用?
  • 这是一个问题吗?
  • 是的,有什么问题,它不知道什么是AlertDialog

标签: java c# unity3d


【解决方案1】:

您可以像这样访问 java.lang.Class 和 java.lang.Object。

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

由此您应该能够设置 GUI 组件、按钮和单击侦听器来模拟警报对话框

this post进一步了解

【讨论】:

  • 我复制了代码,还是一样的问题gyazo.com/3d9a782501ceaeebf5b09ba894713f72
  • 我想这可能意味着您缺少参考资料,不是吗? “如果您想说创建一个自定义对话框,您可以创建一个包含所需代码的 jar 库,并使用相同的技术访问 jar 中的类。”您是否引用了包含所需代码的 jar?
  • 我不确定,我用的是unity自带的visual studio
  • 您必须制作一个包含 AlertDialog java 代码的 jar,将该 jar 添加为对您的项目的引用,然后您就可以操作它了。 JAR 是什么?
【解决方案2】:

我为你做了一个小例子

  1. 将以下脚本附加到游戏对象(例如画布)上
  2. 在您的普通用户界面(例如面板)的根目录中添加一个 CanvasGroup 组件
  3. 在您的退出确认 ui(例如面板)的根目录中添加一个 CanvasGroup 组件
  4. 将两个画布组链接到放置脚本的游戏对象
  5. 对于每个按钮(退出、确认是、确认否)在检查器中添加 脚本对应方法的onclick事件
  6. 这就是我的想法(我试着评论一下代码 参考),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");
     }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2014-04-19
    • 1970-01-01
    相关资源
    最近更新 更多