After Unity release 2019, you can export Unity Library directly for more flexible integration

Official Unity demo: github.com/Unity-Techn…

Reference links:

www.cnblogs.com/zhxmdefj/p/…

Blog.csdn.net/qq_40028144…

Step 1.

Import the relevant moudle

implementation project(path: ':unityLibrary')   
implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
Copy the code

The aar can be removed

implementation(name: 'UnityAds', ext:'aar')
implementation(name: 'UnityAdsAndroidPlugin', ext:'aar')
Copy the code

Note that the AAR package is lost in the unityLibrary moudle as follows:

Reference: www.cnblogs.com/yongdaimi/p…

(1) App. gradle

repositories { flatDir { dirs 'libs','.. /unityLibrary/libs' }}Copy the code

(2) Pay attention to the unityLibrary add, otherwise still error

repositories {    flatDir { dirs 'libs' }}
Copy the code

Step 2.

Resource file added, or crash

<string name="game_view_content_description">Game view</string>
Copy the code

Step 3.

Unity calls android code in the main thread

Android :process=":Unity"Copy the code

Unity3D interface operation

One, click Event Add

The image can be placed under Assets under the new Texture folder

Select the Image and set the Image, otherwise the Image will not be found after adding the Image to the Image

Remember to Apply

1. Add click events

It is also possible to create a Button directly. Here we create an Image and add an Image, and then we add a Button event to the Image

To create the Image

Add an Image to Image

You can set image position, size, zoom and other parameters

Add a Button component to the Image

 

Create the Scripts folder in Assets to hold the method to create the script file and click the event

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class BtnController : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame Void Update() {} public void clickdBtn() { Also have other way to try {AndroidJavaClass jc = new AndroidJavaClass (" com.com pany. Product. OverrideUnityActivity "); AndroidJavaObject overrideActivity = jc.getstatic <AndroidJavaObject>("instance"); AndroidJavaObject overrideActivity = jc.getstatic <AndroidJavaObject>("instance"); overrideActivity.Call("btnClick"); Appendtotext (" Exception during showhostmainWindow ");} catch (Exception e) {//appendtotext(" Exception during showhostmainwindow"); //appendToText(e.Message); Private void toastText(object STR) {#if UNITY_ANDROID AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); ; AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast"); AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext"); currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => { AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", str.ToString()); Toast.CallStatic<AndroidJavaObject>("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT")).Call("show"); })); #endif } }Copy the code

Select the Image and Click + to add one in the On Click() list of the Button component in the Inspector

Mount the script onto the control

Add the click event method in the script in No Function

Ctrl + S save that

The Android callback method is as follows:

package com.company.product; import android.os.Bundle; import com.unity3d.player.UnityPlayerActivity; public abstract class OverrideUnityActivity extends UnityPlayerActivity { public static OverrideUnityActivity instance =  null; abstract protected void clickdBtn(); // Click @override protected void onCreate(Bundle savedInstanceState) {super.oncreate (savedInstanceState); instance = this; } @Override protected void onDestroy() { super.onDestroy(); instance = null; }}Copy the code

2. Add round images

Script code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Sprites; using System; Public class CircleImage: Image {private int segementCount; public class CircleImage: Image {private int segementCount; Private int circleVertexCount; Protected override void OnPopulateMesh(VertexHelper VertexHelper) {segementCount = 100; circleVertexCount = segementCount + 2; Vertexhelper.clear (); vertexhelper.clear (); vertexhelper.clear (); float width = rectTransform.rect.width; float height = rectTransform.rect.height; Vector4 uv = (overrideSprite ! = null) ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero; Float uvCenterX = (uv.x + uv.z) * 0.5f; float uvCenterX = (uv.x + uv.z) * 0.5f; float uvCenterX = (uv.x + uv.z) * 0.5f; Float uvCenterY = (uv.y + uv.w) * 0.5f; float uvCenterY = (uv.y + uv.w) * 0.5f; Float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleX = (uv.z-uv. X)/width; float uvScaleY = (uv.w - uv.y) / height; Float radian = 2 * mathf.pi/segementCount; float radian = 2 * mathf.pi/segementCount; float radius = rectTransform.pivot.x * width; UIVertex centerVertex = new UIVertex(); UIVertex centerVertex = new UIVertex() centerVertex.position = Vector2.zero; // This coordinate is mainly used as a coordinate reference for other vertices. centerVertex.color = color; centerVertex.uv0 = new Vector2(uvCenterX, uvCenterY); vertexHelper.AddVert(centerVertex); float curRadian = 0; // call the current radian and add the remaining vertices on the arc for (int I = 0; i < circleVertexCount - 1; i++) { UIVertex vertex = new UIVertex(); float x = Mathf.Cos(curRadian) * radius; float y = Mathf.Sin(curRadian) * radius; vertex.position = new Vector2(centerVertex.position.x + x, centerVertex.position.y + y); vertex.color = color; vertex.uv0 = new Vector2(x * uvScaleX + uvCenterX, y * uvScaleY + uvCenterY); // uvCenter +uvCenter vertexhelper.addvert (vertex); curRadian += radian; } for (int I = 1; int I = 1; int I = 1; i < circleVertexCount - 1; i++) { vertexHelper.AddTriangle(i, 0, i + 1); / /}}}}Copy the code

Create an empty object

Just drag the CircleImage script onto it. Add images like Image

Second, add a TAG to the object

Similar to the reference jingyan.baidu.com/article/fdf…

Used to get the object corresponding to the TAG

Public GameObject suspensionAwardPanel = GameObject. FindGameObjectsWithTag [0] (" the tag name ");Copy the code

Third, call another script method

To call the VIBRate () method in the GM from a Collider

public class GM : Public bool vibrate() {bool vibrate = playerprefs.getint ("vibrate", 1) == 1; return vibrate; } } public class Collider : MonoBehaviour {// Whether vibrate 1, default vibrate public bool vibrate() {return GameObject.FindGameObjectsWithTag("InnerBox")[0].GetComponent<GM>().vibrate(); }}Copy the code

Fourth, data storage

PlayerPrefs.SetInt("vibrate", 0); Bool vibrate = playerprefs. GetInt("vibrate", 1) == 1; / / get dataCopy the code