unity-doc-sdk

android

TestAndroid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;

public class InvokeAndroid : MonoBehaviour
{
private AndroidJavaClass javaClass;
private AndroidJavaObject javaObject;

void Start()
{
// 权限请求
if (Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}

javaClass = new AndroidJavaClass("com.example.test.Test");
javaObject = new AndroidJavaObject("com.example.test.Test");
}

/// <summary>
/// 调用android静态方法
/// </summary>
public void SetLog()
{
javaClass.CallStatic("SetLOG", "UnityInvoke_SetLog");
}

/// <summary>
/// 修改android静态字段
/// </summary>
public void SetLogField()
{
javaClass.SetStatic("LOG", "UnityInvoke_SetLogField");
}

/// <summary>
/// 调用android实例方法
/// </summary>
public void SetName()
{
javaObject.Call("SetName", "UnityInvoke_SetName");
}

/// <summary>
/// 设置android成员变量
/// </summary>
public void SetNameField()
{
javaObject.SetStatic("name", "UnityInvoke_SetNameField");
}

/// <summary>
/// 调用android静态方法
/// </summary>
/// <returns></returns>
public string GetLOG()
{
return javaClass.CallStatic<string>("GetLOG");
}

/// <summary>
/// 获取android静态字段
/// </summary>
/// <returns></returns>
public string GetLogField()
{
return javaClass.GetStatic<string>("LOG");
}

/// <summary>
/// 调用android成员方法
/// </summary>
/// <returns></returns>
public string GetName()
{
return javaObject.Call<string>("GetName");
}

/// <summary>
/// 获取android成员字段
/// </summary>
/// <returns></returns>
public string GetNameField()
{
return javaObject.Get<string>("name");
}

/// <summary>
/// android调用unity
/// </summary>
public void HelloWorld(string str)
{
Debug.LogFormat("HelloWorld" + str);
}
}
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.test;

import org.junit.Test;

import static org.junit.Assert.*;

import android.util.Log;

public class Test {
public static String LOG = "LOG";

public String name;

public static void SetLOG(String log) {
LOG = log;
Log.d(LOG, "SetLOG:" + log);
}

public static String GetLOG() {
Log.d(LOG, "GetLOG:" + LOG);
return LOG;
}

public void SetName(String name) {
this.name = name;
Log.d(LOG, "SetName:" + name);
}

public String GetName() {
Log.d(LOG, "GetName:" + name);
return this.name;
}

// 调用unity实例上的方法
public void HelloWorld() {
Log.d(LOG, "HelloWorld:");
UnityPlayer.UnitySendMessage("Canvas", "HelloWorld", "AndroidInvoke");
}
}

ios

TestIos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class InvokeIos : MonoBehaviour
{
#if UNITY_IPHONE
[DllImport("__Internal")]
static extern void IOSLog(string message);
#endif

public void InvokeIosLog()
{
#if UNITY_IPHONE
IOSLog("UnityInvoke_InvokeIosLog");
#endif
}

/// <summary>
/// android调用unity
/// </summary>
public void HelloWorld(string str)
{
Debug.LogFormat("HelloWorld" + str);
}
}
test.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#import <Foundation/Foundation.h>

extern "C" {
void IOSLog(const char *message);
}

void IOSLog(const char *message) {
NSString* str = [[NSString alloc]initWithUTF8String:message];
NSLog(str);

// 调用unity代码
UnitySendMessage("Canvas", "HelloWorld", str.UTF8String);
}

构建设置

IosBuildSetting.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public class IosBuildSetting : MonoBehaviour
{
[PostProcessBuild]
private static void OnBuildFinish(BuildTarget buildTarget, string buildPath)
{
if (buildTarget == BuildTarget.iOS)
{
SetXCodeProject(buildPath);
}
}

public static void SetXCodeProject(string buildPath)
{
PBXProject project = new PBXProject();
project.ReadFromString(File.ReadAllText(PBXProject.GetPBXProjectPath(buildPath)));

string targetGuid = project.GetUnityFrameworkTargetGuid();

// 添加库
project.AddFrameworkToProject(targetGuid, "AVKit.framework", false);

// 添加权限
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(Path.Combine(buildPath, "Info.plist")));
plist.root.SetString("NSCameraUsageDescription", "请求相机权限!");
plist.WriteToFile(Path.Combine(buildPath, "Info.plis"));

// 设置构建属性
project.SetBuildProperty(targetGuid, "CODE_SIGN_IDENTITY", "iphone Developer");

// 设置变异标识
string fileGuid = project.FindFileGuidByProjectPath("MainApp/main.mm");
project.SetCompileFlagsForFile(targetGuid, fileGuid, new List<string>(){"xxx"});

project.WriteToFile(PBXProject.GetPBXProjectPath(buildPath));

}
}