Hi all,
I have an Android application and, from Java code, I want call C# method in Unity.
First, I extend UnityPlayerActivity so I can see my Unity Project on my Android device, this is run very well.
Second, I want call C# method from Java and play an animation on Unity (free version). I Google a lot for some tutorial.
- I export Java file and create .jar
- I add jar into Assets/Plugins/Android
- I create AndroidManifest.xml and add in the same directory of jar
- the Android package name is the same of Player settings in Unity
Now I show you my code.
This is AvatarUnity.java
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){
// UnityPlayer.UnitySendMessage("Jump", "performJump", "doJump");
runOnUiThread(new Runnable(){
@Override
public void run() {
//
Bridge.doJump();
}
});
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
AvatarThread.start();
}
@Override
public void onBackPressed(){
Activity a = new Activity();
a.onBackPressed();
}
}
this is Bridge.java , the class where I use UnitySendMessage
package com.example.footm;
import com.unity3d.player.UnityPlayer;
public class Bridge {
public static void doJump(){
UnityPlayer.UnitySendMessage("Jump", "performJump", "");
}
}
and, from Unity side, I have this C# class
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
AndroidJavaClass jc;
int soglia = 120;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void performJump(){
GameObject go2 = GameObject.Find ("myHumanoid");
Animator anim = go2.GetComponent<Animator> ();
anim.Play ("jump_1");
}
}
This is the AndroidManifest that I add into Plugins/Android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.footm">
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name=".Bridge"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So, Why when I run my Android Application I see this message on LogCat? SendMessage: object Jump not found!
Someone can help me?