558 lines
16 KiB
C#
558 lines
16 KiB
C#
using cfg.CharacterCfg;
|
||
using Cysharp.Threading.Tasks;
|
||
using Framework;
|
||
using Gameplay;
|
||
using Gameplay.Common;
|
||
using Gameplay.Emoji;
|
||
using Gameplay.Utils;
|
||
using PhxhSDK;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.Playables;
|
||
using UnityEngine.UI;
|
||
using Button = UnityEngine.UI.Button;
|
||
using Image = UnityEngine.UI.Image;
|
||
|
||
public class UIPosterController : UIWindow, IMobileInputEventHandler
|
||
{
|
||
#region UI_Obj
|
||
//UI GameObj
|
||
///Auto Gen Start///
|
||
public Image Image_BG;
|
||
public Button Button_Back;
|
||
public Button Button_Home;
|
||
public Image Image_Poster;
|
||
public Button Button_Poster;
|
||
public TMP_Text Text_Poster;
|
||
public Image Image_LightBar;
|
||
public Button Button_Poster_Default;
|
||
public TMP_Text Text_Poster_Default;
|
||
public Button Button_Model;
|
||
public TMP_Text Text_Model;
|
||
public Image Image_LightBar01;
|
||
public Button Button_Model_Default;
|
||
public TMP_Text Text_Model_Default;
|
||
RawImage rt;
|
||
#endregion
|
||
|
||
ShowModelPreviewNode node;
|
||
PinchZoomModule PinchZoom;
|
||
private CharacterDataInfo _characterData;
|
||
private EshowType mCurShowType = EshowType.Pic;
|
||
|
||
private CharacterAttri characterAttri;
|
||
private GameObject _sampleModel;
|
||
private GameObject _instModel;
|
||
private Vector3 PosterScale = Vector3.one;
|
||
private float scaleXiShu = 1;
|
||
private GameObject modelPic;
|
||
private GameObject modelParent;
|
||
private UnitEmojiManager _emojiManager;
|
||
private InputInterpreter _inputInterpreter;
|
||
|
||
private const string DEFAULT_ANIMATION = "standready001";
|
||
private const float SCALE_XISHU = 0.5f;
|
||
private const float SCALE_XISHU_DELTA = 0.05f;
|
||
|
||
private readonly Vector3 _imgPosterScale = new(0.5f, 0.5f, 0.5f);
|
||
//private readonly Vector3 _modelPos = new(0, -309, -247);
|
||
//private readonly Quaternion _modelRotate = new(0, 180, 0, 1);
|
||
//private readonly Vector3 _modelScale = new(540, 540, 540);
|
||
private enum EshowType
|
||
{
|
||
Pic,
|
||
Model,
|
||
}
|
||
|
||
|
||
///Auto Gen End///
|
||
|
||
public static async UniTask<UIWindow> Open(CharacterDataInfo characterDataInfo)
|
||
{
|
||
CommonUtils.CaptureScreenshot();
|
||
|
||
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UIPoster, characterDataInfo);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 高斯模糊遮罩
|
||
/// </summary>
|
||
private RawImage rawImageGaussianBlurMask;
|
||
|
||
public override void PreLoad(object data = null)
|
||
{
|
||
base.PreLoad(data);
|
||
_characterData = data as CharacterDataInfo;
|
||
LoadCfg();
|
||
}
|
||
|
||
public override void OnInit()
|
||
{
|
||
//bind UI GameObj
|
||
UIPosterBinder.GetComponents(this);
|
||
|
||
rawImageGaussianBlurMask = GetComponent<RawImage>("GaussianBlurMask");
|
||
rt = GetComponent<RawImage>("Model/rt");
|
||
rawImageGaussianBlurMask.texture = CommonUtils.GetScreenTexture2D();
|
||
|
||
BindButton(Button_Back, OnButton_BackClick);
|
||
BindButton(Button_Poster, OnButton_Poster);
|
||
BindButton(Button_Model, OnButton_Model);
|
||
BindButton(Button_Poster_Default, OnButton_Poster);
|
||
BindButton(Button_Model_Default, OnButton_Model);
|
||
BindButton(Button_Home, OnClickHome);
|
||
|
||
modelPic = FindObj("Model");
|
||
PinchZoom = GetComponent<PinchZoomModule>("Image_Poster");
|
||
PinchZoom.OnDragStarted += OnDragBegin;
|
||
PinchZoom.OnDraging += OnDrag;
|
||
PinchZoom.OnDragCompleted += OnDragEnd;
|
||
|
||
// 创建输入处理器
|
||
_inputInterpreter = InputInterpreterFactory.CreateInterpreter(InputInterpreterType.AutoPlatform, "UIPost",this);
|
||
}
|
||
private void OnClickHome()
|
||
{
|
||
CommonUtils.HomeToMain();
|
||
}
|
||
|
||
//float xoffset = 0;
|
||
//float yoffset = 0;
|
||
//Vector3 curPostion = Vector3.zero;
|
||
//private void OnFingerDrag(Vector2 delta)
|
||
//{
|
||
|
||
// if (mCurShowType == EshowType.Pic)
|
||
// {
|
||
|
||
|
||
// // Image_Poster.transform.localPosition = new Vector3(Image_Poster.transform.localPosition.x+delta.x, Image_Poster.transform.localPosition.y + delta.y, 0);
|
||
|
||
|
||
// if ((Image_Poster.transform.localPosition.x + delta.x) >= (Screen.width/2- (500*Image_Poster.transform.localScale.x)))
|
||
// {
|
||
// xoffset = Screen.width / 2 - (500 * Image_Poster.transform.localScale.x);
|
||
// }
|
||
// else if (((Image_Poster.transform.localPosition.x + delta.x) <=(-Screen.width/2+(500*Image_Poster.transform.localScale.x))))
|
||
// {
|
||
// xoffset = -Screen.width / 2 + (500* Image_Poster.transform.localScale.x);
|
||
// }
|
||
// else
|
||
// {
|
||
// xoffset = Image_Poster.transform.localPosition.x + delta.x;
|
||
// }
|
||
// if ((Image_Poster.transform.localPosition.y + delta.y) >= ((Screen.height/2)- Image_Poster.rectTransform.sizeDelta.y/2 * Image_Poster.transform.localScale.x))
|
||
// {
|
||
// yoffset = ((Screen.height / 2) - (Image_Poster.rectTransform.sizeDelta.y / 2 * Image_Poster.transform.localScale.x));
|
||
// }
|
||
// else if (((Image_Poster.transform.localPosition.y + delta.y) <= ((-Screen.height / 2)+ (Image_Poster.rectTransform.sizeDelta.y/2* Image_Poster.transform.localScale.x))))
|
||
// {
|
||
// yoffset = -Screen.height / 2+(Image_Poster.rectTransform.sizeDelta.y / 2 * Image_Poster.transform.localScale.x);
|
||
// }
|
||
// else
|
||
// {
|
||
// yoffset = Image_Poster.transform.localPosition.y + delta.y;
|
||
// }
|
||
// Image_Poster.transform.localPosition = new Vector3(xoffset, yoffset, 0);
|
||
|
||
// }
|
||
//}
|
||
|
||
#region drag event
|
||
float scaleFactor;
|
||
void OnDragBegin(PointerEventData data)
|
||
{
|
||
if (mCurShowType != EshowType.Pic)
|
||
{
|
||
return;
|
||
}
|
||
scaleFactor = UIRoot.Instance.RootCanvas.GetComponent<RectTransform>().sizeDelta.x / Screen.width;
|
||
}
|
||
void OnDrag(PointerEventData data)
|
||
{
|
||
if (mCurShowType != EshowType.Pic)
|
||
{
|
||
return;
|
||
}
|
||
PinchZoom.GetComponent<RectTransform>().anchoredPosition += data.delta * scaleFactor;//Drag
|
||
//drag.transform.position = GoRoot.transform.position;
|
||
}
|
||
void OnDragEnd(PointerEventData data)
|
||
{
|
||
if (mCurShowType != EshowType.Pic)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
private void OnFingersZoom(float delta)
|
||
{
|
||
if (mCurShowType != EshowType.Pic) return;
|
||
if (delta > 0)
|
||
{
|
||
if (scaleXiShu >= SCALE_XISHU)
|
||
{
|
||
scaleXiShu = SCALE_XISHU;
|
||
}
|
||
else
|
||
{
|
||
scaleXiShu += SCALE_XISHU_DELTA;
|
||
}
|
||
Image_Poster.transform.localScale = PosterScale * scaleXiShu;
|
||
|
||
}
|
||
else
|
||
{
|
||
if (scaleXiShu <= SCALE_XISHU / 2)
|
||
{
|
||
scaleXiShu = SCALE_XISHU / 2;
|
||
}
|
||
else
|
||
{
|
||
scaleXiShu -= SCALE_XISHU_DELTA;
|
||
}
|
||
Image_Poster.transform.localScale = PosterScale * scaleXiShu;
|
||
|
||
|
||
}
|
||
}
|
||
|
||
private void OnButton_Model()
|
||
{
|
||
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
||
if (mCurShowType == EshowType.Model)
|
||
{
|
||
return;
|
||
}
|
||
RefreshModel();
|
||
}
|
||
|
||
private void RefreshModel()
|
||
{
|
||
modelPic.SetActive(true);
|
||
Image_Poster.gameObject.SetActive(false);
|
||
mCurShowType = EshowType.Model;
|
||
RefreshChangeBtn();
|
||
|
||
if (_sampleModel == null)
|
||
{
|
||
DebugUtil.LogError("加载的模型为空");
|
||
return;
|
||
}
|
||
|
||
if (_instModel != null)
|
||
{
|
||
// 做显示处理
|
||
_InitAnim();
|
||
return;
|
||
}
|
||
CommonUtils.DestoryAllChildGameObject(modelParent);
|
||
_instModel = Object.Instantiate(_sampleModel, modelParent.transform, true);
|
||
_instModel.transform.ResetLocals();
|
||
_instModel.transform.localPosition = new Vector3(1.5f, 0.1f, 1.5f);
|
||
|
||
_instModel.gameObject.SetLayerEx(LayerDefine.TeamEdit);
|
||
|
||
var pd = _instModel.GetComponentInChildren<PlayableDirector>();
|
||
if (pd != null)
|
||
{
|
||
pd.enabled = false;
|
||
}
|
||
|
||
_InitAnim();
|
||
}
|
||
|
||
private void _InitEmoji()
|
||
{
|
||
if (_emojiManager != null)
|
||
{
|
||
_emojiManager.Dispose();
|
||
_emojiManager = null;
|
||
}
|
||
var charSingleData = _characterData.Cfg;
|
||
_emojiManager = new UnitEmojiManager(_sampleModel, charSingleData.EmojiEyesColor,
|
||
charSingleData.EmojiEyeBowsType, charSingleData.EmojiEyeBowsColor, true);
|
||
_emojiManager.SwitchEmoji(charSingleData.DefaultDisplayEmojiId);
|
||
}
|
||
|
||
private void _InitAnim()
|
||
{
|
||
var animator = _instModel.GetComponentInChildren<Animator>();
|
||
if (animator != null)
|
||
{
|
||
int ran = UnityEngine.Random.Range(0, 2);
|
||
if (ran == 0)
|
||
{
|
||
animator.Play(DEFAULT_ANIMATION);
|
||
DebugUtil.Log("当前随机播放动作: " + DEFAULT_ANIMATION);
|
||
}
|
||
else
|
||
{
|
||
animator.Play("standready002");
|
||
DebugUtil.Log("当前随机播放动作: standready002");
|
||
}
|
||
//RuntimeAnimatorController runtimeAnimatorController = animator.runtimeAnimatorController;
|
||
//if (runtimeAnimatorController != null)
|
||
//{
|
||
// AnimationClip[] clips = runtimeAnimatorController.animationClips;
|
||
// int ran = UnityEngine.Random.Range(0, clips.Length);
|
||
// string stateName = clips[ran].name.Substring(10);
|
||
// DebugUtil.Log("角色动画机clips数: " + clips.Length + ",当前随机播放: " + stateName);
|
||
// animator.Play(stateName);
|
||
// return;
|
||
//}
|
||
//animator.Play(DEFAULT_ANIMATION);
|
||
}
|
||
}
|
||
|
||
private void OnButton_Poster()
|
||
{
|
||
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
||
if (mCurShowType == EshowType.Pic)
|
||
{
|
||
return;
|
||
|
||
}
|
||
|
||
Refresh2DPic();
|
||
}
|
||
private void RefreshChangeBtn()
|
||
{
|
||
Button_Poster.gameObject.SetActive(mCurShowType == EshowType.Pic);
|
||
Button_Model_Default.gameObject.SetActive(mCurShowType != EshowType.Model);
|
||
Button_Poster_Default.gameObject.SetActive(mCurShowType != EshowType.Pic);
|
||
Button_Model.gameObject.SetActive(mCurShowType == EshowType.Model);
|
||
}
|
||
private void Refresh2DPic()
|
||
{
|
||
modelPic.SetActive(false);
|
||
Image_Poster.gameObject.SetActive(true);
|
||
var picPath = CommonUtils.GetCharacterPicPath(_characterData.Cfg.RoleID, CommonUtils.ECharacterPicPathType.Poster, (int)_characterData.SkinID);
|
||
Image_Poster.sprite = AssetManager.Instance.GetPreLoadResult<Sprite>(picPath);
|
||
if (_characterData.SkinID != 1)
|
||
{
|
||
Image_Poster.transform.localPosition = Vector3.zero;
|
||
Image_Poster.SetNativeSize();
|
||
var heightVal = Image_Poster.sprite.rect.height / Screen.height;
|
||
var widthVal = Image_Poster.sprite.rect.width / Screen.width;
|
||
|
||
var height = UIRoot.Instance.RootCanvas.GetComponent<RectTransform>().sizeDelta.y;
|
||
var width = UIRoot.Instance.RootCanvas.GetComponent<RectTransform>().sizeDelta.x;
|
||
|
||
if (heightVal > widthVal)//高限
|
||
{
|
||
Image_Poster.GetComponent<RectTransform>().sizeDelta = new Vector2(height * Image_Poster.sprite.rect.width / Image_Poster.sprite.rect.height, height);
|
||
}
|
||
else//宽限
|
||
{
|
||
Image_Poster.GetComponent<RectTransform>().sizeDelta = new Vector2(width, width * Image_Poster.sprite.rect.height / Image_Poster.sprite.rect.width);
|
||
}
|
||
}
|
||
|
||
mCurShowType = EshowType.Pic;
|
||
RefreshChangeBtn();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 预加载角色配置
|
||
/// </summary>
|
||
private void LoadCfg()
|
||
{
|
||
OnLoadPostCfg();
|
||
_LoadPic();
|
||
_LoadModel();
|
||
}
|
||
|
||
|
||
private void ResetModel()
|
||
{
|
||
if (_instModel != null)
|
||
{
|
||
_instModel.Destroy();
|
||
_instModel = null;
|
||
}
|
||
}
|
||
|
||
|
||
void ModelNodeInit()
|
||
{
|
||
//展示界面时,创建模型节点
|
||
CreateModelNode().Forget();
|
||
}
|
||
|
||
private void OnButton_BackClick()
|
||
{
|
||
CloseWindow();
|
||
}
|
||
//invoke when open window
|
||
protected override void OnShowWindow(object tempInfo)
|
||
{
|
||
//InputManager.Instance.OnFingersMove += OnFingersZoom;
|
||
//InputManager.Instance.OnFingerMoveUI += OnFingerDrag;
|
||
//InputProcessor.Instance.OnFingerMoveUI += RotateMonster;
|
||
|
||
mCurShowType = EshowType.Pic;
|
||
ResetModel();
|
||
ModelNodeInit();
|
||
|
||
RefreshUI();
|
||
}
|
||
|
||
private void RotateMonster(Vector2 delta)
|
||
{
|
||
if (delta == Vector2.zero) return;
|
||
if (_instModel != null)
|
||
{
|
||
_instModel.transform.Rotate(Vector3.up, -delta.x);
|
||
}
|
||
}
|
||
|
||
|
||
private void RefreshUI()
|
||
{
|
||
if (mCurShowType == EshowType.Pic)
|
||
{
|
||
Refresh2DPic();
|
||
}
|
||
else
|
||
{
|
||
RefreshModel();
|
||
}
|
||
}
|
||
|
||
#region 预载入
|
||
private void OnLoadPostCfg()
|
||
{
|
||
characterAttri = TableManager.Instance.Tables.CharacterAttri;
|
||
}
|
||
private void _LoadPic()
|
||
{
|
||
var picPath = CommonUtils.GetCharacterPicPath(_characterData.Cfg.RoleID, CommonUtils.ECharacterPicPathType.Poster, (int)_characterData.SkinID);
|
||
AssetManager.Instance.PostPreload<Sprite>(picPath).Forget();
|
||
}
|
||
private async void _LoadModel()
|
||
{
|
||
var prefabPath = CommonUtils.GetCharacterModelPath(_characterData);
|
||
_sampleModel = await LoadAssetAsync<GameObject>(prefabPath);
|
||
_InitEmoji();
|
||
}
|
||
#endregion
|
||
|
||
#region model
|
||
private async UniTask CreateModelNode()
|
||
{
|
||
if (node == null && rt != null)
|
||
{
|
||
node = new ShowModelPreviewNode();
|
||
|
||
var size = rt.rectTransform.sizeDelta;
|
||
|
||
await node.Show((int)size.x, (int)size.y);
|
||
modelParent = node.modelParent;
|
||
|
||
RefreshPreviewNodeRawImage();
|
||
}
|
||
}
|
||
|
||
private void RefreshPreviewNodeRawImage()
|
||
{
|
||
if (node != null)
|
||
{
|
||
rt.texture = node.RTexture;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
//invoke when reload window
|
||
protected override void OnReloadWindow(object data = null)
|
||
{
|
||
|
||
}
|
||
private void UnLoad()
|
||
{
|
||
AssetManager.Instance.Unload(CommonUtils.GetCharacterPicPath(_characterData.Cfg.RoleID, CommonUtils.ECharacterPicPathType.Poster, (int)_characterData.SkinID));
|
||
var data = TableManager.Instance.Tables.SkinCfg.Get((int)_characterData.SkinID, (int)_characterData.ID);
|
||
var prefabPath = PathEx.GetCharacterDisplayModelPath(data.NameID);
|
||
AssetManager.Instance.Unload(prefabPath);
|
||
|
||
}
|
||
//invoke when close window
|
||
protected override void OnHideWindow()
|
||
{
|
||
//UnLoad();
|
||
//InputManager.Instance.OnFingersMove -= OnFingersZoom;
|
||
//InputManager.Instance.OnFingerMoveUI -= OnFingerDrag;
|
||
//InputProcessor.Instance.OnFingerMoveUI -= RotateMonster;
|
||
|
||
EventManager.Instance.Send(EventManager.EventName.EN_UIControllCultivateMainInput, true);
|
||
|
||
if (_emojiManager != null)
|
||
{
|
||
_emojiManager.Dispose();
|
||
_emojiManager = null;
|
||
}
|
||
|
||
if (_instModel != null)
|
||
{
|
||
Object.Destroy(_instModel);
|
||
_instModel = null;
|
||
}
|
||
rt.texture = null;
|
||
node.ReleasePreviewNode();
|
||
node = null;
|
||
}
|
||
|
||
public override void OnRelease()
|
||
{
|
||
base.OnRelease();
|
||
|
||
// 销毁InputProcessor
|
||
if (_inputInterpreter != null)
|
||
{
|
||
_inputInterpreter.Destroy();
|
||
_inputInterpreter = null;
|
||
}
|
||
|
||
//预加载图片释放
|
||
var picPath = CommonUtils.GetCharacterPicPath(_characterData.Cfg.RoleID, CommonUtils.ECharacterPicPathType.Poster, (int)_characterData.SkinID);
|
||
AssetManager.Instance.Unload(picPath);
|
||
}
|
||
|
||
public void OnPointerDown(Vector2 screenPosition)
|
||
{
|
||
|
||
}
|
||
|
||
public void OnPointerUp(Vector2 screenPosition)
|
||
{
|
||
|
||
}
|
||
|
||
public void OnPressMove(Vector2 delta)
|
||
{
|
||
// 处理手指移动事件,调用RotateMonster旋转模型
|
||
RotateMonster(delta);
|
||
}
|
||
|
||
public void OnTap(Vector2 screenPosition)
|
||
{
|
||
|
||
}
|
||
|
||
public void OnLongPress(Vector2 screenPosition)
|
||
{
|
||
|
||
}
|
||
|
||
public void OnLongPressUp(Vector2 screenPosition)
|
||
{
|
||
|
||
}
|
||
|
||
public void OnZoom(float delta)
|
||
{
|
||
|
||
}
|
||
} |