Skip to content

Instantly share code, notes, and snippets.

@lokeshsuryan
Created March 19, 2021 07:55
Show Gist options
  • Save lokeshsuryan/94063b01a7da45057767e23f24262ec9 to your computer and use it in GitHub Desktop.
Save lokeshsuryan/94063b01a7da45057767e23f24262ec9 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using HuaweiService;
using HuaweiService.analytic;
using UnityEditor;
using HuaweiService.crash;
using HuaweiService.push;
using HuaweiService.ads;
using Unity.Notifications.Android;
public class Projecttile : MonoBehaviour
{
private Rigidbody2D rb;
private SpringJoint2D springJoint2D;
private bool isPressed;
public static int score = 1;
private bool isShowVideo=false;
public const string ChannelId = "game_channel0";
private HiAnalyticsInstance instance;
string eventID, key, value,token;
// Start is called before the first frame update
void Start()
{
//AGConnectCrash.getInstance().setUserId("TestUserId");
PushListenerRegister.RegisterListener(new PServiceListener());
instance = HiAnalytics.getInstance(new Context());
instance.setAnalyticsEnabled(true);
rb=GetComponent<Rigidbody2D>();
springJoint2D=GetComponent<SpringJoint2D>();
initializeAnalyticsInstane();
TurnOn();
SetListener();
}
// Update is called once per frame
void Update()
{
if(isPressed){
rb.position=Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
void initializeAnalyticsInstane(){
eventID = "GameOver";
key = "status";
value = "you won";
}
private void OnCollisionEnter2D(Collision2D collision2D){
if(collision2D.collider.tag == "Ground"){
score=1;
gameStatus("you Lose this game");
StartCoroutine(waitForReload());
}
}
public void ShowDialog(){
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene
}
// on ball release this method call
private void OnMouseUp(){
isPressed=false;
rb.isKinematic=false;
StartCoroutine(Release());
}
// on mouse streach this method call
private void OnMouseDown(){
isPressed=true;
rb.isKinematic=true;
}
public void ScoreUp()
{
score++;
if (score > 2)
{
gameStatus("you won");
if(isShowVideo){
LoadVideoAds();
}
else {
isShowVideo=true;
LoadImageAds();
}
}
else{
StartCoroutine(waitForReload());
}
}
IEnumerator waitForReload()
{
yield return new WaitForSeconds(5.0f);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
void gameStatus(string gameStatus)
{
//winText.gameObject.SetActive(true);
value=gameStatus;
SendEvent();
}
void SendEvent(){
if (string.IsNullOrEmpty(eventID) && string.IsNullOrEmpty(key) && string.IsNullOrEmpty(value))
{
Debug.Log("[HMS]: Fill Fields");
}
else
{
Bundle bundle = new Bundle();
bundle.putString("eventID",eventID);
//bundle.putString("score",score+"");
bundle.putString("Game Status",value);
instance = HiAnalytics.getInstance(new Context());
instance.onEvent("angry",bundle);
}
}
IEnumerator Release()
{
yield return new WaitForSeconds(0.15f);
GetComponent<SpringJoint2D>().enabled=false;
}
public void SetListener()
{
GetToken();
}
public void GetToken()
{
string appId = AGConnectServicesConfig.fromContext(new Context()).getString("client/app_id");
token = "HMS Push Token \n" + HmsInstanceId.getInstance(new Context()).getToken(appId, "HCM");
Debug.Log(token);
CreateNotificationChannel();
}
public void TurnOn()
{
HmsMessaging.getInstance(new Context()).turnOnPush().addOnCompleteListener(new Clistener());
}
public void TurnOff()
{
HmsMessaging.getInstance(new Context()).turnOffPush().addOnCompleteListener(new Clistener());
}
public class Clistener : OnCompleteListener
{
public override void onComplete(Task task)
{
if (task.isSuccessful())
{
Debug.Log("success");
}
else
{
Debug.Log("fail");
}
}
}
public class PServiceListener : IPushServiceListener
{
private double shortDelay = 10;
private string smallIconName = "icon_0";
private string largeIconName = "icon_1";
public override void onNewToken(string var1)
{
Debug.Log(var1);
}
public override void onMessageReceived(RemoteMessage message)
{
string s = "getCollapseKey: " + message.getCollapseKey()
+ "\n getData: " + message.getData()
+ "\n getFrom: " + message.getFrom()
+ "\n getTo: " + message.getTo()
+ "\n getMessageId: " + message.getMessageId()
+ "\n getOriginalUrgency: " + message.getOriginalUrgency()
+ "\n getUrgency: " + message.getUrgency()
+ "\n getSendTime: " + message.getSentTime()
+ "\n getMessageType: " + message.getMessageType()
+ "\n getTtl: " + message.getTtl();
Debug.Log(s);
}
}
// Load Interstitial Video Ads
public void LoadVideoAds()
{
InterstitialAd ad = new InterstitialAd(new Context());
ad.setAdId("testb4znbuh3n2");
ad.setAdListener(new MAdListener(ad));
AdParam.Builder builder = new AdParam.Builder();
ad.loadAd(builder.build());
}
public void LoadImageAds()
{
InterstitialAd ad = new InterstitialAd(new Context());
ad.setAdId("teste9ih9j0rc3");
ad.setAdListener(new MAdListener(ad));
AdParam.Builder builder = new AdParam.Builder();
AdParam adParam = builder.build();
ad.loadAd(adParam);
}
public class MAdListener : AdListener
{
private InterstitialAd ad;
public MAdListener(InterstitialAd _ad) : base()
{
ad = _ad;
}
public override void onAdLoaded()
{
Debug.Log("AdListener onAdLoaded");
ad.show();
}
}
public void CreateNotificationChannel() {
var c = new AndroidNotificationChannel() {
Id = ChannelId,
Name = "Default Channel",
Importance = Importance.High,
Description = "Generic notifications",
};
AndroidNotificationCenter.RegisterNotificationChannel(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment