Unity3d 与 Android之间的互相调用

Unity3d 与 Android之间的互相调用

Unity3d 开发的过程中,总会遇到各种调用Android的情况,本文以Eclipse IDE开发

  1. 引入classes.jar包,路径:打开文件位置->Data->PlaybackEngines->androidplayer->Variations->mono->release->classes->classes.jar。

  2. 创建的MainActivity需要继承UnityPlayerActivity。cs脚本中调用Android代码

    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    
  3. 调用Android常用的api 如下Unity3d 与 Android之间的互相调用_第1张图片

  4. Android调用Unity3d代码

    UnityPlayer.UnitySendMessage("Main Camera", "OnMessage", "Hello u3d");  //Main Camera 是Scenes中的Objtct,OnMessage 是方法,最后传入参数
    
  5. 创建Eclipse工程, Package Name的名字需要与Unity3d 中的Package Name 相同
    Unity3d 与 Android之间的互相调用_第2张图片

  6. 编写MainActivity类

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    import com.unity3d.player.UnityPlayer;
    import com.unity3d.player.UnityPlayerActivity;
    
    public class MainActivity extends UnityPlayerActivity {
           
    
    	Context mContext = null;
    	String strData = "";
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
           
    		super.onCreate(savedInstanceState);
    		//setContentView(R.layout.activity_main);
    		mContext = this;
    		
    		UnityPlayer.UnitySendMessage("Main Camera", "OnMessage", "Hello u3d");	//调用Unity3d方法
    	}
    	
    	public void SetData(String str)
    	{
           
    		strData = str;
    	}
    	
    	public String GetData()
    	{
           
    		return strData;
    	}
    }
    
  7. 导出jar包,只需要导出src目录即可
    Unity3d 与 Android之间的互相调用_第3张图片

  8. Unity3d 导入jar包,放置路径Assets->Plugins->Android->libs,导入AndroidManifest.xml文件到Assets->Plugins->Android,并修改AndroidManifest.xml文件,特别需要注意android:icon!!!

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mygame"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="21" />
      
        <application
            android:allowBackup="true"
            android:icon="@drawable/app_icon"
            android:label="@string/app_name" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
              
                <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
            </activity>
        </application>
    
    </manifest>
    
  9. Unity3d 编写cs代码,添加到Main Camera上

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Launch : MonoBehaviour {
           
    
        private AndroidJavaObject jo;
    
        // Use this for initialization
        void Start () {
           
            Debug.Log("Launch");
            if (Application.platform == RuntimePlatform.Android)
            {
           
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                if (jc != null)
                    jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            }
            SetData("abc");
            GetData();
        }
    	
    	// Update is called once per frame
    	void Update () {
           
    		
    	}
    
        public void OnMessage(string str)
        {
           
            Debug.Log("OnMessage " + str);
        }
    
        public void SetData(string str)
        {
           
            if (jo != null)
                jo.Call("SetData", str);
        }
    
        public void GetData()
        {
           
            if (jo != null)
            {
           
                string str = jo.Call<string>("GetData");
                Debug.Log("GetData " + str);
            }
        }
    }
    

Demo参考地址:https://download.csdn.net/download/wangningzk123/12581772

你可能感兴趣的:(Unity3d,unity,android,eclipse,java)