关于unity调用AS安卓的真机Debug测试

1.代码篇

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 
/// Discription:A String Extension Powered byMemoryC 
/// Functions: extension a string text to showAsToast, toJavaString, or Speak out
/// CopyRight:MemoryC
/// Time:2017.02.15
/// 
/// 
public static class MemoryCString
{

#if UNITY_ANDROID
    /// 
    /// Show String as Toast.
    /// 
    /// Text.
    /// Activity.
    public static void showAsToast(this string text, AndroidJavaObject activity = null)
    {
        Debug.Log(text);
       
        if (activity == null)
        {
            AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            activity = UnityPlayer.GetStatic("currentActivity");
        }
        AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
        AndroidJavaObject context = activity.Call("getApplicationContext");
        activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
        {
            AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", text);
            Toast.CallStatic("makeText", context, javaString, Toast.GetStatic("LENGTH_SHORT")).Call("show");
        }
            ));
    }

    public static AndroidJavaObject toJavaString(this string CSharpString)
    {
        return new AndroidJavaObject("java.lang.String", CSharpString);
    }
#endif

}

2.这是一个底层的封装好的代码

使用案例如下:

在任何类当中,定义


string aa=//value;
aa.showAsToast();

就可以在真机下方显示

你可能感兴趣的:(自我总结,自我进步)