您的位置:首页 > 移动开发 > Unity3D

Some tips to use unity, eclipse....

2015-11-24 12:18 525 查看
F:http://sleepyrea.blogspot.com/2014/04/some-tips-to-use-unity-eclipse.html

Setup:

1. Start with an Android project which will contain all the Android bits.

2. Prepare the Unity3d Android project. Import the Unity project into your workspace like a regular
Android project.

3. RMB on the Unity project, click “Properties”, then go to “Android” and tick the “Is Library?” check
box.

4. RMB on the Android project, click “Properties”, then go to “Android” and click “Add” to add a library
- the Unity project - to the Android one.

5. Still in the “Properties” dialog, go to “Java Build Path” and select the “Libraries” tab. There,
click “Add external JAR” and navigate to the classes.jar file.

6. Ensure both projects target the same SDK.

7. Move the “assets” folder from the Unity project into the Android project. This step has to be repeated
each time your Unity project is rebuilt.

--

Right-click on your 'Unity.app' in the applications window, and select 'Show Package Contents'. This will expand
the .app out into a small filesystem, in which you can find the files you are looking for.

This happens because applications on MacOS are evaluated by Finder as executables, but are actually folders
which get special treatment- the actual executable is buried deep within the package (usually at Contents/MacOS/something). This allows a program to have all its required resources neatly packaged inside itself, without needing to do something convoluted like
uncompressing a tarball of some kind at runtime.

--

Building Android plugins with UnityPlayerActivity



02 Jan 0 Comment(s)

Posted by Jordi

On this post I'm going to explain how to build your own Android plugins on Unity, generate a valid manifest
and how to establish a bidirectional communication between your Android plugin and Unity.

Setup

First of all we need to prepare our environment and link the correct libraries on Eclipse (that's the IDE I'm
going to use on this post).

1. Create an empty Android project on Eclipse and mark your project as libray. Right-button on your project
/ Properties / Android / Is Library. On this tutorial the name of package would be: com.codestalkers.plugin
2. Copy the file classes.jar from your Unity installation folder (Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar) to libs folder on
your Eclipse project.
3. Create a Main class on your Eclipse project. Right-button on your project / New / Class
4. Prepare your code with UnityPlayerActivity on your Main class.

import com.unity3d.player.UnityPlayerActivity;

public class Main extends UnityPlayerActivity {

public static Context mContext;

@Override

protected void onCreate(Bundle bundle)

{

super.onCreate(bundle);

mContext = this;

}

}

5. Export your Eclipse project to your Unity plugins folder: Assets/Plugins/Android/plugin.jar. Right-click
on your project / Export... / JAR file

Modify your Android manifest

We need to create or modify the Android Manifest file on our Unity project. Create or modifiy the file Assets/Plugins/Android/AndroidManifest.xml.
It would look like:
http://schemas.android.com/apk/res/android
" package="com.company.product">

android:label="@string/app_name"

android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">

Communicate from Unity to Android

Call to non-static method

If we need to call a non-static method on our Android plugin, we need to retrieve the current Android activity.
For this reason I saved as static the Android Context, we can easy get this value and call our non-static method. Let assume we need to call the following non-static method on our Java plugin:

import com.unity3d.player.UnityPlayerActivity;

import android.util.Log;

public class Main extends UnityPlayerActivity {

public static Context mContext;

@Override

protected void onCreate(Bundle bundle)

{

super.onCreate(bundle);

mContext = this;

}

public void nonStaticMethod()

{

Log.d("TAG","Non-static method was called");

}

}

The call to nonStaticMethod() method of our plugin from Unity side will be:

public void AndroidCallNonStatic()

{

using (AndroidJavaClass javaClass = new AndroidJavaClass("com.codestalkers.plugin.Main"))

{

using (AndroidJavaObject activity = javaClass.GetStatic("mContext"))

{

activity.Call("nonStaticMethod");

}

}

}

Call to static method

If we're going to call a static method we don't need to get the current Android activity. For example, let's assume we need to call the following static method:

import com.unity3d.player.UnityPlayerActivity;

import android.util.Log;

public class Main extends UnityPlayerActivity {

@Override

protected void onCreate(Bundle bundle)

{

super.onCreate(bundle);

}

public static void StaticMethod()

{

Log.d("TAG","Static method was called");

}

}

The call to StaticMethod() method of our plugin from Unity side will be:

public void AndroidCallStatic()

{

using (AndroidJavaClass javaClass = new AndroidJavaClass("com.codestalkers.plugin.Main"))

{

javaClass.CallStatic("StaticMethod");

}

}

Communicate from Android to Unity

To communicate from our Android plugin to Unity we can pass data with the method UnitySendMessage of UnityPlayer.

import com.unity3d.player.UnityPlayerActivity;

import com.unity3d.player.UnityPlayer;

public class Main extends UnityPlayerActivity {

@Override

protected void onCreate(Bundle bundle)

{

super.onCreate(bundle);

UnityPlayer.UnitySendMessage("GameManager", "HelloFromAndroid", "Hello!");

}

}

There are 3 parameters on this function UnitySendMessage:

1. GameObject name (in the previous example, will send a message to object GameManager, we need to have an object on our scene with this name)
2. Function to call on this GameObject
3. Data to pass on this function

On Unity: create a GameObject called GameManager and add the following C# code on this GameObject:

using UnityEngine;

public class GameManager : MonoBehaviour {

public void HelloFromAndroid(string dataReceived)

{

Debug.Log("Received data from Android plugin: " + dataReceived);

}

}

If we compile this project we will receive the messages on log console. Feel free to modify and adapt the code to your needs.

Thank you for reading!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: