【问题标题】:UnityPlayerActivity getContext() substitutionUnityPlayerActivity getContext() 替换
【发布时间】:2015-08-14 11:26:31
【问题描述】:

UnityPlayerActivity 中的 getContext 有替代方案吗?

我想要如下所示的代码。

Activity.getContext()

【问题讨论】:

    标签: android android-activity unity3d unityscript


    【解决方案1】:

    here的回答

    using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                playerActivityContext = actClass.GetStatic< AndroidJavaObject >("currentActivity");
            }
    

    或者你可以使用here下面的代码

    public class Main extends UnityPlayerActivity {
    
        public static Context mContext;
    
        @Override
        protected void onCreate(Bundle bundle)
        {
                super.onCreate(bundle);
                mContext = this;
        }
    }
    

    【讨论】:

      【解决方案2】:

      getContext() 未在 Activity 中定义。它在 View 中用于获取对封闭 context(一个 Activity)。

      请阅读下面的答案。希望对您有所帮助。

      Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

      【讨论】:

        猜你喜欢
        • 2018-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-10
        • 1970-01-01
        • 2014-10-24
        • 1970-01-01
        相关资源
        最近更新 更多