| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | using System.Collections;using UnityEngine;using UI.Poem;using System.Collections.Generic;using ET;using FairyGUI;using System.Threading.Tasks;namespace GFGGame{    public class PoemPhotoSaveView : BaseWindow    {        private UI_PoemPhotoSaveUI _ui;        private int curIndex;        private bool saveFinished;        private bool captureFinished;        private byte[] byteArr;        public override void Dispose()        {            if(_ui != null)            {                _ui.Dispose();                _ui = null;            }            base.Dispose();        }        protected override void OnInit()        {            base.OnInit();            packageName = UI_PoemPhotoSaveUI.PACKAGE_NAME;            _ui = UI_PoemPhotoSaveUI.Create();            viewCom = _ui.target;            isfullScreen = true;            this.modal = false;            clickBlankToClose = false;        }        protected override void OnShown()        {            base.OnShown();            SavePhotoList savePhoto = (SavePhotoList)viewData;            _ui.m_c1.selectedIndex = savePhoto.pageIndex;            UpdateProgress(0, savePhoto.listChoose.Count);            RefreshUI(savePhoto);        }        protected override void OnHide()        {            base.OnHide();            ResetData();        }        private void ResetData()        {            curIndex = 0;            byteArr = null;            saveFinished = false;            captureFinished = false;        }        private async void RefreshUI(SavePhotoList savePhoto)        {            for(int i = 0; i < savePhoto.listChoose.Count; i++)            {                int index = GetPhotoListIndex(savePhoto.photoInfos, savePhoto.listChoose[i]);                byte[] bytes = null;                if (_ui.m_c1.selectedIndex == 0)                {                    RefreshPhoto(savePhoto.photoInfos[index]);                    bytes = savePhoto.photoInfos[index].Bytes;                }                else                {                    RefreshTravelPhoto(savePhoto.photoInfos[index]);                    await Task.Delay(50);                    captureFinished = false;                    Timers.inst.StartCoroutine(CaptureByUI());                    while (!captureFinished)                    {                        await Task.Delay(17);                    }                    bytes = byteArr;                }                saveFinished = false;                SavePhoto(bytes);                while (!saveFinished)                {                    await Task.Delay(17);                }                ++curIndex;                UpdateProgress(curIndex, savePhoto.listChoose.Count);                if (curIndex == savePhoto.listChoose.Count)                {                    PromptController.Instance.ShowFloatTextPrompt("已全部保存至相册!");                    Hide();                }            }        }        private int GetPhotoListIndex(List<PoemPhotoData> photoInfos, long pictureID)        {            for (int i = 0; i < photoInfos.Count; i++)            {                if (photoInfos[i].PictureId == pictureID)                {                    return i;                }            }            return -1;        }        private void UpdateProgress(int cur, int max)        {            _ui.m_progress.value = cur;            _ui.m_progress.max = max;        }        private void RefreshPhoto(PoemPhotoData data)        {            UI_ListPhotoPreviewItem item = UI_ListPhotoPreviewItem.Proxy(_ui.m_personalPhoto.target);            item.m_comPhoto.m_loaPhoto.texture = data.Ntexture;            UI_ListPhotoPreviewItem.ProxyEnd();        }        private void RefreshTravelPhoto(PoemPhotoData data)        {            UI_ComPostcard item = UI_ComPostcard.Proxy(_ui.m_travelPhoto.target);            PoemPhotoData photoData = data;            TravelLoactionCfg loactionCfg = TravelLoactionCfgArray.Instance.GetCfg(photoData.TravelLocationId);            item.m_comTravel.m_loaBg.url = ResPathUtil.GetTravelBgPath(loactionCfg.res);            item.m_txtLocationName.text = loactionCfg.name;            item.m_txtTime.text = TimeUtil.FormattingTimeTo_yyyMMdd1(photoData.CreationTime);            item.m_comTravel.m_loaRole.url = "";            if (photoData.TravelSuitId > 0)            {                TravelSuitCfg travelSuitCfg = TravelSuitCfgArray.Instance.GetCfg(photoData.TravelSuitId);                item.m_comTravel.m_loaRole.url = ResPathUtil.GetTravelRolePath(travelSuitCfg.reourcesArr[photoData.SuitResIndex]);                item.m_comTravel.m_loaRole.SetXY(loactionCfg.positionsArr[photoData.PositionIndex][0], loactionCfg.positionsArr[photoData.PositionIndex][1]);            }            UI_ComPostcard.ProxyEnd();        }        private IEnumerator CaptureByUI()        {            //等待帧画面渲染结束            yield return new WaitForEndOfFrame();            int width = (int)(_ui.m_saveImg.width * UIContentScaler.scaleFactor);            int height = (int)(_ui.m_saveImg.height * UIContentScaler.scaleFactor);            Vector2 pos = _ui.m_saveImg.LocalToGlobal(Vector2.zero);            pos.y = Screen.height - pos.y - height;            Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);            //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度            tex.ReadPixels(new Rect(pos.x, pos.y, width, height), 0, 0);            //执行读取操作            tex.Apply();            byte[] bytes = tex.EncodeToPNG();            captureFinished = true;            byteArr = bytes;        }        private void SavePhoto(byte[] bytes)        {#if UNITY_EDITOR            // pc端保存            string path = Application.dataPath + "/StreamingAssets/" + TimeHelper.ServerNowSecs + curIndex + ".png";            System.IO.File.WriteAllBytes(path, bytes);            Debug.Log("the photo saved in:" + path);            saveFinished = true;#else            // 手机端            string fileName = "wsj" + TimeHelper.ServerNowSecs + curIndex + ".jpg";            NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(bytes, "Wanshijing", fileName, (success, path) =>            {                if (success)                {                    saveFinished = true;                }                else                {                }            });#endif        }    }}
 |