[Unity3D] Unity + Android interaction tutorial -- getting the mobile phone up & quot;

Source: Internet
Author: User

If you want to use Unity to implement a QR code scan function, find a plug-in on the Internet and find a plug-in that seems to be called easycode.pdf, but download it and use it. This is really bad. An error will be reported as soon as it is imported and run, after debugging the error code, run the "Discover" button again without responding. after retrying several times, the system still failed to check the source code. The result showed that only the IOS part was implemented, but the Android part was not, I have already been running ..., yes! If you want to test it on an IOS real machine, you still have a problem. If you don't know Android, you can't learn it, it cannot be implemented !!! First, you have to learn about the simple interaction between Android and Unity. Here is a detailed tutorial! For beginners to learn, of course, there are also tutorials in this area on the Internet. If you do not pay attention to some areas, you will still encounter troubles. Below are detailed steps! For more tutorials, please pay attention to my Weibo!

Effect: The Unity shown here calls the Android native method. click the button on the Unity interface to vibrate the mobile phone! For the most basic interaction, refer to this article. The basic interaction between Unity and Android is achieved on this basis. In fact, it is almost the same! It may be repeated. When you consolidate yourself, haha! Test method: a real machine test is required !!! Implementation steps: 1. Create an Android project 1. Create a project

2. Import the jarJar directory of Unity: installation path of unity \ Editor \ Data \ PlaybackEngines \ androidplayer \ bin classes. jar


3. Open MainActivity. java, import various packages, and write your own functions.
Package com. example. clickshake; import android. OS. bundle; import com. unity3d. player. unityPlayerActivity; import android. app. service; import android. OS. vibrator; // vibration Package public class MainActivity extends UnityPlayerActivity {private Vibrator mVibrator01; // declare a Vibrator object @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} // The vibration function public void ClickShake () {mVibrator01 = (Vibrator) getApplication (). getSystemService (Service. VIBRATOR_SERVICE); mVibrator01.vibrate (new long [] {, 10,},-1); // you can customize the entire building mode, shake only once} // judge the size public int Max (int a, int B) {if (a> B) return a; return B ;}}

Add unityActivity so that MainActivity inherits UnityPlayerActivity and delete the SetContentView function (otherwise, only HelloWorld is displayed when the program is started on the mobile phone ), here, there are two self-defined functions mainly called with no parameters above the vibrator's user-defined functions, and there is also a function with a return value with a parameter comparison size, two different functions are called in Unity for comparison! 4. Modify the XML file and Add the vibration permission. Select AndroidManifest. xml and click Permissions-> Add UserPermission-> android. perssion. VIBRATE.
5. Export the jar package and right-click the project root directory-> Export



Click Finish! 2. Create a Unity project 1. create a Plugins/Android folder under Assets. This folder is a special folder. It is used to store Android package files, so that you can call the Android interface to import the xml, jar, res, put the libs file under the Android File

2. Write Unity to call Android code
Using UnityEngine; using System. collections; public class NewBehaviourScript: MonoBehaviour {private AndroidJavaObject activity; private int res = 0; // Use this for initializationvoid Start () {AndroidJavaClass jc = new AndroidJavaClass ("com. unity3d. player. unityPlayer "); activity = jc. getStatic <AndroidJavaObject> ("currentActivity"); res = activity. call <int> ("Max", new object [] {10, 20});} void OnGUI () {GUI. label (new Rect (20, 20,300, 20), "the Android method called: Find the larger number in 10 and 20:" + res. toString (); GUI. label (new Rect (20, 70,300, 20), "http://blog.csdn.net/dingxiaowei2013"); if (GUI. button (new Rect (Screen. width/2-20, Screen. height/2 + 20,100, 40), "click shake") {activity. call ("ClickShake ");}}}

It indicates that AndroidJavaObject is a java object, which obtains the java view and then calls the custom method. (Note: The reason why the parameter here is "com. unity3d. player. unityPlayer "and" currentActivity ", because when the jar file is generated, MainActivityclass has been specified to inherit UnityPlayerActivity, and MainActivity is specified as Main class in XML)
Activity. call <int> ("", ""); this calls a method with a returned value. int Is a return type, activity. call (""); Call a method that does not return a value or has no parameters.
3. Importent)

4. I will not upload the test on the real machine. It is probably a button that can shake the phone by clicking it. There is also a label that displays the comparison result values of 10 and 20.
More tutorials welcome to Weibo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.