-
Notifications
You must be signed in to change notification settings - Fork 10
/
ui.gui_script
359 lines (330 loc) · 14 KB
/
ui.gui_script
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
local dirtylarry = require "dirtylarry/dirtylarry"
local log_helper = require("example.log")
local sys_name = sys.get_sys_info().system_name
local gameIDs = {
["Android"] = "1401815",
["iPhone OS"] = "1425385"
}
local BACK_KEY = hash("back")
local REWARDED = "rewardedVideo"
local INTERSTITIAL = "video"
local BANNER = "banner"
--log logic
local gprint = print
local log = {}
local text = ""
local LINES = 7
_G.print = function(...)
gprint(...)
local args = {...}
local num = #log+1
log[num] = "--"
for k, v in pairs(args) do
log[num] = log[num] .. tostring(v) .. " "
end
log[num] = log[num] .. "\n"
text = ""
if num > LINES then
table.remove(log, 1)
end
for k, v in pairs(log) do
text = text .. v
end
end
function update()
gui.set_text(gui.get_node("console"), text)
end
-- end log logic
local COLOR = {
[true] = vmath.vector4(1, 1, 1, 1),
[false] = vmath.vector4(1, 0, 0, 1)
}
local function update_ui(self)
local is_initialized = unityads.is_initialized()
gui.set_enabled(gui.get_node("idfa/larrybutton"), not is_initialized)
gui.set_enabled(gui.get_node("type"), is_initialized)
if self.ad_type then
gui.set_enabled(gui.get_node("banner_group"), false)
gui.set_color(gui.get_node("load_video/larrylabel"), COLOR[not self.is_ready[self.ad_type]])
gui.set_color(gui.get_node("show_video/larrylabel"), COLOR[self.is_ready[self.ad_type] == true])
if self.ad_type == REWARDED then
gui.set_enabled(gui.get_node("load_video/larrybutton"), true)
gui.set_enabled(gui.get_node("show_video/larrybutton"), true)
elseif self.ad_type == INTERSTITIAL then
gui.set_enabled(gui.get_node("load_video/larrybutton"), true)
gui.set_enabled(gui.get_node("show_video/larrybutton"), true)
elseif self.ad_type == BANNER then
gui.set_enabled(gui.get_node("load_video/larrybutton"), false)
gui.set_enabled(gui.get_node("show_video/larrybutton"), false)
gui.set_enabled(gui.get_node("banner_group"), true)
gui.set_color(gui.get_node("show_banner/larrylabel"), COLOR[self.is_ready[BANNER]])
end
else
gui.set_enabled(gui.get_node("load_video/larrybutton"), false)
gui.set_enabled(gui.get_node("show_video/larrybutton"), false)
gui.set_enabled(gui.get_node("banner_group"), false)
end
end
local function unity_ads_callback(self, message_id, message)
print(log_helper.get_msg_log(message_id, message))
-- message = {event = int, ...}
if message_id == unityads.MSG_INIT then
if message.event == unityads.EVENT_COMPLETED then
-- message = {placement_id = "string", ...}
elseif message.event == unityads.EVENT_SDK_ERROR then
-- message = {code = int, error = "error message string"}
if message.code == unityads.ERROR_INTERNAL then
-- initialization failed due to environment or internal services
elseif message.code == unityads.ERROR_INVALID_ARGUMENT then
-- initialization failed due to invalid argument(e.g. game ID)
elseif message.code == unityads.ERROR_AD_BLOCKER_DETECTED then
-- initialization failed due to url being blocked
end
elseif message.event == unityads.EVENT_JSON_ERROR then
-- message = {error = "error message string"}
end
elseif message_id == unityads.MSG_SHOW then
if message.event == unityads.EVENT_COMPLETED then
-- message = {placement_id = "string"}
-- An event that indicates that the ad was played entirely.
elseif message.event == unityads.EVENT_SKIPPED then
-- message = {placement_id = "string"}
-- An event that indicates that the user skipped the ad.
elseif message.event == unityads.EVENT_START then
-- message = {placement_id = "string"}
-- UnityAds has started to show ad with a specific placement.
self.is_ready[message.placement_id] = nil
elseif message.event == unityads.EVENT_CLICKED then
-- message = {placement_id = "string"}
-- UnityAds has received a click while showing ad with a specific placement.
elseif message.event == unityads.EVENT_SDK_ERROR then
-- message = {code = int, error = "error message string", placement_id = "string"}
if message.code == unityads.ERROR_NOT_INITIALIZED then
-- show failed due to SDK not initialized.
elseif message.code == unityads.ERROR_NOT_READY then
--show failed due to placement not being ready.
elseif message.code == unityads.ERROR_VIDEO_PLAYER then
-- show failed due to video player.
elseif message.code == unityads.ERROR_INVALID_ARGUMENT then
-- show failed due to invalid arguments.
elseif message.code == unityads.ERROR_NO_CONNECTION then
-- show failed due to internet connection.
elseif message.code == unityads.ERROR_ALREADY_SHOWING then
-- show failed due to ad is already being showen.
elseif message.code == unityads.ERROR_INTERNAL then
-- show failed due to environment or internal services.
elseif message.code == unityads.ERROR_TIMEOUT then
-- Error related to an Ad being unable to show within a specified time frame
end
elseif message.event == unityads.EVENT_JSON_ERROR then
-- message = {error = "error message string"}
end
elseif message_id == unityads.MSG_LOAD then
if message.event == unityads.EVENT_LOADED then
-- message = {placement_id = "string"}
-- Load request has successfully filled the specified placementId with an ad that is ready to show.
self.is_ready[message.placement_id] = true
elseif message.event == unityads.EVENT_SDK_ERROR then
-- message = {code = int, error = "error message string", placement_id = "string"}
if message.code == unityads.ERROR_NOT_INITIALIZED then
-- Error related to SDK not initialized
elseif message.code == unityads.ERROR_INTERNAL then
-- Error related to environment or internal services
elseif message.code == unityads.ERROR_INVALID_ARGUMENT then
-- Error related to invalid arguments
elseif message.code == unityads.ERROR_NO_FILL then
-- Error related to there being no ads available
elseif message.code == unityads.ERROR_TIMEOUT then
-- Error related to there being no ads available
end
elseif message.event == unityads.EVENT_JSON_ERROR then
-- message = {error = "error message string"}
end
elseif message_id == unityads.MSG_BANNER then
if message.event == unityads.EVENT_LOADED then
-- message = {placement_id = "string"}
-- Banner is loaded and ready to be placed in the view hierarchy.
self.is_ready[message.placement_id] = true
gui.set_text(gui.get_node("banner/larrylabel"), "Unload Banner")
elseif message.event == unityads.EVENT_LEFT_APPLICATION then
-- message = {placement_id = "string"}
-- Banner links outside the application.
elseif message.event == unityads.EVENT_CLICKED then
-- message = {placement_id = "string"}
-- Banner is clicked.
elseif message.event == unityads.EVENT_DID_SHOW then
-- message = {placement_id = "string"}
-- Banner has shown.
elseif message.event == unityads.EVENT_SDK_ERROR then
-- message = {code = int, error = "error message string", "placement_id", placementId}
if message.code == unityads.ERROR_UNKNOWN then
-- Unknown error
elseif message.code == unityads.ERROR_NATIVE then
-- Error related to native
elseif message.code == unityads.ERROR_WEBVIEW then
-- Error related to webview
elseif message.code == unityads.ERROR_NO_FILL then
-- Error related to there being no ads available
end
elseif message.event == unityads.EVENT_JSON_ERROR then
-- message = {error = "error message string"}
end
elseif message_id == unityads.MSG_IDFA then
if message.event == unityads.EVENT_NOT_SUPPORTED then
-- IDFA isn't supported
elseif message.event == unityads.EVENT_STATUS_AUTORIZED then
elseif message.event == unityads.EVENT_STATUS_DENIED then
elseif message.event == unityads.EVENT_STATUS_NOT_DETERMINED then
elseif message.event == unityads.EVENT_STATUS_RESTRICTED then
elseif message.event == unityads.EVENT_JSON_ERROR then
-- message = {error = "error message string"}
end
end
update_ui(self)
end
function init(self)
if not unityads then
return
end
self.is_ready = {}
msg.post(".", "acquire_input_focus")
update_ui(self)
if unityads then
unityads.set_debug_mode(true)
print("VERSION: ", unityads.get_version(), "DEBUG:", unityads.get_debug_mode())
print("isInitialized:", unityads.is_initialized(),"isSupported:", unityads.is_supported())
unityads.set_callback(unity_ads_callback)
end
end
function final(self)
print("Final! Remove Callback!")
if unityads then
unityads.set_callback()
end
end
local function init_unity_ads(self, appId)
if unityads and appId then
if not unityads.is_initialized() then
print("Init SDK with id", appId)
unityads.initialize(appId, unity_ads_callback, true)
else
print("UnityADS already inited. Just set callback one more time")
unityads.set_callback(unity_ads_callback)
end
end
end
local function set_callback(self)
if unityads then
print("setCallback(unity_ads_callback)")
unityads.set_callback(unity_ads_callback)
end
end
local function remove_callback(self)
if unityads then
print("setCallback(nil)")
unityads.set_callback(nil) -- same as unityads.set_callback()
end
end
local function loadbanner(self)
if unityads then
if not self.is_ready[BANNER] then
print("loadBanner('banner')")
unityads.load_banner(BANNER)
else
self.is_ready[BANNER] = nil
self.is_show_banner = false
print("unloadBanner()")
gui.set_text(gui.get_node("show_banner/larrylabel"), "Show Banner")
gui.set_color(gui.get_node("show_banner/larrylabel"), vmath.vector4(1, 0, 0, 1))
gui.set_text(gui.get_node("banner/larrylabel"), "Load Banner")
unityads.unload_banner()
update_ui(self)
end
end
end
local function showbanner(self)
if unityads then
if not self.is_show_banner then
self.is_show_banner = true
gui.set_text(gui.get_node("show_banner/larrylabel"), "Hide Banner")
print("showBanner()")
unityads.show_banner()
else
self.is_show_banner = false
gui.set_text(gui.get_node("show_banner/larrylabel"), "Show Banner")
print("hideBanner()")
unityads.hide_banner()
end
end
end
local function change_banner_pos(self)
if unityads then
local banners = {
"BANNER_POSITION_TOP_LEFT",
"BANNER_POSITION_TOP_CENTER",
"BANNER_POSITION_TOP_RIGHT",
"BANNER_POSITION_BOTTOM_LEFT",
"BANNER_POSITION_BOTTOM_CENTER",
"BANNER_POSITION_BOTTOM_RIGHT",
"BANNER_POSITION_CENTER"
}
if not self.banner_pos_num then
self.banner_pos_num = 3
else
self.banner_pos_num = self.banner_pos_num + 1
if self.banner_pos_num > #banners then
self.banner_pos_num = 1
end
end
unityads.set_banner_position(unityads[banners[self.banner_pos_num]])
gui.set_text(gui.get_node("baner_pos"), banners[self.banner_pos_num])
end
end
function on_input(self, action_id, action)
if unityads.is_initialized() then
local prev_type = self.ad_type
self.ad_type = dirtylarry:radio("ad_type_interstitial", action_id, action, INTERSTITIAL, self.ad_type)
self.ad_type = dirtylarry:radio("ad_type_rewarded", action_id, action, REWARDED, self.ad_type)
self.ad_type = dirtylarry:radio("ad_type_banner", action_id, action, BANNER, self.ad_type)
if prev_type ~= self.ad_type then
update_ui(self)
end
else
dirtylarry:button("idfa", action_id, action, function ()
unityads.request_idfa()
end)
end
dirtylarry:button("init", action_id, action, function ()
init_unity_ads(self, gameIDs[sys_name])
end)
dirtylarry:button("set_callback", action_id, action, function ()
set_callback(self)
end)
dirtylarry:button("remove_callback", action_id, action, function ()
remove_callback(self)
end)
if self.ad_type == BANNER then
dirtylarry:button("banner", action_id, action, function ()
loadbanner(self)
end)
dirtylarry:button("show_banner", action_id, action, function ()
showbanner(self)
end)
dirtylarry:button("banner_pos", action_id, action, function ()
change_banner_pos(self)
end)
elseif self.ad_type then
dirtylarry:button("load_video", action_id, action, function ()
print("unityads.load("..self.ad_type..")")
unityads.load(self.ad_type)
end)
dirtylarry:button("show_video", action_id, action, function ()
print("unityads.show("..self.ad_type..")")
unityads.show(self.ad_type)
end)
end
if action_id == BACK_KEY and action.released then
print("Time to exit!")
msg.post("@system:", "exit", {code = 0})
end
end