Unity UGUI改变pivot后显示位置错误bug

 代码改动背包物品详情panel显示位置,通过鼠标进入矩形区域识别,区分不同矩形以显示不同的pviot位置,调试出现详情panel位置显示错误。

 代码:

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/*
背包装备详细信息
*/
public class ItemInfoPanel : MonoBehaviour
{
    public TextMeshProUGUI nameTxt;
    public TextMeshProUGUI typeNameTxt;
    public TextMeshProUGUI idTxt;
    public TextMeshProUGUI despTxt;
    public TextMeshProUGUI sellPriceTxt;
    public Image icon;
public void Init(ItemPro _pro,Transform _transform)//初始化道具详细信息面板
    {
        //设置父亲,正确写法
        transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform,false) ;
        //错误写法,会显示错乱
        //transform.parent=GameObject.FindGameObjectWithTag("Canvas").transform;
        //transform.SetParent(_transform, false);

        //第一步
        //设置新对象的坐标
        transform.position = _transform.position;

        //第二步
        //AreaPanel的GetPivotInArea返回中心点位,AreaPanel在BagPanel唤醒时就已创建且为静态
        Vector2 pivot = BagPanel.areaPanel.GetPivotInArea(Input.mousePosition);
        //设置中心点需要RectTransform组件(矩形变换)
        transform.GetComponent<RectTransform>().pivot = pivot;

        nameTxt.text = _pro.name;
        typeNameTxt.text = "类型:"+ _pro.typeNmae;
        idTxt.text = "ID:"+ _pro.id.ToString();
        //idTxt.text = "<color=green>药品</color>";// 副文本
        despTxt.text = _pro.desc;
        sellPriceTxt.text = "出售价格:"+ _pro.sellPrice.ToString();
        icon.sprite = Resources.Load<Sprite>(_pro.iconPath);
    }
}

 原因:第一步positon位置正确,第二部重新设置了轴心点后,将对panel的position重新设定。

所以只要先设定pivot,再设定positon(把第一步和第二部互换)。

 效果图:

代码:

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/*
背包装备详细信息
*/
public class ItemInfoPanel : MonoBehaviour
{
    public TextMeshProUGUI nameTxt;
    public TextMeshProUGUI typeNameTxt;
    public TextMeshProUGUI idTxt;
    public TextMeshProUGUI despTxt;
    public TextMeshProUGUI sellPriceTxt;
    public Image icon;


    public void Init(ItemPro _pro,Transform _transform)//初始化道具详细信息面板
    {
        //设置父亲,正确写法
        transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform,false) ;
        //错误写法,会显示错乱
        //transform.parent=GameObject.FindGameObjectWithTag("Canvas").transform;
        //transform.SetParent(_transform, false);

        //第一步
        //AreaPanel的GetPivotInArea返回中心点位,AreaPanel在BagPanel唤醒时就已创建且为静态
        Vector2 pivot = BagPanel.areaPanel.GetPivotInArea(Input.mousePosition);
        //设置中心点需要RectTransform组件(矩形变换)
        transform.GetComponent<RectTransform>().pivot = pivot;

        //第二步
        //设置新对象的坐标
        transform.position = _transform.position;

        nameTxt.text = _pro.name;
        typeNameTxt.text = "类型:"+ _pro.typeNmae;
        idTxt.text = "ID:"+ _pro.id.ToString();
        //idTxt.text = "<color=green>药品</color>";// 副文本
        despTxt.text = _pro.desc;
        sellPriceTxt.text = "出售价格:"+ _pro.sellPrice.ToString();
        icon.sprite = Resources.Load<Sprite>(_pro.iconPath);
    }
}

!一切bug都是纸老虎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值