| using System.Collections.Generic;  | 
| using UnityEngine;  | 
|   | 
| //头像模块(支持帧动画和特效)  | 
| public class AvatarCell : MonoBehaviour  | 
| {  | 
|   | 
|     private void Awake()  | 
|     {  | 
|         LoadPrefab();  | 
|   | 
|     }  | 
|     ImageEx m_BgImage;  | 
|     private ImageEx bgImage  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_BgImage == null)  | 
|             {  | 
|                 m_BgImage = this.GetComponent<ImageEx>("AvatarCell/Img_BG");  | 
|             }  | 
|             return m_BgImage;  | 
|         }  | 
|     }  | 
|   | 
|     ImageEx m_AvatarImage;  | 
|     private ImageEx avatarImage  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarImage == null)  | 
|             {  | 
|                 m_AvatarImage = this.GetComponent<ImageEx>("AvatarCell/Img_Avatar");  | 
|             }  | 
|             return m_AvatarImage;  | 
|         }  | 
|     }  | 
|   | 
|     ImageEx m_AvatarFrameImage;  | 
|     private ImageEx avatarFrameImage  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarFrameImage == null)  | 
|             {  | 
|                 m_AvatarFrameImage = this.GetComponent<ImageEx>("AvatarCell/Img_AvatarFrame");  | 
|             }  | 
|             return m_AvatarFrameImage;  | 
|         }  | 
|     }  | 
|   | 
|     ButtonEx m_button;  | 
|     public ButtonEx button  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_button == null)  | 
|             {  | 
|                 m_button = this.GetComponent<ButtonEx>("AvatarCell/Img_AvatarFrame");  | 
|             }  | 
|             return m_button;  | 
|         }  | 
|     }  | 
|   | 
|     UIFrame m_AvatarUIFrame;  | 
|     private UIFrame avatarUIFrame  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarUIFrame == null)  | 
|             {  | 
|                 m_AvatarUIFrame = this.GetComponent<UIFrame>("AvatarCell/Img_Avatar");  | 
|             }  | 
|             return m_AvatarUIFrame;  | 
|         }  | 
|     }  | 
|   | 
|     UIFrame m_AvatarFrameUIFrame;  | 
|     private UIFrame avatarFrameUIFrame  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarFrameUIFrame == null)  | 
|             {  | 
|                 m_AvatarFrameUIFrame = this.GetComponent<UIFrame>("AvatarCell/Img_AvatarFrame");  | 
|             }  | 
|             return m_AvatarFrameUIFrame;  | 
|         }  | 
|     }  | 
|   | 
|     UIEffectPlayer m_AvatarUIEffect;  | 
|     private UIEffectPlayer avatarUIEffect  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarUIEffect == null)  | 
|             {  | 
|                 m_AvatarUIEffect = this.GetComponent<UIEffectPlayer>("AvatarCell/Img_Avatar");  | 
|             }  | 
|             return m_AvatarUIEffect;  | 
|         }  | 
|     }  | 
|   | 
|     UIEffectPlayer m_AvatarFrameUIEffect;  | 
|     private UIEffectPlayer avatarFrameUIEffect  | 
|     {  | 
|         get  | 
|         {  | 
|             if (m_AvatarFrameUIEffect == null)  | 
|             {  | 
|                 m_AvatarFrameUIEffect = this.GetComponent<UIEffectPlayer>("AvatarCell/Img_AvatarFrame");  | 
|             }  | 
|             return m_AvatarFrameUIEffect;  | 
|         }  | 
|     }  | 
|   | 
|     GameObject prefab;  | 
|   | 
|   | 
|     protected void LoadPrefab()  | 
|     {  | 
|         if (prefab != null)  | 
|             return;  | 
|         var tmp = transform.Find("AvatarCell");  | 
|   | 
|         if (tmp != null)  | 
|         {  | 
|             prefab = tmp.gameObject;  | 
|             return;  | 
|         }  | 
|         prefab = UIUtility.CreateWidget("AvatarCell", "AvatarCell");  | 
|   | 
|         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);  | 
|         }  | 
|     }  | 
|   | 
|     public void InitUI(AvatarModel model)  | 
|     {  | 
|         if (model == null)  | 
|             return;  | 
|         string img = AvatarHelper.GetAvatarBgColorStr(model.avatarID);  | 
|         bgImage.SetSprite(img);  | 
|         InitUI(model.avatarID, model.avatarUIEffectID, model.avatarFrameID, model.avatarFrameUIEffectID);  | 
|     }  | 
|   | 
|     private void InitUI(int avatarID, int avatarUIEffectID, int avatarFrameID, int avatarFrameUIEffectID)  | 
|     {  | 
|         InitAvatarUI(avatarID, avatarUIEffectID);  | 
|         InitAvatarFrameUI(avatarFrameID, avatarFrameUIEffectID);  | 
|     }  | 
|   | 
|     private void InitAvatarUI(int avatarID, int avatarUIEffectID)  | 
|     {  | 
|         avatarUIEffect?.Stop();  | 
|         avatarUIFrame.enabled = false;  | 
|         if (!PlayerFaceConfig.HasKey(avatarID))  | 
|             return;  | 
|   | 
|         PlayerFaceConfig config = PlayerFaceConfig.Get(avatarID);  | 
|         string imgStr = config.Image;  | 
|         if (UIFrameMgr.Inst.ContainsDynamicImage(imgStr))  | 
|         {  | 
|             avatarUIFrame.ResetFrame(imgStr);  | 
|   | 
|             List<UnityEngine.Sprite> spriteList = UIFrameMgr.Inst.GetDynamicImage(imgStr);  | 
|             if (!spriteList.IsNullOrEmpty())  | 
|             {  | 
|                 avatarImage.rectTransform.sizeDelta = new Vector2(spriteList[0].rect.width, spriteList[0].rect.height);  | 
|             }  | 
|   | 
|             avatarUIFrame.enabled = true;  | 
|         }  | 
|         else  | 
|         {  | 
|             avatarImage.SetSprite(imgStr);  | 
|             avatarImage.SetNativeSize();  | 
|             avatarUIFrame.enabled = false;  | 
|         }  | 
|   | 
|         if (!EffectConfig.HasKey(avatarUIEffectID))  | 
|             return;  | 
|         avatarUIEffect.effectId = avatarUIEffectID;  | 
|         // avatarUIEffect.loop = true;  | 
|         avatarUIEffect.Play();  | 
|     }  | 
|   | 
|     private void InitAvatarFrameUI(int avatarFrameID, int avatarFrameUIEffectID)  | 
|     {  | 
|         avatarFrameUIEffect?.Stop();  | 
|         avatarFrameUIFrame.enabled = false;  | 
|         if (!PlayerFacePicConfig.HasKey(avatarFrameID))  | 
|             return;  | 
|   | 
|         PlayerFacePicConfig config = PlayerFacePicConfig.Get(avatarFrameID);  | 
|         string imgStr = config.Image;  | 
|         if (UIFrameMgr.Inst.ContainsDynamicImage(imgStr))  | 
|         {  | 
|             avatarFrameUIFrame.ResetFrame(imgStr);  | 
|   | 
|             List<UnityEngine.Sprite> spriteList = UIFrameMgr.Inst.GetDynamicImage(imgStr);  | 
|             if (!spriteList.IsNullOrEmpty())  | 
|             {  | 
|                 avatarFrameImage.rectTransform.sizeDelta = new Vector2(spriteList[0].rect.width, spriteList[0].rect.height);  | 
|             }  | 
|   | 
|             avatarFrameUIFrame.enabled = true;  | 
|         }  | 
|         else  | 
|         {  | 
|             avatarFrameImage.SetSprite(imgStr);  | 
|             avatarFrameImage.SetNativeSize();  | 
|             avatarFrameUIFrame.enabled = false;  | 
|         }  | 
|   | 
|         if (!EffectConfig.HasKey(avatarFrameUIEffectID))  | 
|             return;  | 
|         avatarUIEffect.effectId = avatarFrameUIEffectID;  | 
|         // avatarUIEffect.loop = true;  | 
|         avatarUIEffect.Play();  | 
|     }  | 
| }  | 
|   | 
| public class AvatarModel  | 
| {  | 
|     public int avatarID { get; private set; }  | 
|     public int avatarFrameID { get; private set; }  | 
|     public int avatarUIEffectID { get; private set; }  | 
|     public int avatarFrameUIEffectID { get; private set; }  | 
|     public float scale { get; private set; }  | 
|   | 
|     public AvatarModel(int avatarID, int avatarFrameID, int avatarUIEffectID = 0, int avatarFrameUIEffectID = 0)  | 
|     {  | 
|         this.avatarID = avatarID;  | 
|         this.avatarFrameID = avatarFrameID;  | 
|         this.avatarUIEffectID = avatarUIEffectID;  | 
|         this.avatarFrameUIEffectID = avatarFrameUIEffectID;  | 
|     }  | 
| } |