AnySDKUtil.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace anysdk {
  8. public enum AnySDKType
  9. {
  10. Ads = 16,
  11. Analytics = 1,
  12. IAP = 8,
  13. Share = 2,
  14. User = 32,
  15. Social = 4,
  16. Push = 64,
  17. Crash = 128,
  18. Custom = 256,
  19. REC = 512,
  20. AdTracking = 1024
  21. } ;
  22. public class AnySDKUtil
  23. {
  24. #if !UNITY_EDITOR && UNITY_ANDROID
  25. public const string ANYSDK_PLATFORM = "PluginProtocol";
  26. #else
  27. public const string ANYSDK_PLATFORM= "__Internal";
  28. #endif
  29. public const int MAX_CAPACITY_NUM = 1024;
  30. /**
  31. @brief the Dictionary type change to the string type
  32. @param Dictionary<string,string>
  33. @return string
  34. */
  35. public static string dictionaryToString( Dictionary<string,string> maps )
  36. {
  37. StringBuilder param = new StringBuilder();
  38. if ( null != maps ) {
  39. foreach (KeyValuePair<string, string> kv in maps){
  40. if ( param.Length == 0)
  41. {
  42. param.AppendFormat("{0}={1}",kv.Key,kv.Value);
  43. }
  44. else
  45. {
  46. param.AppendFormat("&{0}={1}",kv.Key,kv.Value);
  47. }
  48. }
  49. }
  50. // byte[] tempStr = Encoding.UTF8.GetBytes (param.ToString ());
  51. // string msgBody = Encoding.Default.GetString(tempStr);
  52. return param.ToString ();
  53. }
  54. /**
  55. @brief the Dictionary type change to the string type
  56. @param Dictionary
  57. @return string
  58. */
  59. public static Dictionary<string,string> stringToDictionary( string message )
  60. {
  61. Dictionary<string,string> param = new Dictionary<string,string>();
  62. if ( null != message) {
  63. if(message.Contains("&info="))
  64. {
  65. Regex regex = new Regex(@"code=(.*)&msg=([\s\S]*)&info=([\s\S]*)");
  66. string[] tokens = regex.Split(message);
  67. string code = tokens[1];
  68. string msg = tokens[2];
  69. string info = tokens[3];
  70. param.Add("code",code);
  71. param.Add("msg",msg);
  72. param.Add("info",info);
  73. }
  74. else
  75. {
  76. Regex regex = new Regex(@"code=(.*)&msg=([\s\S]*)");
  77. string[] tokens = regex.Split(message);
  78. string code = tokens[1];
  79. string msg = tokens[2];
  80. param.Add("code",code);
  81. param.Add("msg",msg);
  82. }
  83. }
  84. return param;
  85. }
  86. /**
  87. @brief the List type change to the string type
  88. @param List<string>
  89. @return string
  90. */
  91. public static string ListToString( List<string> list )
  92. {
  93. StringBuilder param = new StringBuilder();
  94. if ( null != list ) {
  95. foreach (string kv in list){
  96. if ( param.Length == 0)
  97. {
  98. param.AppendFormat("{0}",kv);
  99. }
  100. else
  101. {
  102. param.AppendFormat("&{0}",kv);
  103. }
  104. }
  105. }
  106. // byte[] tempStr = Encoding.UTF8.GetBytes (param.ToString ());
  107. // string msgBody = Encoding.Default.GetString(tempStr);
  108. return param.ToString ();
  109. }
  110. /**
  111. @brief the string type change to the List type
  112. @param string
  113. @return List<string>
  114. */
  115. public static List<string> StringToList( string value )
  116. {
  117. List<string> param = new List<string>();
  118. if (null != value && "" != value) {
  119. string[] temp = value.Split('&');
  120. if ( null != temp ) {
  121. foreach (string kv in temp){
  122. param.Add(kv);
  123. }
  124. }
  125. }
  126. return param;
  127. }
  128. #if !UNITY_EDITOR && UNITY_ANDROID
  129. private static AndroidJavaClass mAndroidJavaClass;
  130. #endif
  131. public static void registerActionCallback(AnySDKType type,MonoBehaviour gameObject,string functionName)
  132. {
  133. #if !UNITY_EDITOR && UNITY_ANDROID
  134. if (mAndroidJavaClass == null)
  135. {
  136. mAndroidJavaClass = new AndroidJavaClass( "com.anysdk.framework.unity.MessageHandle" );
  137. }
  138. string gameObjectName = gameObject.gameObject.name;
  139. mAndroidJavaClass.CallStatic( "registerActionResultCallback", new object[]{(int)type,gameObjectName,functionName});
  140. #endif
  141. }
  142. }
  143. }