UnityPlatformAndroid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #if (UNITY_ANDROID)
  2. using System;
  3. using CommonUI_Unity3D.Platform;
  4. using UnityEngine;
  5. using System.Runtime.InteropServices;
  6. using CommonLang.Concurrent;
  7. using System.IO;
  8. using CommonLang;
  9. using MPQ.Updater;
  10. namespace CommonUI_Unity3D_Android
  11. {
  12. public class UnityPlatformAndroid : IUnityPlatform
  13. {
  14. private AndroidSysFont mSysFont;
  15. public UnityPlatformAndroid()
  16. {
  17. mSysFont = new AndroidSysFont();
  18. }
  19. public void Assert(string msg)
  20. {
  21. Debug.LogError(msg);
  22. }
  23. #region TEXT
  24. private const string fontName = "Helvetica";
  25. class AndroidSysFont : IDisposable
  26. {
  27. [DllImport("UnityPlugin")]
  28. static extern void Argb2Rgba(IntPtr argb, int length);
  29. private AndroidJavaObject SysFont = new AndroidJavaObject("com.morefun.SysFont");
  30. public void Dispose()
  31. {
  32. SysFont.Dispose();
  33. }
  34. public void sysSetFont(string fontName, int fontStyle, int fontSize)
  35. {
  36. try
  37. {
  38. object[] args = new object[]
  39. {
  40. fontName,
  41. fontStyle,
  42. fontSize,
  43. };
  44. SysFont.CallStatic("sysSetFont", args);
  45. }
  46. catch (Exception err)
  47. {
  48. Debug.LogException(err);
  49. }
  50. }
  51. public bool sysFontTexture2(
  52. string pText,
  53. int fontColorRGBA,
  54. int bgCount,
  55. int bgColorRGBA,
  56. int expectSizeW,
  57. int expectSizeH,
  58. int glTextureID)
  59. {
  60. try
  61. {
  62. bool ret = SysFont.CallStatic<bool>("sysFontTexture2",
  63. pText,
  64. fontColorRGBA,
  65. bgCount,
  66. bgColorRGBA,
  67. expectSizeW,
  68. expectSizeH,
  69. glTextureID);
  70. return ret;
  71. }
  72. catch (Exception err)
  73. {
  74. Debug.LogException(err);
  75. }
  76. return false;
  77. }
  78. public bool sysFontGetPixels(
  79. string pText,
  80. int fontColorRGBA,
  81. int bgCount,
  82. int bgColorRGBA,
  83. int pixelW,
  84. int pixelH,
  85. out byte[] rgba)
  86. {
  87. try
  88. {
  89. AndroidJavaObject ret = SysFont.CallStatic<AndroidJavaObject>("sysFontGetPixels",
  90. pText,
  91. fontColorRGBA,
  92. bgCount,
  93. bgColorRGBA,
  94. pixelW,
  95. pixelH);
  96. if (ret.GetRawObject().ToInt32() != 0)
  97. {
  98. rgba = AndroidJNIHelper.ConvertFromJNIArray<byte[]>(ret.GetRawObject());
  99. return true;
  100. }
  101. }
  102. catch (Exception err)
  103. {
  104. Debug.LogException(err);
  105. }
  106. rgba = null;
  107. return false;
  108. }
  109. public bool sysFontTest(
  110. string pText,
  111. int bgCount,
  112. int expectSizeW,
  113. int expectSizeH,
  114. ref int outW,
  115. ref int outH)
  116. {
  117. try
  118. {
  119. bool ret = SysFont.CallStatic<bool>("sysFontTest",
  120. pText,
  121. bgCount,
  122. expectSizeW,
  123. expectSizeH);
  124. if (ret)
  125. {
  126. outW = SysFont.GetStatic<int>("sysFontTestW");
  127. outH = SysFont.GetStatic<int>("sysFontTestH");
  128. //Debug.Log(string.Format(">>>sysFontTest<<< {0}x{1}", outW, outH));
  129. }
  130. return ret;
  131. }
  132. catch (Exception err)
  133. {
  134. Debug.LogException(err);
  135. }
  136. return false;
  137. }
  138. }
  139. // ----------------------------------------------------------------------------------------------
  140. public Texture2D SysFontTexture(
  141. string text,
  142. bool readable,
  143. CommonUI.Display.FontStyle style,
  144. int fontSize,
  145. uint fontColor,
  146. int borderTime,
  147. uint borderColor,
  148. CommonUI.Gemo.Size2D expectSize,
  149. out int boundW,
  150. out int boundH)
  151. {
  152. //Debug.Log(">>>UnityPlatformAndroid.SysFontTexture<<< " + text);
  153. text = text + "";
  154. int _expectW = boundW = 0;
  155. int _expectH = boundH = 0;
  156. if (expectSize != null)
  157. {
  158. _expectW = boundW = (int)expectSize.width;
  159. _expectH = boundH = (int)expectSize.height;
  160. }
  161. mSysFont.sysSetFont(fontName, (int)style, fontSize);
  162. if (mSysFont.sysFontTest(text, borderTime, _expectW, _expectH, ref boundW, ref boundH))
  163. {
  164. int _pixelW = boundW;
  165. int _pixelH = boundH;
  166. byte[] rgba;
  167. if (mSysFont.sysFontGetPixels(
  168. text,
  169. (int)fontColor,
  170. (int)borderTime,
  171. (int)borderColor,
  172. (int)_pixelW,
  173. (int)_pixelH,
  174. out rgba))
  175. {
  176. Texture2D mTexture = new UnityEngine.Texture2D(_pixelW, _pixelH, TextureFormat.RGBA32, false, true);
  177. mTexture.filterMode = FilterMode.Bilinear;
  178. mTexture.wrapMode = TextureWrapMode.Clamp;
  179. mTexture.anisoLevel = 0;
  180. mTexture.mipMapBias = 0;
  181. mTexture.LoadRawTextureData(rgba);
  182. if (readable)
  183. {
  184. mTexture.Apply(false, false);
  185. }
  186. else
  187. {
  188. mTexture.Apply(false, true);
  189. }
  190. return mTexture;
  191. }
  192. }
  193. Texture2D tex = new UnityEngine.Texture2D(8, 8, TextureFormat.RGBA32, false, true);
  194. tex.filterMode = FilterMode.Point;
  195. tex.wrapMode = TextureWrapMode.Clamp;
  196. boundW = 8;
  197. boundH = 8;
  198. tex.Apply();
  199. return tex;
  200. }
  201. public bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
  202. int borderTime,
  203. float testWidth,
  204. out float realWidth,
  205. out float realHeight)
  206. {
  207. int tw = 0;
  208. int th = 0;
  209. mSysFont.sysSetFont(fontName, (int)style, (int)size);
  210. mSysFont.sysFontTest(text, borderTime, 0, 0, ref tw, ref th);
  211. realWidth = tw;
  212. realHeight = th;
  213. if (realWidth > testWidth)
  214. {
  215. mSysFont.sysFontTest(text, borderTime, (int)testWidth, 0, ref tw, ref th);
  216. realWidth = tw;
  217. return true;
  218. }
  219. return false;
  220. }
  221. #endregion
  222. // ----------------------------------------------------------------------------------------------
  223. private static void InnRect(Texture2D src, ref int sx, ref int sy)
  224. {
  225. if (sx < 0)
  226. sx = 0;
  227. if (sx >= src.width)
  228. sx = src.width - 1;
  229. if (sy < 0)
  230. sy = 0;
  231. if (sy >= src.height)
  232. sy = src.height - 1;
  233. }
  234. public void CopyPixels(Texture2D src, int sx, int sy, int sw, int sh, Texture2D dst, int dx, int dy)
  235. {
  236. int sx2 = sx + sw;
  237. int sy2 = sy + sh;
  238. int dx2 = dx + sw;
  239. int dy2 = dy + sh;
  240. InnRect(src, ref sx, ref sy);
  241. InnRect(src, ref sx2, ref sy2);
  242. InnRect(dst, ref dx, ref dy);
  243. InnRect(dst, ref dx2, ref dy2);
  244. sw = Mathf.Min(sx2 - sx, dx2 - dx);
  245. sh = Mathf.Min(sy2 - sy, dy2 - dy);
  246. try
  247. {
  248. if (sw > 0 && sh > 0)
  249. {
  250. UnityEngine.Color[] colors = src.GetPixels(sx, sy, sw, sh);
  251. dst.SetPixels(dx, dy, sw, sh, colors);
  252. dst.Apply();
  253. }
  254. }
  255. catch (Exception err)
  256. {
  257. Debug.LogError(err.Message);
  258. Debug.LogException(err);
  259. }
  260. }
  261. // ----------------------------------------------------------------------------------------------
  262. #region IME
  263. // ----------------------------------------------------------------------------------------------
  264. #if HZMFUI
  265. private GameObject mGameObject = null;
  266. private UnityPlatformAndroidTextInput mTextInput = null;
  267. public void OpenIME(CommonUI.UI.UITextInput input)
  268. {
  269. if (mGameObject == null)
  270. {
  271. mGameObject = new GameObject();
  272. GameObject.DontDestroyOnLoad(mGameObject);
  273. }
  274. if (mTextInput == null)
  275. {
  276. mTextInput = mGameObject.AddComponent<UnityPlatformAndroidTextInput>();
  277. }
  278. mTextInput.SetInput(input);
  279. }
  280. public void CloseIME()
  281. {
  282. if (mTextInput != null) { mTextInput.SetInput(null); }
  283. }
  284. #endif
  285. #endregion
  286. public long GetAvaliableSpace(string path)
  287. {
  288. try
  289. {
  290. DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
  291. return drive.AvailableFreeSpace;
  292. }
  293. catch (Exception)
  294. {
  295. return long.MaxValue;
  296. }
  297. }
  298. public long GetTotalSpace(string path)
  299. {
  300. try
  301. {
  302. DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
  303. return drive.TotalSize;
  304. }
  305. catch (Exception)
  306. {
  307. return long.MaxValue;
  308. }
  309. }
  310. public bool NativeDecompressFile(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip_file, MPQUpdater.RemoteFileInfo mpq_file, AtomicLong current_unzip_bytes)
  311. {
  312. return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_RunUnzipMPQ(updater, zip_file, mpq_file, current_unzip_bytes);
  313. }
  314. public bool NativeDecompressMemory(ArraySegment<byte> src, ArraySegment<byte> dst)
  315. {
  316. return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_DecompressZ(src, dst);
  317. }
  318. public bool NativeGetFileMD5(string fullname, out string md5)
  319. {
  320. using (var fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
  321. {
  322. md5 = CMD5.CalculateMD5(fs);
  323. }
  324. return true;
  325. }
  326. }
  327. }
  328. #endif