如何通过UnitySendMessage方法从Android java项目中调用Unity C#方法?

时间:2014-10-16 12:09:29

标签: java c# android eclipse unity3d

我创建了一个Unity项目,并将其添加到我的Android布局中。一切都很好。当我尝试使用UnityPlayer.UnitySendMessage(" GameObject","方法","消息")从我的Android项目调用c#方法时,我遇到了问题。我收到以下错误:找不到对象跳转!我读过很多类似的问题但有人帮助我。

  1. 在Unity中我有一个游戏对象调用Jump
  2. c#类的名称是Jump
  3. 我构建一个Android项目并在Eclipse中添加Staging Area文件夹(通过创建新的android项目)
  4. 此项目标记为库
  5. 我在我的库中添加了classes.jar
  6. 我告诉你我的安卓代码:

    package com.example.footm;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    import com.unity3d.player.UnityPlayer;
    import com.unity3d.player.UnityPlayerActivity;
    //Studio dei movimenti del soggetto
    
    public class AvatarUnity extends UnityPlayerActivity {
    
        UnityPlayer mUnityPlayer;
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            MachineState.Avatar_Thread_isRunning = true;
            Thread AvatarThread = new Thread(new Runnable(){
    
                @Override
                public void run() {
                    String start_string = "s";
                    OutputStream os;
                    byte[] byte_start = new byte[start_string.length()];
                    byte_start=start_string.getBytes();
                    try {
    
                         os = MachineState.socket.getOutputStream();
                         os.write(byte_start);
                         os.flush();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        MachineState.Avatar_Thread_isRunning=false;
                        e1.printStackTrace();
                    }
                    byte[] buffer_dati = new byte[1024];
                    int count_bytes_read;
                    int Pressure_back;
                    int Pressure_front;
                    //Valori baseline di pressione di un soggetto in piedi (punta e tacco della scarpa)
                    int soglia_front = 120;
                    int soglia_back = 120;
    
                    InputStream is;
                    try {
                        is = MachineState.socket.getInputStream();
                        while((count_bytes_read = is.read(buffer_dati))!=-1 && MachineState.Avatar_Thread_isRunning){
                            String data = new String(buffer_dati,0,count_bytes_read);
                            if(data.length()==19){
                                Pressure_back = Integer.parseInt(data.substring(10, 13), 16);
                                Pressure_front = Integer.parseInt(data.substring(13, 16), 16);
                                if(Pressure_back>soglia_back && Pressure_front>soglia_front){
    
                                    doJump();
    
    
                                }
    
                            }
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
    
                }
    
            });
            AvatarThread.start();
    
    
        }
    
        @Override
        public void onBackPressed(){
            Activity a = new Activity();
            a.onBackPressed();
        }
    
        public static void doJump(){
            com.unity3d.player.UnityPlayer.UnitySendMessage("Jump", "performJump", "doJump");
        }
    }
    

    这是我的Unity代码:

    using UnityEngine;
    using System.Collections;
    
    public class Jump : MonoBehaviour {
    
        AndroidJavaClass androidClass;
        // Use this for initialization
        void Start () {
            AndroidJNIHelper.debug = true; 
            using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { 
                    jc.CallStatic ("UnitySendMessage", "Jump", "performJump", "doJump"); 
            }
    //      AndroidJNI.AttachCurrentThread ();
    //      androidClass = new AndroidJavaClass("com.example.footm.Avatar");
    
        }
    
        // Update is called once per frame
        void Update () {
    
        }
    
        public void performJump(string message){
            //Void esecuzione salto
            GameObject go2 = GameObject.Find ("myHumanoid");
            Animator anim = go2.GetComponent<Animator> ();
            anim.Play ("jump_1");
        }
    }
    

2 个答案:

答案 0 :(得分:3)

您应该检查当前场景中是否存在名为“Jump”的游戏对象。本质上,

UnityPlayer. UnitySendMessage("Gameobject Name","Method","Message")

答案 1 :(得分:-1)

我认为你使用的是AndroidJavaClass错误的方式。

using (AndroidJavaClass jc = new
AndroidJavaClass("com.unity3d.player.UnityPlayer")) { 
//jc.CallStatic ("UnitySendMessage", "Jump", "performJump", "doJump"); 
jc.CallStatic("doJump");

没有名为&#34; UnitySendMessage&#34;在你的Android代码中