123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #if (UNITY_ANDROID)
- using System;
- using CommonUI_Unity3D.Platform;
- using UnityEngine;
- using System.Runtime.InteropServices;
- using CommonLang.Concurrent;
- using System.IO;
- using CommonLang;
- using MPQ.Updater;
- namespace CommonUI_Unity3D_Android
- {
- public class UnityPlatformAndroid : IUnityPlatform
- {
- private AndroidSysFont mSysFont;
- public UnityPlatformAndroid()
- {
- mSysFont = new AndroidSysFont();
- }
- public void Assert(string msg)
- {
- Debug.LogError(msg);
- }
- #region TEXT
- private const string fontName = "Helvetica";
- class AndroidSysFont : IDisposable
- {
- [DllImport("UnityPlugin")]
- static extern void Argb2Rgba(IntPtr argb, int length);
- private AndroidJavaObject SysFont = new AndroidJavaObject("com.morefun.SysFont");
- public void Dispose()
- {
- SysFont.Dispose();
- }
- public void sysSetFont(string fontName, int fontStyle, int fontSize)
- {
- try
- {
- object[] args = new object[]
- {
- fontName,
- fontStyle,
- fontSize,
- };
- SysFont.CallStatic("sysSetFont", args);
- }
- catch (Exception err)
- {
- Debug.LogException(err);
- }
- }
- public bool sysFontTexture2(
- string pText,
- int fontColorRGBA,
- int bgCount,
- int bgColorRGBA,
- int expectSizeW,
- int expectSizeH,
- int glTextureID)
- {
- try
- {
- bool ret = SysFont.CallStatic<bool>("sysFontTexture2",
- pText,
- fontColorRGBA,
- bgCount,
- bgColorRGBA,
- expectSizeW,
- expectSizeH,
- glTextureID);
- return ret;
- }
- catch (Exception err)
- {
- Debug.LogException(err);
- }
- return false;
- }
- public bool sysFontGetPixels(
- string pText,
- int fontColorRGBA,
- int bgCount,
- int bgColorRGBA,
- int pixelW,
- int pixelH,
- out byte[] rgba)
- {
- try
- {
- AndroidJavaObject ret = SysFont.CallStatic<AndroidJavaObject>("sysFontGetPixels",
- pText,
- fontColorRGBA,
- bgCount,
- bgColorRGBA,
- pixelW,
- pixelH);
- if (ret.GetRawObject().ToInt32() != 0)
- {
- rgba = AndroidJNIHelper.ConvertFromJNIArray<byte[]>(ret.GetRawObject());
- return true;
- }
- }
- catch (Exception err)
- {
- Debug.LogException(err);
- }
- rgba = null;
- return false;
- }
- public bool sysFontTest(
- string pText,
- int bgCount,
- int expectSizeW,
- int expectSizeH,
- ref int outW,
- ref int outH)
- {
- try
- {
- bool ret = SysFont.CallStatic<bool>("sysFontTest",
- pText,
- bgCount,
- expectSizeW,
- expectSizeH);
- if (ret)
- {
- outW = SysFont.GetStatic<int>("sysFontTestW");
- outH = SysFont.GetStatic<int>("sysFontTestH");
- //Debug.Log(string.Format(">>>sysFontTest<<< {0}x{1}", outW, outH));
- }
- return ret;
- }
- catch (Exception err)
- {
- Debug.LogException(err);
- }
- return false;
- }
- }
- // ----------------------------------------------------------------------------------------------
- public Texture2D SysFontTexture(
- string text,
- bool readable,
- CommonUI.Display.FontStyle style,
- int fontSize,
- uint fontColor,
- int borderTime,
- uint borderColor,
- CommonUI.Gemo.Size2D expectSize,
- out int boundW,
- out int boundH)
- {
- //Debug.Log(">>>UnityPlatformAndroid.SysFontTexture<<< " + text);
- text = text + "";
- int _expectW = boundW = 0;
- int _expectH = boundH = 0;
- if (expectSize != null)
- {
- _expectW = boundW = (int)expectSize.width;
- _expectH = boundH = (int)expectSize.height;
- }
- mSysFont.sysSetFont(fontName, (int)style, fontSize);
- if (mSysFont.sysFontTest(text, borderTime, _expectW, _expectH, ref boundW, ref boundH))
- {
- int _pixelW = boundW;
- int _pixelH = boundH;
- byte[] rgba;
- if (mSysFont.sysFontGetPixels(
- text,
- (int)fontColor,
- (int)borderTime,
- (int)borderColor,
- (int)_pixelW,
- (int)_pixelH,
- out rgba))
- {
- Texture2D mTexture = new UnityEngine.Texture2D(_pixelW, _pixelH, TextureFormat.RGBA32, false, true);
- mTexture.filterMode = FilterMode.Bilinear;
- mTexture.wrapMode = TextureWrapMode.Clamp;
- mTexture.anisoLevel = 0;
- mTexture.mipMapBias = 0;
- mTexture.LoadRawTextureData(rgba);
- if (readable)
- {
- mTexture.Apply(false, false);
- }
- else
- {
- mTexture.Apply(false, true);
- }
- return mTexture;
- }
- }
- Texture2D tex = new UnityEngine.Texture2D(8, 8, TextureFormat.RGBA32, false, true);
- tex.filterMode = FilterMode.Point;
- tex.wrapMode = TextureWrapMode.Clamp;
- boundW = 8;
- boundH = 8;
- tex.Apply();
- return tex;
- }
- public bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
- int borderTime,
- float testWidth,
- out float realWidth,
- out float realHeight)
- {
- int tw = 0;
- int th = 0;
- mSysFont.sysSetFont(fontName, (int)style, (int)size);
- mSysFont.sysFontTest(text, borderTime, 0, 0, ref tw, ref th);
- realWidth = tw;
- realHeight = th;
- if (realWidth > testWidth)
- {
- mSysFont.sysFontTest(text, borderTime, (int)testWidth, 0, ref tw, ref th);
- realWidth = tw;
- return true;
- }
- return false;
- }
- #endregion
- // ----------------------------------------------------------------------------------------------
- private static void InnRect(Texture2D src, ref int sx, ref int sy)
- {
- if (sx < 0)
- sx = 0;
- if (sx >= src.width)
- sx = src.width - 1;
- if (sy < 0)
- sy = 0;
- if (sy >= src.height)
- sy = src.height - 1;
- }
- public void CopyPixels(Texture2D src, int sx, int sy, int sw, int sh, Texture2D dst, int dx, int dy)
- {
- int sx2 = sx + sw;
- int sy2 = sy + sh;
- int dx2 = dx + sw;
- int dy2 = dy + sh;
- InnRect(src, ref sx, ref sy);
- InnRect(src, ref sx2, ref sy2);
- InnRect(dst, ref dx, ref dy);
- InnRect(dst, ref dx2, ref dy2);
- sw = Mathf.Min(sx2 - sx, dx2 - dx);
- sh = Mathf.Min(sy2 - sy, dy2 - dy);
- try
- {
- if (sw > 0 && sh > 0)
- {
- UnityEngine.Color[] colors = src.GetPixels(sx, sy, sw, sh);
- dst.SetPixels(dx, dy, sw, sh, colors);
- dst.Apply();
- }
- }
- catch (Exception err)
- {
- Debug.LogError(err.Message);
- Debug.LogException(err);
- }
- }
- // ----------------------------------------------------------------------------------------------
- #region IME
- // ----------------------------------------------------------------------------------------------
- #if HZMFUI
- private GameObject mGameObject = null;
- private UnityPlatformAndroidTextInput mTextInput = null;
- public void OpenIME(CommonUI.UI.UITextInput input)
- {
- if (mGameObject == null)
- {
- mGameObject = new GameObject();
- GameObject.DontDestroyOnLoad(mGameObject);
- }
- if (mTextInput == null)
- {
- mTextInput = mGameObject.AddComponent<UnityPlatformAndroidTextInput>();
- }
- mTextInput.SetInput(input);
- }
- public void CloseIME()
- {
- if (mTextInput != null) { mTextInput.SetInput(null); }
- }
- #endif
- #endregion
- public long GetAvaliableSpace(string path)
- {
- try
- {
- DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
- return drive.AvailableFreeSpace;
- }
- catch (Exception)
- {
- return long.MaxValue;
- }
- }
- public long GetTotalSpace(string path)
- {
- try
- {
- DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
- return drive.TotalSize;
- }
- catch (Exception)
- {
- return long.MaxValue;
- }
- }
- public bool NativeDecompressFile(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip_file, MPQUpdater.RemoteFileInfo mpq_file, AtomicLong current_unzip_bytes)
- {
- return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_RunUnzipMPQ(updater, zip_file, mpq_file, current_unzip_bytes);
- }
- public bool NativeDecompressMemory(ArraySegment<byte> src, ArraySegment<byte> dst)
- {
- return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_DecompressZ(src, dst);
- }
- public bool NativeGetFileMD5(string fullname, out string md5)
- {
- using (var fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
- {
- md5 = CMD5.CalculateMD5(fs);
- }
- return true;
- }
- }
- }
- #endif
|