5

我正在 Unity3d 引擎上为 android 开发应用程序。此应用程序必须通过网络套接字连接到我在 PC 上的服务器。我找到了一些适用于 Android 的 Unity3d 插件示例。基于它们,我在 C# 上为 Unity3d 编写了一些代码,在 Java 上为 Android 编写了一些代码。我发现网络操作不能在 Android 应用程序的 UI 线程中运行。所以我必须使用 AsynTask 来处理网络请求。我还尝试从 c# 脚本调用非静态方法,但它们不返回任何数据。只有静态调用从 java 应用程序返回数据。所以我的 AsyncTask 类是静态的。但是当我使用 AsyncTask 作业调用静态函数以通过网络获取数据时,我的应用程序崩溃了。我收到错误。你能帮我解决我的问题吗?我看到了两种解决此问题的方法:1) 更改 Unity3d 的 c# 代码以通过非静态方法调用获取数据。在 Java 代码中将所有方法更改为非静态。2) 更改我的 Java 代码以使用静态方法和静态 AsyncTask。

我的 c# 脚本 AndroidClientPlugin.cs:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class AndroidClientPlugin : MonoBehaviour {
    private float TEST;

    private AndroidJavaClass cls_UnityPlayer;
    private AndroidJavaObject obj_Activity;
    private AndroidJavaClass cls_CompassActivity;
    // Use this for initialization
    void Start () {

        AndroidJNI.AttachCurrentThread();
        cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        cls_CompassActivity = new AndroidJavaClass("com.lab.Android.AndroidClientPlugin");

        cls_CompassActivity.SetStatic<String>("ServerAddressValue", "192.168.1.5");
        cls_CompassActivity.SetStatic<String>("ServerPortValue", "8881");

    }
    void OnGUI() {
        GUI.Label(new Rect(Screen.width / 2 -200, Screen.height / 2, 400,100), "x = " + TEST.ToString());
    }
    void Update()
    {
        if(cls_CompassActivity.CallStatic<bool>("GetData"))
        {
            TEST = cls_CompassActivity.CallStatic<float>("getPosX");
        }
    }
}

我的 Java 脚本 AndroidClientPlugin.java:

package com.lab.Android;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.os.StrictMode;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;

public class AndroidClientPlugin  extends UnityPlayerActivity {

    //Server address parameters
    public  static String ServerAddressValue;
    public  static String ServerPortValue;
    //Tracker parameters
    public  static String vServerName;
    public  static String vSensorNumber;

    private  static SensorData vTaskResult;

    public static cTask BackgroundTask;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //set thread strict mode off
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        vTaskResult = new SensorData(); 
        ServerAddressValue = "192.168.1.5";
        ServerPortValue = "8881";
        vServerName = "Tracker0";
        vSensorNumber = "0";       
        BackgroundTask = new cTask();        
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

    public static boolean GetData()
    {    
        cTaskResult taskResult = new cTaskResult();
        taskResult = BackgroundTask.DoAsyncTask(ServerAddressValue, ServerPortValue, vServerName, vSensorNumber);
        vTaskResult = taskResult.ResultData;
        return taskResult.DataIsReady;
    }


    public static class cTaskResult
    {
        public boolean DataIsReady;
        public SensorData ResultData;
        public cTaskResult()
        {
            DataIsReady = false;
            ResultData = new SensorData();  
        }   
    }

    public static class cTask
    {       
        public cTask()
        {               
        }
        public cTaskResult DoAsyncTask(String serverAddress, String serverPort, String trackerName, String trackerSensorNumber)
        {
            cTaskResult Result = new cTaskResult();     
            GetDataTask Task;
            Task = new GetDataTask();       
            Task.execute(serverAddress, serverPort, trackerName, trackerSensorNumber, Result);          
            try {
                Result = Task.get(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (TimeoutException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return Result;          
        }

        public static class GetDataTask extends AsyncTask<Object, Void, cTaskResult> 
        {
            @Override
            protected void onPreExecute() {
              super.onPreExecute();
            }

            @Override
            protected cTaskResult doInBackground(Object... params) {
                cTaskResult TMPData = new cTaskResult();
                //doing network requests
                //TMPData.DataIsReady = NetClient.getInstance().GetData((String)params[0], (String)params[1], (String)params[2],  Integer.valueOf((String)params[3]), TMPData.ResultData);
                //TMPData is a result of network operations
                TMPData.DataIsReady = true;
                TMPData.ResultData = new SensorData();

                return TMPData;
            }

            @Override
            protected void onPostExecute(cTaskResult result) {
              super.onPostExecute(result);
            }
          }
    }

    public static float getPosX()
    {
        return vTaskResult.posX;
    }   
}

调试消息:

    09-11 12:56:05.514: E/CMarlinMediator(137): Error : MarlinMediator Failed to get TrustedTime
09-11 12:56:05.594: E/CMarlinMediator(137): Error : MarlinMediator Failed to get TrustedTime
09-11 12:56:12.184: E/Adreno200-EGL(7590): <qeglDrvAPI_eglGetConfigAttrib:484>: EGL_BAD_ATTRIBUTE
09-11 12:56:12.184: E/Adreno200-EGL(7590): <qeglDrvAPI_eglGetConfigAttrib:484>: EGL_BAD_ATTRIBUTE
09-11 12:56:12.184: E/Adreno200-EGL(7590): <qeglDrvAPI_eglGetConfigAttrib:484>: EGL_BAD_ATTRIBUTE
09-11 12:56:14.304: E/AndroidRuntime(7590): FATAL EXCEPTION: GLThread 741
09-11 12:56:14.304: E/AndroidRuntime(7590): java.lang.ExceptionInInitializerError
09-11 12:56:14.304: E/AndroidRuntime(7590):     at com.lab.Android.AndroidClientPlugin$cTask.DoAsyncTask(AndroidClientPlugin.java:128)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at com.lab.Android.AndroidClientPlugin.GetData(AndroidClientPlugin.java:79)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
09-11 12:56:14.304: E/AndroidRuntime(7590): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.os.Handler.<init>(Handler.java:121)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
09-11 12:56:14.304: E/AndroidRuntime(7590):     at android.os.AsyncTask.<clinit>(AsyncTask.java:190)
09-11 12:56:14.304: E/AndroidRuntime(7590):     ... 6 more
09-11 12:56:15.194: E/CMarlinMediator(137): Error : MarlinMediator Failed to get TrustedTime
09-11 12:56:15.234: E/CMarlinMediator(137): Error : MarlinMediator Failed to get TrustedTime
4

1 回答 1

2

首先,为什么要在Update函数中调用android代码?因为它将在每一帧中调用。

也许您应该放置一些标志来指示本机代码正在运行,并且不要调用它两次。

关于您的问题,据我所知,必须在 UI-Thread 中调用 AsyncTask 创建和任务运行。问题是:

  1. 在 UnityAndroidClientPlugin.GetData的方法中调用静态方法。Update据我所知,这与android的UI-Thread不是同一个线程。所以你在 Unity 自己的线程中调用它,并且:
  2. 您在 Unity3d 的线程中创建并运行 AsyncTask,因此会出错。

我的解决方案是(你可以试试):

public static void GetData()
{    
    //this will be called on UIThread of Android
    com.unity3d.player.UnityPlayer.currentActivity.runOnUiThread(new Runnable(){
        public void run(){
            cTaskResult taskResult = new cTaskResult();
            taskResult = BackgroundTask.DoAsyncTask(ServerAddressValue, ServerPortValue, vServerName, vSensorNumber);
            vTaskResult = taskResult.ResultData;
                com.unity3d.player.UnityPlayer.currentActivity.SendMessage("YourGameObjectName", "YourMethodName", taskResult.DataIsReady);
            //return taskResult.DataIsReady;    
        }
    });

}
于 2013-10-04T08:21:44.497 回答