修改Unity插件SuperScollerView中ChatMsg场景中消息框宽度自适应消息体宽度

本文介绍了一个Unity中的UI元素,用于显示聊天消息。该元素能够根据消息类型(文本或图片)调整布局,包括背景颜色、箭头颜色、文字宽度等,以适应不同消息的展示需求。

 public class ListItem4 : MonoBehaviour
    {
        public Text mMsgText;
        public Image mMsgPic;
        public Image mIcon;
        public Image mItemBg;
        public Image mArrow;
        public Text mIndexText;
        int mItemIndex = -1;
        float maxWidth;
        private void Awake()
        {
            maxWidth = mMsgText.GetComponent<RectTransform>().sizeDelta.x;
        }
        public int ItemIndex
        {
            get
            {
                return mItemIndex;
            }
        }
        public void Init()
        {

        }


        public void SetItemData(ChatMsg itemData, int itemIndex)
        {
            mIndexText.text = itemIndex.ToString();
            PersonInfo person = ChatMsgDataSourceMgr.Get.GetPersonInfo(itemData.mPersonId);
            mItemIndex = itemIndex;
            if (itemData.mMsgType == MsgTypeEnum.Str)
            {
                mMsgPic.gameObject.SetActive(false);
                mMsgText.gameObject.SetActive(true);
                mMsgText.text = itemData.mSrtMsg;

                mIcon.sprite = ResManager.Get.GetSpriteByName(person.mHeadIcon);
                float width = mMsgText.preferredWidth >= maxWidth ? maxWidth : mMsgText.preferredWidth;
                float height = mMsgText.preferredHeight;
                mMsgText.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
                mMsgText.GetComponent<ContentSizeFitter>().SetLayoutVertical();
                Vector2 size = mMsgText.GetComponent<RectTransform>().sizeDelta + new Vector2(20, 20);
                mItemBg.GetComponent<RectTransform>().sizeDelta = size;
                if (person.mId == 0)
                {
                    mItemBg.color = new Color32(160, 231, 90, 255);
                    mArrow.color = mItemBg.color;
                }
                else
                {
                    mItemBg.color = Color.white;
                    mArrow.color = mItemBg.color;
                }
                RectTransform tf = gameObject.GetComponent<RectTransform>();
                float y = size.y;
                if (y < 75)
                {
                    y = 75;
                }
                tf.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, y);
            }
            else
            {
                mMsgPic.gameObject.SetActive(true);
                mMsgText.gameObject.SetActive(false);
                mMsgPic.sprite = ResManager.Get.GetSpriteByName(itemData.mPicMsgSpriteName);
                mMsgPic.SetNativeSize();
                mIcon.sprite = ResManager.Get.GetSpriteByName(person.mHeadIcon);
                Vector2 size = mItemBg.GetComponent<RectTransform>().sizeDelta;
                size.x = mMsgPic.GetComponent<RectTransform>().sizeDelta.x + 20;
                size.y = mMsgPic.GetComponent<RectTransform>().sizeDelta.y + 20;
                mItemBg.GetComponent<RectTransform>().sizeDelta = size;
                if (person.mId == 0)
                {
                    mItemBg.color = new Color32(160, 231, 90, 255);
                    mArrow.color = mItemBg.color;
                }
                else
                {
                    mItemBg.color = Color.white;
                    mArrow.color = mItemBg.color;
                }
                RectTransform tf = gameObject.GetComponent<RectTransform>();
                float y = size.y;
                if (y < 75)
                {
                    y = 75;
                }
                tf.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, y);

            }


        }


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值