[] Unity game development SDK can not take pull add QQ group operating in the game? [] Unity game development SDK access and integration - White Beginners

First, the primer

  When external testing is generally performed in the game players will have a QQ group, to facilitate the players feedback issues, exchange game experience and so on. So in order to increase the player's desire to add QQ group, may be designed to function in the game a little, you can click directly pulled up the hand Q plus group, plus the QQ group after also automatically delivers some small incentive to stimulate the players. Originally I thought the operation to pull the hand of Q plus QQ group must access the platform SDK related to the job until I read the next detail Tencent QQ group official website of the official home page , I learned joined hands in the game Q operation launched additive group is so simple, do not need to access any third party SDK, just a few lines of code can be easily achieved. Without further ado, Syria, the book The text, immediately start the actual operation.

Second, the solution

1.Android end of work

  In Tencent QQ group official website of the official home page , we can directly see the official has provided us with a good Android-related code, copy it directly into our game project and corresponding Android project is ready for use. If you are using Internal packaged way, you can write Java code related Eclispe, labeled jar package for Unity and end calls. If you are using Gradle way, you can export the project AndroidStudio during the project, the automation add this piece of code. Gradle on the use of non-invasive way to access SDK package, Masan has had some new experiences on the job, I will explain in a later blog. (Damn this kid and digging, over time it will not eunuch, right)

  

  FIG 1: QQ Qunguan network side code Android

  Add in your Java code to the following code, key in the dead do not write Java code, preferably by the end of the incoming call Unity:

1  / ** **************
 2       *
 3       * Add group to initiate the process. Group number: key ColaFramework exchange group (421 527 940) is: xxxxxxxxx
 4       * call joinQQGroup (xxxxxxxxx) to start a client application plus hand-Q group ColaFramework exchange group (421 527 940)
 5       *
 6       * @param Key generated by the official website of the Key
 7       * @return returns true if successful call hands Q, represents a return call from fals failure
 . 8       ******************************************************** * / 
. 9      public  Boolean joinQQGroup (String Key) {
 10          the Intent Intent = new new the Intent ();
 . 11         intent.setData (Uri.parse ( "mqqopensdkapi:? // bizAgent / qm / qr url = http% 3A% 2F% 2Fqm.qq.com% 2Fcgi-bin% 2Fqm% 2Fqr% 3Ffrom% 3Dapp% 26p% 3Dandroid% 26k 3D% "+ Key));
 12 is          // this Flag can be customized, such as setting, at the interface by adding the group to return depending on the product required, to return the main interface Q hand, is not provided, according to the call will be returned from the product interface     // intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK) 
13 is          the try {
 14              startActivity (Intent);
 15              return  to true ;
 16          } the catch (Exception E) {
 . 17              // is not installed or hand-installed Q version does not support 
18 is              return  to false ;
 . 19          }
 20      }

  At this point we need to do end Android work is complete, is not super convenient ha ~ almost nothing dry, clams clams.  

2.iOS end of work

  Let's look at what we need to do in iOS end? In fact, much the same work with Android side, mainly to provide an interface to the Unity-side call. In Tencent QQ group official website of the official home page , the official also provided us with a good iOS-related code, but the code to be used on Unity, then, need little to transform and package it. Like a softly lit, we immediately following the teaching field, teach you how to handle it:

  

  FIG 2: QQ Qunguan network side code iOS

  Add the following code in the corresponding iOS Unity engineering project UnityAppController.mm on it. Compared to Android, iOS above also need to pass a uid parameter. And Unity end side pass over the type is const char * string pointer, we need to convert it to NSString for API calls.

 1     extern "C" BOOL iOSJoinQQGroup(const char* rawKey,const char* rawUid){
 2         NSString * key = [NSString stringWithUTF8String:rawKey];
 3         NSString * uid = [NSString stringWithUTF8String:rawUid];
 4         NSString *urlStr = [NSString stringWithFormat:@"mqqapi://card/show_pslcard?src_type=internal&version=1&uin=%@&key=%@&card_type=group&source=external", uid,key];
 5         NSURL *url = [NSURL URLWithString:urlStr];
 6         if([[UIApplication sharedApplication] canOpenURL:url]){
 7         [[UIApplication sharedApplication] openURL:url];
 8             return YES;
 9         }
10     else return NO;
11     }

  At this point, we need to do in iOS end of work completed, the next step is to call the Unity of these interfaces.

3.Unity end of work

  Simply set up a simulation game interface plus group, there is a two-dimensional code and a key group plus button, we expect that players in the game inside the click of a button can be added directly QQ group.

  

  Figure 3: a schematic view of the game interface

  Corresponding to the following C # code, the basic operation is simple and the package and Unity Android, iOS end of a communication interaction, if not understand this, it can be seen before Umazo blog " [] Unity game developers with access SDK integration - white Beginners . " It is noteworthy that, we need to address the issue of calling different platforms, add the necessary platform macro judge, in this case running Unity editor should also be taken into account.

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using System.Runtime.InteropServices;
 4 using UnityEngine;
 5 using UnityEngine.UI;
 6 
 7 public class MyScript : MonoBehaviour
 8 {
 9 
10     private static readonly string AndroidKey = "YouAndroidQQGroupKey";
11 
12     private static readonly string iOSUid = "YouiOSUid";
13     private static readonly string iOSKey = "YouiOSQQGroupKey";
14 
15     private AndroidJavaClass _jc;
16     private AndroidJavaObject _jo;
17 
18     // Use this for initialization
19     void Start()
20     {
21         _jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
22         _jo = _jc.GetStatic<AndroidJavaObject>("currentActivity");
23 
24         var btnObj = this.transform.Find("BtnQQ");
25         var button = btnObj.GetComponent<Button>();
26         button.onClick.AddListener(OnClick);
27     }
28 
29     private void OnClick()
30     {
31         bool result = JoinQQGroup();
32         if (result)
33         {
34             //TODO 你的业务逻辑
35         }
36         else
37         {
38             Debug.LogWarning("未安装手Q或者版本不支持!");
39         }
40     }
41 
42     /// <summary>
43     /// 加入QQ群的方法,有返回值,代表成功或者失败
44     /// </summary>
45     /// <returns></returns>
46     private bool JoinQQGroup()
47     {
48 #if !UNITY_EDITOR && UNITY_ANDROID
49         return CallAndroidMethod<bool>("joinQQGroup", AndroidKey);
50 #elif !UNITY_EIDTOR && UNITY_IOS
51         return iOSJoinQQGroup(iOSKey, iOSUid);
52 #else
53         return false;
54 #endif
55     }
56 
57     /// <summary>
58     /// 调用一个带有返回值的原生Android方法
59     /// </summary>
60     /// <typeparam name="ReturnType"></typeparam>
61     /// <param name="method"></param>
62     /// <param name="args"></param>
63     /// <returns></returns>
64     private ReturnType CallAndroidMethod<ReturnType>(string method, params object[] args)
65     {
66 #if !UNITY_EDITOR && UNITY_ANDROID
67         return _jo.Call<ReturnType>(method, args);
68 #endif
69         return default(ReturnType);
70     }
71 
72     //iOS方法导入
73 #if !UNITY_EDITOR && UNITY_IOS
74     [DllImport("__Internal")]
75     private static extern bool iOSJoinQQGroup(string key, string uid);
76 #endif
77 
78 }

  

  图4:在编辑器下调用的结果

  Android和iOS的效果马三就不单独打包展示了,感兴趣的话,大家可以自己打包测试一下。

三、总结

  在本篇博客中,马三和大家一起学习了如何实现在不接SDK的情况在手机上拉起手Q加群。在开发工作中,有些问题可能并没有我们想象中的那么复杂,进行功能开发之前多阅读一下官方文档、收集下相关资料,说不定就会有更加简单优雅的解决方案。

 

 

 

 

如果觉得本篇博客对您有帮助,可以扫码小小地鼓励下马三,马三会写出更多的好文章,支持微信和支付宝哟!

       

 

作者:马三小伙儿
出处:https://www.cnblogs.com/msxh/p/11243588.html 
请尊重别人的劳动成果,让分享成为一种美德,欢迎转载。另外,文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论!

Guess you like

Origin www.cnblogs.com/msxh/p/11243588.html