unity转OPPO小游戏
付心武士 Lv3

已知问题

  • unity默认字体在OPPO小游戏上不支持中文,英文正常显示,中文直接不显示
  • OPPO小游戏打包工具生成的包名为:com..。Company Name 和Product Name为unity-Player Settings中设置的包名和应用名。可以通过修改OPPO小游戏目录中manifest.json文件然后重新导出rpk实现包名的修改。

Unity中调用OPPO平台API

  • unity项目Assets/Plugins目录下新建后缀为.jslib文件
  • .jslib文件实现OPPO平台API的调用,并回调C#文件

.jslib文件:

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
mergeInto(LibraryManager.library, {

InitSDK: function (adUnitId, type) {
const qg = window.qg;//qg OPPO 小游戏平台提供的全局对象
if (qg) {
console.log("-------window.qg 存在")
var systemInfo = qg.getSystemInfoSync();
console.log(systemInfo);
switch (Pointer_stringify(type)) {
case "banner":
var bannerAd = qg.createBannerAd({
adUnitId: Pointer_stringify(adUnitId), //字符串需要转换一下
style: {
left: 0,
top: systemInfo.screenHeight - 100 - 10,
width: systemInfo.screenWidth,
height: 50
}
});
if (!window.OPPOAD) {
window.OPPOAD = {};
}
window.OPPOAD.bannerAd = bannerAd;
break;
case "video":
var videoAd = qg.createRewardedVideoAd({
adUnitId: Pointer_stringify(adUnitId)
})
if (!window.OPPOAD) {
window.OPPOAD = {};
}
window.OPPOAD.videoAd = videoAd;
break;
case "gameBanner":
// var gameBannerAd = qg.createGameBannerAd({
// adUnitId: Pointer_stringify(adUnitId)
// })
break;
default:
break;
}
} else {
SendMessage('BtnRoot', 'CallBack', 'init failed mybe not OPPO-quickgame'); //组件名,方法名,参数
}
},

JSShowBannerAd: function () {
if (window.OPPOAD && window.OPPOAD.bannerAd) {
const bannerAd = window.OPPOAD.bannerAd;
console.log("-------bannerAd.show()");
bannerAd.show();
bannerAd.onLoad(function () {
console.log('banner 广告加载成功')
SendMessage('BtnRoot', 'CallBack', 'bannerAd load success');
});
bannerAd.onError(function (err) {
console.log("this.bannerAd onError", res);
});
} else {
console.log("------- no bannerAd");
};

},

JSHideBannerAd: function () {
if (window.OPPOAD && window.OPPOAD.bannerAd) {
const bannerAd = window.OPPOAD.bannerAd;
console.log("-------bannerAd.hide()");
bannerAd.hide();
bannerAd.onHide(function () {
console.log('banner 广告隐藏')
SendMessage('BtnRoot', 'CallBack', 'bannerAd hide');
});
} else {
console.log("------- no bannerAd");
};
},

JSShowVideoAd: function () {
if (window.OPPOAD && window.OPPOAD.videoAd) {
const videoAd = window.OPPOAD.videoAd;
videoAd.load();
videoAd.onLoad(function () {
console.log('激励视频加载成功')
videoAd.show()
});
videoAd.onClose(function(res) {
if (res.isEnded) {
console.log('激励视频广告完成,发放奖励');
SendMessage('BtnRoot', 'CallBack', 'videoAd isEnded, Rewarded');
} else {
console.log('激励视频广告取消关闭,不发放奖励');
SendMessage('BtnRoot', 'CallBack', 'videoAd cancle, no Rewarded');
}
});
} else {
console.log("------- no videoAd");
};
},

JSVibrateShort: function () {//震动
if (window.qg) {
qg.vibrateShort({
success: function (res) { },
fail: function (res) { },
complete: function (res) {
console.log("-------qg.vibrateShort complete");
}
});
};
},

});
  • C#脚本中调用.jslib文件中的方法

C#脚本:

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using UnityEngine.UI;

public class SDKHelper : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void InitSDK(string id, string tag);
[DllImport("__Internal")]
private static extern void JSShowBannerAd();
[DllImport("__Internal")]
private static extern void JSHideBannerAd();
[DllImport("__Internal")]
private static extern void JSShowVideoAd();
[DllImport("__Internal")]
private static extern void JSVibrateShort();

[SerializeField] private Text tipText = default;
private string bannerAdID = "355963"; //Banner广告
private string videoAdID = "355959"; //激励视频广告
private string gameBannerAdID = "355966";//互推盒子广告

//开屏广告 无需技术接入,创建开屏广告位,并更新游戏包即可自动开通开屏广告。

void Start()
{
InitAD();
}

private void InitAD()
{
InitSDK(bannerAdID, "banner");
InitSDK(videoAdID, "video");
InitSDK(gameBannerAdID, "gameBanner");
}
#region 按钮点击事件
/**banner广告*/
public void ShowBannerAd()
{
Debug.Log("~~~~~~ ShowBannerAd");
JSShowBannerAd();
}

public void HideBannerAd()
{
Debug.Log("~~~~~~ HideBannerAd");
JSHideBannerAd();
}
/**激励视频广告*/
public void ShowVideoAd()
{
Debug.Log("~~~~~~ ShowVideoAd");
JSShowVideoAd();
}
/**互推盒子横幅广告*/
public void ShowGameBannerAd()
{
Debug.Log("~~~~~~ ShowGameBannerAd");
}

public void HideGameBannerAd()
{
Debug.Log("~~~~~~ HideGameBannerAd");
}
/**互推盒子九宫格广告*/
public void ShowGamePortalAd()
{
Debug.Log("~~~~~~ ShowGamePortalAd");
}

public void HideGamePortalAd()
{
Debug.Log("~~~~~~ HideGamePortalAd");
}
/**互推盒子抽屉广告*/
public void ShowGameDrawerAd()
{
Debug.Log("~~~~~~ ShowGameDrawerAd");
}

public void HideGameDrawerAd()
{
Debug.Log("~~~~~~ HideGameDrawerAd");
}
#endregion


public void CallBack(string str) {
Debug.Log("~~~~~~~~ 我收到了JS层的调用: " + str);
tipText.text = str;
}
}

安装OPPO小游戏工具
1
npm install -g @oppo-minigame/cli

执行

1
quickgame -V   

显示版本号则表示安装成功

unity发布WebGL项目

在unity发布的WebGl项目目录下执行

  • unity安装WebGl Build Support
  • Player Settings设置OPPO小游戏对应的包名
  • Resolution and Presentation 选项卡下WebGL Template选择Minimal
  • Other Settings选项卡下取消勾选Auto Graphics Api,Graphics Api添加WebGl1.0,删除WebGL 2.0选项
  • publish Setting选项卡下Compression Format修改为Disabled
    发布WebGL项目

在发布出的WebGL项目下执行

1
quickgame unity

会在同级目录下生成quickgame目录

真机调试

OPPO手机安装 OPPO 小游戏调试器

将quickgame/dist目录下的rpk文件拷贝到手机根目录下games目录中,没有games目录的新建gamse目录

打开小游戏调试器,选择“OPPO小游戏”,找到对应小游戏点击“秒开”

Chrome查看日志

手机与电脑在同一局域网下

devtools://devtools/bundled/inspector.html?v8only=true&ws=192.168.0.13:12345/00010002-0003-4004-8005-000600070008

192.168.0.13 是手机IP,端口号12345或12346

unity文档

OPPO文档