From 21e26b81734e390b161056253b53ebbe90d56a7a Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Wed, 23 May 2018 23:16:44 +0300 Subject: [PATCH] Remove callback using unityads.setCallback() or unityads.setCallback(nil) Will not run DefUnityAds callback when the instance has been deleted. --- README.md | 4 +- defunityads/src/DefUnityCallback.cpp | 59 ++--- example/main.gui | 334 ++++++++++++++++++++++++++- example/ui.gui_script | 63 ++++- input/game.input_binding | 4 + 5 files changed, 419 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 813895e..2a5b359 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> See the [example folder](https://github.com/AGulev/DefUnityAds/tree/master/example) for understand how to use extension. Especially [ui.gui_script](https://github.com/AGulev/DefUnityAds/blob/master/example/ui.gui_script) file. -![Example screenshot](https://cdn.rawgit.com/AGulev/DefUnityAds/2f17110e/example/example_screenshot.png) +![Example screenshot](https://user-images.githubusercontent.com/2209596/40448649-cf08e002-5ede-11e8-9681-c20d17c0512a.jpg) ## LUA Api @@ -54,6 +54,8 @@ unityads.initialize("1401815", defunityads_callback, true) -- testMode is option [original doc](https://github.com/Unity-Technologies/unity-ads-ios/wiki/sdk_android_api_reference#unityadssetlistener) ```lua unityads.setCallback(defunityads_callback) -- set callback +unityads.setCallback(nil) -- remove callback +unityads.setCallback() -- remove callback ``` diff --git a/defunityads/src/DefUnityCallback.cpp b/defunityads/src/DefUnityCallback.cpp index 6ad42c4..d05408c 100755 --- a/defunityads/src/DefUnityCallback.cpp +++ b/defunityads/src/DefUnityCallback.cpp @@ -5,18 +5,6 @@ DefUnityAdsListener defUtoLua; dmArray m_callbacksQueue; -static void ClearQueue() -{ - for(uint32_t i = 0; i != m_callbacksQueue.Size(); ++i) - { - CallbackData* data = &m_callbacksQueue[i]; - if(data->value_1) - free(data->value_1); - data->value_1 = 0; - m_callbacksQueue.EraseSwap(i--); - } -} - static void RegisterCallback(lua_State* L, int index, DefUnityAdsListener* cbk) { if(cbk->m_Callback != LUA_NOREF) @@ -49,6 +37,7 @@ static void invoke_callback(int type, char*key_1, char*value_1, char*key_2, int { if(cbk->m_Callback == LUA_NOREF) { + dmLogInfo("DefUnityAds callback do not exist."); return; } @@ -59,33 +48,45 @@ static void invoke_callback(int type, char*key_1, char*value_1, char*key_2, int lua_pushvalue(L, -1); dmScript::SetInstance(L); - lua_pushnumber(L, type); - int count_table_elements = 1; - if (key_2 != NULL) { - count_table_elements = 2; + if (!dmScript::IsInstanceValid(L)) { + UnregisterCallback(&defUtoLua); + dmLogError("Could not run DefUnityAds callback because the instance has been deleted."); + lua_pop(L, 2); } - lua_createtable(L, 0, count_table_elements); - luaL_push_pair_str_str(L, key_1, value_1); - if (key_2 != NULL) { - luaL_push_pair_str_num(L, key_2, value_2); - } - - int number_of_arguments = 3; - int ret = lua_pcall(L, number_of_arguments, 0, 0); - if(ret != 0) { - dmLogError("Error running callback: %s", lua_tostring(L, -1)); - lua_pop(L, 1); + else { + lua_pushnumber(L, type); + int count_table_elements = 1; + if (key_2 != NULL) { + count_table_elements = 2; + } + lua_createtable(L, 0, count_table_elements); + luaL_push_pair_str_str(L, key_1, value_1); + if (key_2 != NULL) { + luaL_push_pair_str_num(L, key_2, value_2); + } + + int number_of_arguments = 3; + int ret = lua_pcall(L, number_of_arguments, 0, 0); + if(ret != 0) { + dmLogError("Error running callback: %s", lua_tostring(L, -1)); + lua_pop(L, 1); + } } assert(top == lua_gettop(L)); } void finalize(){ UnregisterCallback(&defUtoLua); - ClearQueue(); } void set_callback(lua_State* L, int pos){ - RegisterCallback(L, pos, &defUtoLua); + int type = lua_type(L, pos); + if (type == LUA_TNONE || type == LUA_TNIL) { + UnregisterCallback(&defUtoLua); + } + else{ + RegisterCallback(L, pos, &defUtoLua); + } } void add_to_queue(int type, char*key_1, char*value_1, char*key_2, int value_2){ diff --git a/example/main.gui b/example/main.gui index f3d55da..6908bd6 100644 --- a/example/main.gui +++ b/example/main.gui @@ -338,8 +338,8 @@ nodes { } nodes { position { - x: 623.2685 - y: 445.16846 + x: 324.268 + y: 445.168 z: 0.0 w: 1.0 } @@ -662,8 +662,8 @@ nodes { } nodes { position { - x: 445.19797 - y: 593.76245 + x: 143.198 + y: 593.762 z: 0.0 w: 1.0 } @@ -927,6 +927,332 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } +nodes { + position { + x: 919.141 + y: 592.256 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "set_callback" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/dirtylarry/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: -1.2791587 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 400.0 + y: 88.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.9960785 + y: 1.0 + z: 0.9921569 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "button/button_normal" + id: "set_callback/larrybutton" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "set_callback" + layer: "" + inherit_alpha: true + slice9 { + x: 32.0 + y: 32.0 + z: 32.0 + w: 32.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + overridden_fields: 1 + overridden_fields: 4 + overridden_fields: 5 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.9921569 + y: 0.9921569 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Set callback" + font: "larryfont" + id: "set_callback/larrylabel" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "set_callback/larrybutton" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + overridden_fields: 1 + overridden_fields: 5 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 919.141 + y: 448.256 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "remove_callback" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/dirtylarry/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: -1.2791587 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 400.0 + y: 88.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.9960785 + y: 1.0 + z: 0.9921569 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "button/button_normal" + id: "remove_callback/larrybutton" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "remove_callback" + layer: "" + inherit_alpha: true + slice9 { + x: 32.0 + y: 32.0 + z: 32.0 + w: 32.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + overridden_fields: 1 + overridden_fields: 4 + overridden_fields: 5 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.9921569 + y: 0.9921569 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Remove callback" + font: "larryfont" + id: "remove_callback/larrylabel" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "remove_callback/larrybutton" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + overridden_fields: 1 + overridden_fields: 5 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_PARENT max_nodes: 512 diff --git a/example/ui.gui_script b/example/ui.gui_script index 4714bba..800d67e 100644 --- a/example/ui.gui_script +++ b/example/ui.gui_script @@ -6,21 +6,25 @@ local gameIDs = { ["iPhone OS"] = "1425385" } +local BACK_KEY = hash("back") + local REWARDED = "rewardedVideo" local VIDEO = "video" local function print_placement_state(placement) - local state = placement and unityads.getPlacementState(placement) or unityads.getPlacementState() - if state == unityads.PLACEMENT_STATE_READY then - print("PLACEMENT_STATE_READY ", placement) - elseif state == unityads.PLACEMENT_STATE_NOT_AVAILABLE then - print("PLACEMENT_STATE_NOT_AVAILABLE ", placement) - elseif state == unityads.PLACEMENT_STATE_DISABLED then - print("PLACEMENT_STATE_DISABLED ", placement) - elseif state == unityads.PLACEMENT_STATE_WAITING then - print("PLACEMENT_STATE_WAITING ", placement) - elseif state == unityads.PLACEMENT_STATE_NO_FILL then - print("PLACEMENT_STATE_NO_FILL ", placement) + if unityads then + local state = placement and unityads.getPlacementState(placement) or unityads.getPlacementState() + if state == unityads.PLACEMENT_STATE_READY then + print("PLACEMENT_STATE_READY ", placement) + elseif state == unityads.PLACEMENT_STATE_NOT_AVAILABLE then + print("PLACEMENT_STATE_NOT_AVAILABLE ", placement) + elseif state == unityads.PLACEMENT_STATE_DISABLED then + print("PLACEMENT_STATE_DISABLED ", placement) + elseif state == unityads.PLACEMENT_STATE_WAITING then + print("PLACEMENT_STATE_WAITING ", placement) + elseif state == unityads.PLACEMENT_STATE_NO_FILL then + print("PLACEMENT_STATE_NO_FILL ", placement) + end end end @@ -88,6 +92,13 @@ function init(self) end end +function final(self) + print("Final! Remove Callback!") + if unityads then + unityads.setCallback() + end +end + local function init_unity_ads(self, appId) if unityads and appId then if not unityads.isInitialized() then @@ -116,6 +127,20 @@ local function show_ad(self, placementId) end end +local function set_callback(self) + if unityads then + print("Set Callback!") + unityads.setCallback(unity_ads_callback) + end +end + +local function remove_callback(self) + if unityads then + print("Remove Callback!") + unityads.setCallback(nil) -- same as unityads.setCallback() + end +end + function on_input(self, action_id, action) self.input_default = dirtylarry:input("input", action_id, action, gui.KEYBOARD_TYPE_DEFAULT, "AppId") dirtylarry:button("init", action_id, action, function () @@ -124,10 +149,12 @@ function on_input(self, action_id, action) end init_unity_ads(self, self.input_default) end) + dirtylarry:button(REWARDED, action_id, action, function () print_placement_state(REWARDED) show_ad(self, REWARDED) end) + dirtylarry:button(VIDEO, action_id, action, function () print_placement_state(VIDEO) show_ad(self, VIDEO) @@ -136,4 +163,18 @@ end) dirtylarry:button("default", action_id, action, function () show_ad(self) 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 action_id == BACK_KEY and action.released then + print("Time to exit!") + msg.post("@system:", "exit", {code = 0}) +end + end diff --git a/input/game.input_binding b/input/game.input_binding index 3180b8d..46dd0da 100644 --- a/input/game.input_binding +++ b/input/game.input_binding @@ -2,6 +2,10 @@ key_trigger { input: KEY_BACKSPACE action: "backspace" } +key_trigger { + input: KEY_BACK + action: "back" +} mouse_trigger { input: MOUSE_BUTTON_1 action: "touch"