Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: 9TOFRIDAY on February 18, 2016, 07:57:27 AM
-
Hey peeps
so I'm trying to write custom action for Facebook audience network.
here is the script I'm trying to replicate in a playmaker action:
using System.Collections;
using System.Collections.Generic;
using AudienceNetwork;
[RequireComponent (typeof(CanvasRenderer))]
[RequireComponent (typeof(RectTransform))]
public class NativeAdTest : MonoBehaviour
{
private NativeAd nativeAd;
// UI elements in scene
[Header("Text:")]
public Text title;
public Text socialContext;
[Header("Images:")]
public Image coverImage;
public Image iconImage;
[Header("Buttons:")]
public Text callToAction;
public Button callToActionButton;
public Text errorHandle;
void Awake ()
{
// Create a native ad request with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
NativeAd nativeAd = new AudienceNetwork.NativeAd ("319403784850492_327199570737580");
this.nativeAd = nativeAd;
// Wire up GameObject with the native ad; the specified buttons will be clickable.
nativeAd.RegisterGameObjectForImpression (gameObject, new Button[] { callToActionButton });
// Set delegates to get notified on changes or when the user interacts with the ad.
nativeAd.NativeAdDidLoad = (delegate() {
Debug.Log ("Native ad loaded.");
Debug.Log ("Loading images...");
// Use helper methods to load images from native ad URLs
StartCoroutine (nativeAd.LoadIconImage (nativeAd.IconImageURL));
StartCoroutine (nativeAd.LoadCoverImage (nativeAd.CoverImageURL));
Debug.Log ("Images loaded.");
title.text = nativeAd.Title;
socialContext.text = nativeAd.SocialContext;
callToAction.text = nativeAd.CallToAction;
errorHandle.text = ("Loaded");
});
nativeAd.NativeAdDidFailWithError = (delegate(string error) {
Debug.Log ("Native ad failed to load with error: " + error);
errorHandle.text = ("error");
});
nativeAd.NativeAdWillLogImpression = (delegate() {
Debug.Log ("Native ad logged impression.");
});
nativeAd.NativeAdDidClick = (delegate() {
Debug.Log ("Native ad clicked.");
});
// Initiate a request to load an ad.
nativeAd.LoadAd ();
}
void OnGUI ()
{
// Update GUI from native ad
coverImage.sprite = nativeAd.CoverImage;
iconImage.sprite = nativeAd.IconImage;
}
}
This is how far I've gotten:
namespace HutongGames.PlayMaker.Actions {
[ActionCategory("Facebook Adverts")]
[Tooltip("Load adverts")]
//[RequireComponent (typeof(CanvasRenderer))]
//[RequireComponent (typeof(RectTransform))]
public class LoadFBAdvert : FsmStateAction {
[Header("Images:")]
[RequiredFieldAttribute]
public FsmString title;
[RequiredFieldAttribute]
public FsmString socialContext;
[RequiredFieldAttribute]
[CheckForComponent(typeof(Image))]
public FsmObject coverImage;
[RequiredFieldAttribute]
[CheckForComponent(typeof(Image))]
public FsmObject iconImage;
[RequiredFieldAttribute]
public FsmString callToAction;
[RequiredFieldAttribute]
[CheckForComponent(typeof(Button))]
public FsmObject callToActionButton;
private NativeAd nativeAd;
[RequiredFieldAttribute]
public FsmEvent AdvertSuccessful;
[RequiredFieldAttribute]
public FsmEvent AdvertFailed;
public override void OnEnter()
{
// Create a native ad request with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
NativeAd nativeAd = new AudienceNetwork.NativeAd ("319403784850492_327199570737580");
this.nativeAd = nativeAd;
// Wire up GameObject with the native ad; the specified buttons will be clickable.
nativeAd.RegisterGameObjectForImpression (gameObject, new Button[] { callToActionButton });
}
}
}
I don't know what this means:
nativeAd.RegisterGameObjectForImpression (gameObject, new Button[] { callToActionButton });
i know I'm doing something wrong here saving the variables - but i don't understand the basics. game object gives a red error.
can anyone pleaser assist me? maybe even explain what exactly one needs to do in an instance like this.
thanks
-
Hi,
Indeed, this is a bold script to turn into an action if you are new to c#.
Typically, this is too much for a custom action. Instead you should keep this as a monobehavior and control it from PlayMaker.
Instead of doing the job in "awake" do it in a a Public function that you can then call using "sendMessage" action for example.
The two difficulties you are facing with this script to port as Custom action are:
-- Coroutine don't work inside Custom Actions, only in Monobehavior
-- the legacy UI isn't ideal within Custom Action. I strongly suggest you switch to the new Unity UI or ngui and the likes.
Bye,
Jean
Bye,
Jean
-
Thanks for getting back to me and thanks for pointing me in the right direction!!!!