Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Local data sharing by Android in Unity

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to realize local data sharing in Android in Unity? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Let me first talk about the principle of local data sharing in Unity combined with Android NDK. As shown in the following figure, after the Unity project joins NDK, the project can be divided into three parts: Android (JAVA), Android + (.so) and Unity (C #). In the previous article, I introduced the principle of mutual call between Android and Unity. Unity3D Research Institute opens Activity and calls JAVA code to pass parameters (XVIII). This approach can only pass String strings to each other, and can be used when passing a small amount of data. If it is a large amount of data, it will be a bit limited. In fact, we can use the code of Cpicket + as a transit station to realize the data reference and sharing between the two sides.

First of all, download the latest installation package of NDK from the official website. The installation package is divided into three versions. Here I download the MAC OS version, and the latest version has reached R8.

Download address: http://developer.android.com/sdk/ndk/index.html

OK, we create an Android project in Eclipse and create a jni folder under the res and src level folder directory. Then create two code files in the folder, C and C++, and a configuration file Android.mk.

C. C code is divided into two methods. First, the method Java_com_xys_UnityTestActivity_TestAddInt is missed to the Java code. Jint indicates that the return value of this method is shaping, and the data types can also be jlong, jfloat, jdouble, jobject, jboolean, jbyte, jchar, jshort. I won't explain them one by one. In the method name, java begins with the standard usage, com_xys represents the package name of the current program, UnityTestActivity represents the current class, and TestAddInt represents the method name, which is called in Android, which implements a simple integer addition operation. When it comes to the second method, int addInt (), which is reserved for the C # language in Unity, its structure is different from the above and cannot be called in Java code, which also implements the operation of shaping and adding.

First, C's file c. C.

[code] c#/cpp/oc code: the java code in 01#include02#include0304//Android calls this method 05jint06Java_com_xys_UnityTestActivity_TestAddInt (JNIEnv* env, jobject thiz, jint ajint b) 07 {08return addInt (ajint b); 09} the C# code in 1011//Unity calls this method 12int addInt (int a, int b) 13 {14return a + bbot 15}

Looking at the C++ file, it is similar to the principle of calling C file, but it is worth noting that C++ must write the method that needs to be called in the extern "C" {}, otherwise it cannot be called.

Cplus.cpp

[code] c#/cpp/oc code: 01#include02#include0304// declares a class 05class MyClass06 {07public:08static float add (float a, float b) 09 {10return a + b * * 11} 1213}; 1415 16extern / externally called methods need to be written here 16extern "C" 17 {1819jfloat20Java_com_xys_UnityTestActivity_TestAddFloat (JNIEnv* env, jobject thiz, jfloat a dint jfloat b) 21 {22return MyClass::add (Azov b) 23} 2425float addFloat (float a float b) 26 {27return MyClass::add (a float b); 28} 2930}

Let's take a look at the third configuration file, the two more important variables in the file, LOCAL_MODULE, represent the generated .so name LOCAL_SRC_FILES represents the file that needs to be compiled, and if it is more than one Cmax Cure + file, you need to use\ to separate it.

Android.mk

C#/cpp/oc Code: 01LOCAL_PATH: = $(call my-dir) 0203include $(CLEAR_VARS) 0405LOCAL_MODULE: = xuanyusong06LOCAL_SRC_FILES: =\ 07c.c\ 08cplus.cpp0910include $(BUILD_SHARED_LIBRARY)

OK, at this point, the preparation of the Cmax Cure + code is done, so let's learn how to package c. C and cplus.cpp a C file and a C++ file into a .so file. First open the terminal, cd to the jni directory you just created, and then execute the ndk-build command in the DNK package you downloaded at the beginning. You can find ndk-build directly in android-ndk-r8 and drag it to the terminal. If there are no errors in the code, the .so file is compiled successfully as shown in the figure.

Let's take a look at the directory structure of the current Android project. Libs-> armeabi-> libxuanyusong.so is the .so file just compiled. The lib in front of xuanyusong.so is added by default, so don't panic.

Let's write the Java code to learn how to call the C _ Candle codes in the java code. The code is relatively simple to call the methods of C and C++ in the OnCreat () method, and a Toast pops up to display in the interface.

UnityTestActivity.java

Java Code: 01package com.xys;0203import android.os.Bundle;04import android.widget.Toast;0506import com.unity3d.player.UnityPlayerActivity;0708public class UnityTestActivity extends UnityPlayerActivity09 {1011@Override12public void onCreate (Bundle savedInstanceState) 13 {14super.onCreate (savedInstanceState) 1516int / two methods in .so are called here and are displayed on the screen 17Toast.makeText (getApplicationContext (), "shaping =" + TestAddInt (1)) + "floating point =" + TestAddFloat (1.5f, 1.1f), 18Toast.LENGTH_LONG). Show (); 1920} 2122Universe / TestAddInt method 23public native int TestAddInt in declaration c (int a, int b); 2425pm / declaration TestAddFloat method 26public native float TestAddFloat in cplus.cpp (float a, float b) 2728static29 {30max / load .so file 31System.loadLibrary ("xuanyusong"); 32} 3334}

OK, we have finally completed the Android project. Then we need to copy the project to the Android plug-in of Unity. Who doesn't know how to make the plug-in? No friends, please read the previous article, I will not repeat it here. As shown in the following figure, the Android plug-in has been made and placed in Unity. The directory structure is as follows.

Write a test.cs script to access the libxuanyusong.so file directly through the C# script and hang the test.cs directly on the camera. Use [DllImport ("xuanyusong")] to introduce the .so method. Here, be sure to remove the lib and the suffix .so of the .so file name, and finally display the data on the screen through GUI.

Test.cs

[code] c#/cpp/oc code: 01using UnityEngine;02using System.Collections;03using System.Runtime.InteropServices;04public class test: MonoBehaviour {0506int I = 0; 07float f = 0.0f politics 0809max / reference C, method 10 in C++ [DllImport ("xuanyusong")] 11private static extern int addInt (int aline int b); 12 [DllImport ("xuanyusong")] 13private static extern float addFloat (float a float b); 1415void Start () 16 {17max / summing function 18i = addInt in the calling method 19f = addFloat (1.0faddFloat 2.2f); 2021} 2223void OnGUI () 24 {2526max / the added information is displayed on the screen 27if (I! = 0) 28GUILayout.Label ("use c =" + I); 2930if (f! = 0.0f) 31GUILayout.Label ("use cplus =" + f); 32} 33}

All the work is done, and we package and compile the real machine, starting with popping up the resulting Toast when calling .so in Android.

Then there is the result of drawing on the screen through GUI after calling .so in Unity.

This is the answer to the question about how to achieve local data sharing in Android in Unity. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report