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都是纸老虎

猜你喜欢

转载自blog.csdn.net/Kevin_meta/article/details/126970647