Files
back_cantanBuilding/Assets/Scripts/TravelMap/TravelMapPhotoPanel.cs
2026-05-26 16:15:54 +08:00

215 lines
6.7 KiB
C#

using asap.core;
using cfg;
using game;
using GameCore;
using System;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityNative.Sharing;
public class TravelMapPhotoPanel : MonoBehaviour
{
Image icon_photo;
TMP_Text text_map;
Button btn_share;
IUnityNativeSharing adapter;
Button btn_next;
Camera photo_camera;
GameObject clickMask;
CampDataMM campData;
PlayerData playerData;
Tables tables;
byte[] bytes;
string imageName;
string savePath;
private void Awake()
{
playerData = GContext.container.Resolve<PlayerData>();
campData = GContext.container.Resolve<CampDataMM>();
tables = GContext.container.Resolve<Tables>();
btn_share = transform.Find("root/btn_share/btn_green").GetComponent<Button>();
btn_next = transform.Find("root/btn_next/btn_green").GetComponent<Button>();
icon_photo = transform.Find("root/photo/icon_photo").GetComponent<Image>();
photo_camera = transform.Find("photo_camera").GetComponent<Camera>();
text_map = transform.Find("root/text_map").GetComponent<TMP_Text>();
clickMask = transform.Find("root/clickMask").gameObject;
}
private void Start()
{
clickMask.SetActive(true);
int PreID = campData.PreID;
savePath = "";
Construction data = tables.TbConstruction.DataMap[PreID];
imageName = $"TravelMap.png";
if (campData.SwitchBuildingData != null)
{
text_map.text = LocalizationMgr.GetText(campData.constructionArea.PhotoSwitch);
}
else
{
string mapname = LocalizationMgr.GetText(data.Name_l10n_key);
text_map.text = $"{data.ID % 100}.{mapname}";
}
btn_next.onClick.AddListener(OnNextClick);
btn_share.onClick.AddListener(OnClickInviteLink);
OnCaptureFishPhoto();
}
async void OnCaptureFishPhoto()
{
Rect rect = icon_photo.rectTransform.rect;
var _rt = new RenderTexture((int)rect.width, (int)rect.height, 24);
photo_camera.targetTexture = _rt;
await Awaiters.NextFrame;
photo_camera.transform.position = Camera.main.transform.position;
photo_camera.transform.rotation = Camera.main.transform.rotation;
await Awaiters.NextFrame;
photo_camera.Render();
photo_camera.targetTexture = null;
//Capture By Camera
Texture2D texture2D = new Texture2D(_rt.width, _rt.height, TextureFormat.RGB24, false);
var currentRt = RenderTexture.active;
RenderTexture.active = _rt;
texture2D.ReadPixels(new Rect(0, 0, _rt.width, _rt.height), 0, 0);
texture2D.Apply();
icon_photo.sprite = Sprite.Create(texture2D, new Rect(0, 0, _rt.width, _rt.height), new Vector2(0.5f, 0.5f));
bytes = texture2D.EncodeToPNG();
//ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_65"));
RenderTexture.active = currentRt;
_rt.Release();
clickMask.SetActive(false);
}
void MediaSaveCallback(bool success, string path)
{
if (success)
{
savePath = path;
Debug.Log("Image saved to: " + path);
}
else
{
Debug.LogError("Failed to save image.");
}
}
void OnClickInviteLink()
{
//存图片
clickMask.SetActive(true);
CaptureImageSaver.SaveImagePersistent(bytes, imageName, MediaSaveCallback);
clickMask.SetActive(false);
if (string.IsNullOrEmpty(savePath))
{
return;
}
if (adapter == null)
{
adapter = UnityNativeSharing.Create();
}
string invite = LocalizationMgr.GetText("UI_FishingSocialPanel_44");
adapter.ShareScreenshotAndText(invite, savePath);
}
void OnNextClick()
{
if (!string.IsNullOrEmpty(savePath))
{
CaptureImageSaver.DeleteImagePersistent(savePath);
}
if (campData.IsCanSkyscraper)
{
InfiniteBuildingAct();
}
else
{
if (campData.SwitchBuildingData != null)
{
SwitchBuilding();
}
else
{
OnGoto();
}
}
}
void SwitchBuilding()
{
GContext.Publish(new SwitchVirtualCameraEvent() { type = 0, isDrag = false });
GContext.Publish(campData.SwitchBuildingData);
campData.SwitchBuildingData = null;
UIManager.Instance.DestroyUI(UITypes.TravelMapCampPhotoPanel);
}
async void OnGoto()
{
var mapData = tables.TbMapData.DataMap[playerData.lastMapId];
ILoadResourceService loadResourceService = GContext.container.Resolve<ILoadResourceService>();
bool isCanEnter = await loadResourceService.Load(mapData.EnvName);
if (isCanEnter)
{
EnterMap();
}
else
{
var panel = await UIManager.Instance.ShowUI(UITypes.CloudTransitionPanel);
panel.GetComponent<CloudTransitionPanel>().SetBtn(true, EnterMap, OnCloseClick);
}
}
void EnterMap()
{
if (campData.NextMapID != -1)
{
campData.ChangeNextCampID();
GContext.Publish(new ConditionTypeEvent(ConditionType.MapUnlocked, campData.Id));
}
playerData.SetCurrentMapId(playerData.lastMapId);
campData.SaveChangeCamp();
//打开旅行地图
//OnCloseClick();
OpenTravelMap();
}
async Task OpenTravelMap()
{
TravelMapCtrl travelMapCtrl = new TravelMapCtrl();
travelMapCtrl.Init(false);
GContext.container.RegisterInstance<TravelMapCtrl>(travelMapCtrl);
await UIManager.Instance.ShowUI(UITypes.TravelMapPanel);
UIManager.Instance.DestroyUI(UITypes.TravelMapCampPhotoPanel);
}
public void OnCloseClick()
{
GContext.Publish(new UnloadActToNextAct());
UIManager.Instance.DestroyUI(UITypes.TravelMapCampPhotoPanel);
}
async void InfiniteBuildingAct()
{
ILoadResourceService loadResourceService = GContext.container.Resolve<ILoadResourceService>();
bool isCanEnter = await loadResourceService.Load("InfiniteBuildingAct");
if (isCanEnter)
{
ToCampSkyscraper();
}
else
{
var panel = await UIManager.Instance.ShowUI(UITypes.CloudTransitionPanel);
panel.GetComponent<CloudTransitionPanel>().SetBtn(true, ToCampSkyscraper, OnCloseClick);
}
}
void ToCampSkyscraper()
{
GContext.Publish(new UnloadActToNextAct("InfiniteBuildingAct"));
UIManager.Instance.DestroyUI(UITypes.TravelMapCampPhotoPanel);
}
}