using asap.core; using GameCore; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using cfg; namespace game { public class EventPotionController : MonoBehaviour { public Camera potionCamera; public GameObject centerPointGameObject; public Transform centerOfTopMouth = null; public List operatingPointList = new List(); public List fountainList = new List(); public List changingFountainEffectList = new List(); #region fountain 相关 public int GetFountainLevel() { int index = -1; int count = fountainList.Count; for (int i = 0; i < count; ++i) { if (fountainList[i].activeSelf == true) { index = i; } } return index + 1; } public void ChangeFountain(int fountainLevel, bool showEffect, float waitTime) { int index = fountainLevel - 1; int count = fountainList.Count; for (int i = 0; i < count; ++i) { if (i == index) { fountainList[i].SetActive(true); } else { fountainList[i].SetActive(false); } } if (showEffect) { foreach (var gameObject in changingFountainEffectList) { gameObject.SetActive(false); gameObject.SetActive(true); } } if (waitTime > 0) { this.StartCoroutine(StopChangingFountainEffect(waitTime)); } } public IEnumerator StopChangingFountainEffect(float time) { yield return new WaitForSeconds(time); foreach (var gameObject in changingFountainEffectList) { gameObject.SetActive(false); } } #endregion #region 外部接口 //public void CloseCamera() //{ // if (potionCamera == null) return; // potionCamera.gameObject.SetActive(false); //} public void OpenCamera() { if (potionCamera == null) return; potionCamera.gameObject.SetActive(true); } #endregion #region 事件相关 public void InitEvent() { FishingPotionAct.Subscribe(CloseCamera); } private void CloseCamera(EventPotionCloseCameraData data) { if (potionCamera == null) return; potionCamera.gameObject.SetActive(false); } #endregion #region 生命周期 async void Awake() { CloseCamera(null); EventPotionDataManager dataManager = GContext.container.Resolve(); EventPotionStage level = dataManager.GetCurrentPotionLevel(); ChangeFountain(level.FountainLevel, false, 0); GameObject pannelObj = await UIManager.Instance.ShowUI(dataManager.GetUIPanel()); EventPotionPanel panel = pannelObj.transform.GetComponent(); panel.SetEventPotionController(this); RectTransform panelRectTransform = pannelObj.transform.GetComponent(); float screenHeight = panelRectTransform.rect.height - panelRectTransform.sizeDelta.y; RectTransform rawImageRectTransform = panel.ScenePotionRawImage.transform.GetComponent(); float rawImageHeight = rawImageRectTransform.rect.height; float ratioHeight = rawImageHeight / screenHeight; OpenCamera(); potionCamera.rect = new Rect() { x = 0f, y = 0f, width = 1.0f, height = 1.0f }; float width = Screen.width; float height = Screen.height * ratioHeight; potionCamera.aspect = width / height; // 3渲2,rawimage Vector2 originUIPosition = new Vector2(panel.ScenePotionRawImage.transform.position.x, panel.ScenePotionRawImage.transform.position.y); Vector2 pivot = panel.ScenePotionRawImage.rectTransform.pivot; Vector2 tmpUIPosition = panel.ScenePotionRawImage.rectTransform.anchoredPosition; float imgWidth = panel.ScenePotionRawImage.rectTransform.rect.width; float imgHeight = panel.ScenePotionRawImage.rectTransform.rect.height; // --------------------------------- 场景内物体映射到UI上的位置 Begin --------------------------------------- Vector3 screenPosition = potionCamera.WorldToScreenPoint(centerPointGameObject.transform.position); float scaleX = screenPosition.x / Screen.width; float scaleY = screenPosition.y / Screen.height; float pixelX = panel.ScenePotionRawImage.rectTransform.position.x + imgWidth * (scaleX - pivot.x); float pixelY = panel.ScenePotionRawImage.rectTransform.position.y + imgHeight * (scaleY - pivot.y); Vector2 uiPosition = new Vector2(pixelX, pixelY); panel.centerPointPosition = uiPosition; // 操作点,倒水特效挂点 int count = operatingPointList.Count; for (int i = 0; i < count; i++) { Transform transform = operatingPointList[i]; screenPosition = potionCamera.WorldToScreenPoint(transform.position); scaleX = screenPosition.x / Screen.width; scaleY = screenPosition.y / Screen.height; pixelX = panel.ScenePotionRawImage.rectTransform.localPosition.x + imgWidth * (scaleX - pivot.x); pixelY = panel.ScenePotionRawImage.rectTransform.localPosition.y + imgHeight * (scaleY - pivot.y); uiPosition = new Vector2(pixelX, pixelY); panel.operatingPointTransformList.Add(uiPosition); } // 圈口挂点 if (centerOfTopMouth != null) { screenPosition = potionCamera.WorldToScreenPoint(centerOfTopMouth.position); scaleX = screenPosition.x / Screen.width; scaleY = screenPosition.y / Screen.height; pixelX = panel.ScenePotionRawImage.rectTransform.localPosition.x + imgWidth * (scaleX - pivot.x); pixelY = panel.ScenePotionRawImage.rectTransform.localPosition.y + imgHeight * (scaleY - pivot.y); panel.centerOfTopMouth = new Vector2(pixelX, pixelY); } // --------------------------------- 场景内物体映射到UI上的位置 End --------------------------------------- int renderTextureWidth = (int)rawImageRectTransform.rect.width; int renderTextureHeight = (int)rawImageRectTransform.rect.height; RenderTexture renderTexture = new RenderTexture(renderTextureWidth, renderTextureHeight, 24); potionCamera.targetTexture = renderTexture; panel.ScenePotionRawImage.texture = renderTexture; FishingPotionAct.IsInitControllerComplete = true; InitEvent(); } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } #endregion } }