using System;
|
using Cysharp.Threading.Tasks;
|
using UnityEngine;
|
using UnityEngine.Events;
|
|
//澶村儚妯″潡
|
public class AvatarCell : MonoBehaviour
|
{
|
ButtonEx m_button;
|
public ButtonEx button
|
{
|
get
|
{
|
if (m_button == null)
|
{
|
m_button = this.GetComponent<ButtonEx>("AvatarCell/Img_FaceBG");
|
}
|
return m_button;
|
}
|
}
|
|
ImageEx m_FaceBGImage;
|
public ImageEx faceBGImage
|
{
|
get
|
{
|
if (m_FaceBGImage == null)
|
{
|
m_FaceBGImage = this.GetComponent<ImageEx>("AvatarCell/Img_FaceBG");
|
}
|
return m_FaceBGImage;
|
}
|
}
|
|
EllipseMask m_FaceMask;
|
public EllipseMask faceMask
|
{
|
get
|
{
|
if (m_FaceMask == null)
|
{
|
m_FaceMask = this.GetComponent<EllipseMask>("AvatarCell/Img_FaceBG/Mask_Face");
|
}
|
return m_FaceMask;
|
}
|
}
|
|
UIFrame m_FaceUIFrame;
|
public UIFrame faceUIFrame
|
{
|
get
|
{
|
if (m_FaceUIFrame == null)
|
{
|
m_FaceUIFrame = this.GetComponent<UIFrame>("AvatarCell/Img_FaceBG/Mask_Face/Img_Face");
|
}
|
return m_FaceUIFrame;
|
}
|
}
|
|
ImageEx m_FaceImage;
|
public ImageEx faceImage
|
{
|
get
|
{
|
if (m_FaceImage == null)
|
{
|
m_FaceImage = this.GetComponent<ImageEx>("AvatarCell/Img_FaceBG/Mask_Face/Img_Face");
|
}
|
return m_FaceImage;
|
}
|
}
|
|
UIEffectPlayer m_faceSpine;
|
public UIEffectPlayer faceSpine
|
{
|
get
|
{
|
if (m_faceSpine == null)
|
{
|
m_faceSpine = this.GetComponent<UIEffectPlayer>("AvatarCell/Img_FaceBG/Mask_Face/Img_Face/Spine_Face");
|
}
|
return m_faceSpine;
|
}
|
}
|
|
|
ImageEx m_FacePicImage;
|
public ImageEx facePicImage
|
{
|
get
|
{
|
if (m_FacePicImage == null)
|
{
|
m_FacePicImage = this.GetComponent<ImageEx>("AvatarCell/Img_FaceBG/Img_FacePic");
|
}
|
return m_FacePicImage;
|
}
|
}
|
|
UIFrame m_FacePicUIFrame;
|
public UIFrame facePicUIFrame
|
{
|
get
|
{
|
if (m_FacePicUIFrame == null)
|
{
|
m_FacePicUIFrame = this.GetComponent<UIFrame>("AvatarCell/Img_FaceBG/Img_FacePic");
|
}
|
return m_FacePicUIFrame;
|
}
|
}
|
|
UIEffectPlayer m_facePicSpine;
|
public UIEffectPlayer facePicSpine
|
{
|
get
|
{
|
if (m_facePicSpine == null)
|
{
|
m_facePicSpine = this.GetComponent<UIEffectPlayer>("AvatarCell/Img_FaceBG/Img_FacePic/Spine_FacePic");
|
}
|
return m_facePicSpine;
|
}
|
}
|
|
RedpointBehaviour m_redpoint;
|
public RedpointBehaviour redpoint
|
{
|
get
|
{
|
if (m_redpoint == null)
|
{
|
m_redpoint = this.GetComponent<RedpointBehaviour>("AvatarCell/Img_FaceBG/RedPoint");
|
}
|
return m_redpoint;
|
}
|
}
|
GameObject prefab;
|
AvatarModel avatarModel;
|
PhantasmPavilionManager manager { get { return PhantasmPavilionManager.Instance; } }
|
bool isLoaded { get { return prefab != null; } }
|
Action onLoaded = null;
|
|
private void Awake()
|
{
|
LoadPrefab().Forget();
|
}
|
|
public void SetOnLoaded(Action _onloaded)
|
{
|
if (isLoaded)
|
{
|
_onloaded?.Invoke();
|
}
|
else
|
{
|
onLoaded += _onloaded;
|
}
|
}
|
|
void OnEnable()
|
{
|
PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefresh;
|
}
|
|
void OnDisable()
|
{
|
PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefresh;
|
}
|
|
private void PlayerDataRefresh(PlayerDataType type)
|
{
|
if (type == PlayerDataType.Face || type == PlayerDataType.FacePic)
|
{
|
if (avatarModel == null || avatarModel.playerID != PlayerDatas.Instance.baseData.PlayerID)
|
return;
|
InitUI(AvatarHelper.GetAvatarModel((int)PlayerDatas.Instance.baseData.PlayerID,
|
PlayerDatas.Instance.baseData.face,
|
PlayerDatas.Instance.baseData.facePic)).Forget();
|
}
|
}
|
|
protected async UniTask LoadPrefab()
|
{
|
if (prefab != null)
|
return;
|
var tmp = transform.Find("AvatarCell");
|
|
if (tmp != null)
|
{
|
prefab = tmp.gameObject;
|
return;
|
}
|
var inst = await UIUtility.CreateWidget("AvatarCell", "AvatarCell");
|
|
if (this == null)
|
{
|
if (inst != null) GameObject.DestroyImmediate(inst);
|
return;
|
}
|
|
if (prefab != null)
|
{
|
GameObject.DestroyImmediate(inst);
|
return;
|
}
|
prefab = inst;
|
|
prefab.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
|
prefab.transform.SetAsFirstSibling();
|
|
RectTransform prefabRect = prefab.GetComponent<RectTransform>();
|
RectTransform parentRect = GetComponent<RectTransform>();
|
if (prefabRect != null && parentRect != null)
|
{
|
prefabRect.anchorMin = new Vector2(0.5f, 0.5f);
|
prefabRect.anchorMax = new Vector2(0.5f, 0.5f);
|
prefabRect.sizeDelta = new Vector2(parentRect.rect.width, parentRect.rect.height);
|
}
|
|
onLoaded?.Invoke();
|
onLoaded = null;
|
|
}
|
public async UniTask InitUI(AvatarModel model)
|
{
|
if (model == null)
|
{
|
Debug.LogError("[AvatarCell] model is null");
|
return;
|
}
|
avatarModel = model;
|
await LoadPrefab(); //瀛樺湪琚嵏杞界殑鍙兘锛岄噸鏂板姞杞?
|
|
if (this == null)
|
{
|
return;
|
}
|
|
int faceID = model.faceID;
|
int facePicID = model.facePicID;
|
|
// 璇婃柇锛氭鏌?Mask_Face 鑺傜偣鍙婂叾瀛愬璞?
|
var maskTf = prefab?.transform.Find("Img_FaceBG/Mask_Face");
|
if (maskTf != null)
|
{
|
for (int i = 0; i < maskTf.childCount; i++)
|
{
|
var child = maskTf.GetChild(i);
|
var img = child.GetComponent<UnityEngine.UI.Graphic>();
|
}
|
}
|
|
// 璇婃柇锛氭鏌?Img_FacePic 鑺傜偣
|
var facePicTf = prefab?.transform.Find("Img_FaceBG/Img_FacePic");
|
if (facePicTf != null)
|
{
|
var fpImg = facePicTf.GetComponent<UnityEngine.UI.Graphic>();
|
}
|
|
await manager.ShowFace(faceImage, faceSpine, faceUIFrame, faceMask, faceID);
|
|
if (this == null) return;
|
|
string str = AvatarHelper.GetAvatarBgColorStr(faceID);
|
faceBGImage.SetSprite(str);
|
|
int resourceType = manager.GetResourceType(PhantasmPavilionType.FacePic, facePicID);
|
string resourceValue = manager.GetResourceValue(PhantasmPavilionType.FacePic, facePicID);
|
manager.Show(PhantasmPavilionType.FacePic, facePicImage, facePicSpine, facePicUIFrame, resourceType, resourceValue);
|
|
BindButtonClick(model);
|
}
|
// 娣诲姞鏍囧織浣嶏細鏄惁宸茶缃嚜瀹氫箟鐩戝惉鍣?
|
private bool hasCustomListener = false;
|
public void SetListener(UnityAction action)
|
{
|
hasCustomListener = true; // 鏍囪宸茶缃嚜瀹氫箟鐩戝惉
|
button.SetListener(action);
|
}
|
public void AddListener(UnityAction action)
|
{
|
hasCustomListener = true; // 鏍囪宸茶缃嚜瀹氫箟鐩戝惉
|
button.SetListener(action);
|
}
|
private void BindButtonClick(AvatarModel model)
|
{
|
// 濡傛灉宸茬粡璁剧疆浜嗚嚜瀹氫箟鐩戝惉鍣紝涓嶈鐩?
|
if (hasCustomListener)
|
return;
|
|
button.SetListener(() =>
|
{
|
if (avatarModel == null)
|
{
|
return;
|
}
|
AvatarHelper.TryViewOtherPlayerInfo(avatarModel.playerID);
|
});
|
}
|
|
}
|
|
public class AvatarModel
|
{
|
public int playerID { get; private set; }
|
public int faceID { get; private set; }
|
public int facePicID { get; private set; }
|
|
public AvatarModel(int playerID, int faceID, int facePicID)
|
{
|
this.playerID = playerID;
|
this.faceID = faceID;
|
this.facePicID = facePicID;
|
}
|
}
|