Exemplo n.º 1
0
        private IEnumerator TrimScreenShotAndShare()
        {
            yield return(new WaitForEndOfFrame());

            Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

            QuitShareHUD();

            Debug.LogFormat("截屏图片大小[{0},{1}]", texture.width, texture.height);

            int shareHUDWidth  = (int)((shareContainer.transform as RectTransform).rect.width / CommonData.scalerToPresetResulotion);
            int shareHUDHeight = (int)((shareContainer.transform as RectTransform).rect.height / CommonData.scalerToPresetResulotion);

            int offsetX = (int)((texture.width - shareHUDWidth) / 2);
            int offsetY = (int)((texture.height - shareHUDHeight) / 2);

            Texture2D newT2d = new Texture2D(shareHUDWidth, shareHUDHeight);

            for (int i = offsetX; i < texture.width - offsetX; i++)
            {
                for (int j = offsetY; j < texture.height - offsetY; j++)
                {
                    Color c = texture.GetPixel(i, j);

                    newT2d.SetPixel(i - offsetX, j - offsetY, c);
                }
            }

            newT2d.Apply();

            Texture2D resizedT2d = ScaleTexture(newT2d, texture.width, texture.height);

            byte[] trimImgData = resizedT2d.EncodeToPNG();

            if (!DataHandler.DirectoryExist(Application.persistentDataPath + "/tempPics"))
            {
                DataHandler.CreateDirectory(Application.persistentDataPath + "/tempPics");
            }

            string trimImgPath = Application.persistentDataPath + "/tempPics/shareImage.png";

            File.WriteAllBytes(trimImgPath, trimImgData);


                        #if UNITY_EDITORtemp
                        #elif UNITY_IOS || UNITY_ANDROID
            Share();
                        #endif
        }
Exemplo n.º 2
0
        /// <summary>
        /// 持久化数据
        /// </summary>
        public void PersistData()
        {
            DirectoryInfo persistDi = new DirectoryInfo(CommonData.persistDataPath);

            IEnumerator initDataCoroutine = null;

            CheckDataModel checkData = new CheckDataModel();

            MySQLiteHelper sql = MySQLiteHelper.Instance;

            // 检查数据的完整性
            bool dataComplete = CheckDataComplete();

            // 如果原始玩家数据存在的话
            if (File.Exists(CommonData.playerDataFilePath))
            {
                // 记录原始玩家数据
                checkData.playerData = DataHandler.LoadDataToSingleModelWithPath <PlayerData>(CommonData.playerDataFilePath);
                checkData.playerData.needChooseDifficulty = false;

                if (ApplicationInfo.Instance != null)
                {
                    checkData.versionUpdate = ApplicationInfo.Instance.currentVersion + 0.001f < GameManager.Instance.currentVersion;
                }
                else
                {
                    checkData.versionUpdate = true;
                }
            }

            if (checkData.versionUpdate)
            {
                if (File.Exists(CommonData.dataBaseFilePath))
                {
                    sql.GetConnectionWith(CommonData.dataBaseName);

                    sql.BeginTransaction();

                    checkData.learnedWordsInSimple = GetWordRecordsInDataBase(sql, 0);
                    checkData.learnedWordsInMedium = GetWordRecordsInDataBase(sql, 1);
                    checkData.learnedWordsInMaster = GetWordRecordsInDataBase(sql, 2);

                    sql.EndTransaction();
                    sql.CloseConnection(CommonData.dataBaseName);
                }

                checkData.playerData.isNewPlayer = true;

                if (File.Exists(CommonData.buyRecordFilePath))
                {
                    checkData.buyRecord = BuyRecord.Instance;
                }

                if (File.Exists(CommonData.chatRecordsFilePath))
                {
                    checkData.chatRecords = GameManager.Instance.gameDataCenter.chatRecords;
                }

                if (File.Exists(CommonData.mapEventsRecordFilePath))
                {
                    checkData.mapEventsRecords = GameManager.Instance.gameDataCenter.mapEventsRecords;
                }

                if (File.Exists(CommonData.gameSettingsDataFilePath))
                {
                    checkData.gameSettings = GameManager.Instance.gameDataCenter.gameSettings;
                }

                if (File.Exists(CommonData.miniMapRecordFilePath))
                {
                    checkData.miniMapRecord = GameManager.Instance.gameDataCenter.currentMapMiniMapRecord;
                }
                if (File.Exists(CommonData.currentMapEventsRecordFilePath))
                {
                    checkData.currentMapEventsRecord = GameManager.Instance.gameDataCenter.currentMapEventsRecord;
                }
                if (File.Exists(CommonData.playRecordsFilePath))
                {
                    checkData.playRecords = GameManager.Instance.gameDataCenter.allPlayRecords;
                }
            }



#if UNITY_IOS
            if (!persistDi.Exists || checkData.versionUpdate || !dataComplete)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                DataHandler.CopyDirectory(CommonData.originDataPath, CommonData.persistDataPath, true);

                if (!persistDi.Exists)
                {
                    OnNewInstall();
                }

                else if (checkData.versionUpdate || !dataComplete)
                {
                    GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;

                    OnVersionUpdate(checkData, sql);
                }

                initDataCoroutine = InitData();

                StartCoroutine(initDataCoroutine);

                return;
            }
            else if (alwaysPersistData)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                DataHandler.CopyDirectory(CommonData.originDataPath, CommonData.persistDataPath, true);

                if (!persistDi.Exists)
                {
                    OnNewInstall();
                }

                else if (checkData.versionUpdate || !dataComplete)
                {
                    GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;

                    OnVersionUpdate(checkData, sql);
                }
            }

            initDataCoroutine = InitData();

            StartCoroutine(initDataCoroutine);
#elif UNITY_ANDROID
            if (!persistDi.Exists || checkData.versionUpdate || !dataComplete)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                IEnumerator copyDataCoroutine = CopyDataForPersist(delegate {
                    if (!persistDi.Exists)
                    {
                        OnNewInstall();
                    }
                    else if (checkData.versionUpdate || !dataComplete)
                    {
                        GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;
                        OnVersionUpdate(checkData, sql);
                    }
                });
                StartCoroutine(copyDataCoroutine);
                return;
            }
            else if (alwaysPersistData)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                if (DataHandler.DirectoryExist(CommonData.persistDataPath + "/Data"))
                {
                    DataHandler.DeleteDirectory(CommonData.persistDataPath + "/Data");
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                IEnumerator copyDataCoroutine = CopyDataForPersist(delegate {
                    if (!persistDi.Exists)
                    {
                        OnNewInstall();
                    }
                    else if (checkData.versionUpdate || !dataComplete)
                    {
                        GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;
                        OnVersionUpdate(checkData, sql);
                    }
                });
                StartCoroutine(copyDataCoroutine);
            }
            else
            {
                initDataCoroutine = InitData();

                StartCoroutine(initDataCoroutine);
            }
#endif
        }
        /// <summary>
        /// 截屏并截取分享部分的图片
        /// </summary>
        /// <returns>The screen shot and share.</returns>
        private IEnumerator TrimScreenShotAndShare()
        {
            yield return(new WaitForEndOfFrame());

            // 截屏
            Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

            Debug.LogFormat("截屏图片大小[{0},{1}]", texture.width, texture.height);

            float transferScaler = 1f;

            // 按照分辨率来确认转换系数
            if (Camera.main.pixelWidth < 1080f)
            {
                if (CommonData.HWScalerOfCurrentScreen < 1.7f)
                {
                    transferScaler = CommonData.scalerToPresetH;
                }
                else
                {
                    transferScaler = CommonData.scalerToPresetW;
                }
            }

            // 获取分享区域的大小
            int shareHUDWidth  = (int)((shareShotcutRect.transform as RectTransform).rect.width * transferScaler);
            int shareHUDHeight = (int)((shareShotcutRect.transform as RectTransform).rect.height * transferScaler);


            Debug.LogFormat("实际图片大小:[{0},{1}]", shareHUDWidth, shareHUDHeight);

            // 分享区域在y方向的偏移
            int sharePlaneFixY = (int)(sharePlane.localPosition.y * transferScaler);

            Debug.LogFormat("3y:{0}", sharePlane.localPosition.y);

            // 分享区域的X方向offset和Y方向offset
            int offsetYFix = (int)(shareShotcutRect.localPosition.y * transferScaler);
            int offsetX    = (texture.width - shareHUDWidth) / 2;

            // 实际分享区域的最小y值
            int offsetYMin = (texture.height - shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY;
            // 实际分享区域的最大y值
            int offsetYMax = (texture.height + shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY;


            //int offsetYMin = offsetYFix;
            //int offsetYMax = offsetYFix + shareHUDHeight;

            Debug.LogFormat("实际最小y{0},最大y{1}", offsetYMin, offsetYMax);

            // 按照分享图片的大小创建新的空纹理
            Texture2D newT2d = new Texture2D(shareHUDWidth, shareHUDHeight);

            // 像素处理
            for (int i = offsetX; i < texture.width - offsetX; i++)
            {
                for (int j = offsetYMin; j < offsetYMax; j++)
                {
                    Color c = texture.GetPixel(i, j);

                    newT2d.SetPixel(i - offsetX, j - offsetYMin, c);
                }
            }


            // 纹理应用
            newT2d.Apply();

            // 纹理转化为jpg格式二进制数据
            byte[] trimImgData = newT2d.EncodeToJPG();

            // 检查临时分享文件夹是否存在,不存在创建文件夹
            if (!DataHandler.DirectoryExist(Application.persistentDataPath + "/tempPics"))
            {
                DataHandler.CreateDirectory(Application.persistentDataPath + "/tempPics");
            }

            // 临时图片保存位置
            string trimImgPath = Application.persistentDataPath + "/tempPics/shareImage.jpg";

            // 保存图片
            File.WriteAllBytes(trimImgPath, trimImgData);

            // 清理工作
            Destroy(texture);
            Destroy(newT2d);

            texture     = null;
            newT2d      = null;
            trimImgData = null;

            Resources.UnloadUnusedAssets();
            GC.Collect();

#if UNITY_EDITOR
            //DataHandler.DeleteFile(Application.persistentDataPath + "/tempPics/shareImage.jpg");
            QuitShareView();
#elif UNITY_IOS || UNITY_ANDROID
            Share();
#endif
        }