unity3d调用安卓java

时间:2024-09-29 18:41:33

一、unity3d调用java

unity里面想要获取安卓设备的唯一ID,必须通过原生的java接口来获取。

首先创建一个安卓库文件工程,里面新建一个java类,并从UnityPlayerMainActivity类派生,如下代码所示:

public class AndroidPlugin extends UnityPlayerMainActivity
{
    private   static String unitygameobjectName = "_main"; //Unity 中对应挂脚本对象的名称
    private static AndroidPlugin thisActivity=null;
    public final static int FILECHOOSER_RESULTCODE = 19238467;     
    protected static ValueCallback<Uri> _uploadMessages;
    protected static ValueCallback<Uri[]> _uploadCallback;
    protected static String _cameraPhotoPath;
    protected static final String LOG_TAG = "AndroidPlugin";
    private static final String TAG = "AndroidPlugin";
    private int _textureId = 0;
    private SurfaceTexture _surfaceTexture;
    private IjkMediaPlayer player = null;
    private Texture2DExt mTexture2DExt;
    private Texture2D mUnityTexture;
    private FBO mFBO;
    private float[] mMVPMatrix = new float[16];
 
    private MyPluginCallbackListener _listener;
 
    public static Activity getUnityActivity_()
    {
        return thisActivity;//UnityPlayer.currentActivity;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        thisActivity = this;
        super.onCreate(savedInstanceState);  
    }
 
 public static String getAndroidUUID()
    {
        String deviceID = "androiduuid";
        if (ContextCompat.checkSelfPermission(thisActivity, android.Manifest.permission.READ_PHONE_STATE) != 0)
        {
            ActivityCompat.requestPermissions(thisActivity, new String[] {Manifest.permission.READ_PHONE_STATE}, 9999);
        }
        else
        {
            TelephonyManager tm = (TelephonyManager)getUnityActivity_().getSystemService(Context.TELEPHONY_SERVICE);
            deviceID = tm.getDeviceId();
        }
 
        Log.d("androiduuid", "androiduuid is:" + deviceID);
        return deviceID;
    }
}
 

AndroidPlugin类里面添加需要调用的接口,比如:getAndroidUUID,将此类编译成一个jar库文件,复制到Plugins\Android\libs目录下。

c#需要添加以下相关的代码:

    void Awake()
    {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
#elif UNITY_ANDROID
        //   unityClassObj = new AndroidJavaClass(UNTIFY_CLASS);
        AndroidJavaClassObj = new AndroidJavaClass(JAVA_CLASS);
        AndroidJavaObj = AndroidJavaClassObj.CallStatic<AndroidJavaObject>("getUnityActivity_");
#endif
     }
 
 public string GetDeviceID()
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
            try
            {
#if UNITY_ANDROID
                //AndroidJavaObject curA = unityClassObj.GetStatic<AndroidJavaObject>("currentActivity");    
                string aID = SystemInfo.deviceUniqueIdentifier;
                if (AndroidJavaObj != null)
                    aID = AndroidJavaObj.CallStatic<string>("getAndroidUUID");
                return "ANDROID-" + aID;
#elif UNITY_IPHONE
        string iID = GetIphoneADID();
        return "IOS-"+iID;
#endif
            }
            catch (System.Exception e)
            {
                Debug.Log(e);
            }
            return SystemInfo.deviceUniqueIdentifier;
        }
        return SystemInfo.deviceUniqueIdentifier;
    }