【问题标题】:Objects gets big when dragged around Unity C#在 Unity C# 中拖动时对象变大
【发布时间】:2016-08-06 16:58:12
【问题描述】:

当我在某个游戏对象上运行此脚本时遇到问题,我将它拖动到周围,它通过某种方式改变 z 位置变得非常大

public class Draggable : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(transform.position.x, transform.position.y, 0);
    }
}

http://prnt.sc/c2e7t7之前

http://prnt.sc/c2e7wb之后

即使我将它附加到全新的 Image 对象,例如它总是将其调整为这个精确的 Z 值 -708.4954

更新添加了仍然存在问题的小示例项目:http://s000.tinyupload.com/?file_id=65691163815194240868

【问题讨论】:

    标签: c# user-interface unity3d


    【解决方案1】:

    这可能是因为当您执行 transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition); 时 Z 轴正在发生变化,您应该将其存储为临时值,然后将 z 值更改为 0,然后再将其分配回对象的位置。

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 noZValue = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        noZValue.z = 0;
        transform.position = noZValue;
    }
    

    如果这不能解决问题,那么您正在从另一个代码修改对象比例或位置,您还应该显示该部分代码。供您参考,对象实际上并没有使用该代码进行缩放。它越来越靠近相机,使它看起来很大。

    编辑

    您问题中的代码是一种移动 Sprites/Sprite Renderer 而不是 UI 组件(如 Image、Button、RawImage....

    当您的 UI 画布处于 屏幕空间 - 相机模式时,这应该会移动您的 UI:

    public class Draggable : MonoBehaviour, IDragHandler
    {
        public Canvas parentCanvas;
        public void OnDrag(PointerEventData eventData)
        {
            Vector2 movePos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out movePos);
            transform.position = parentCanvas.transform.TransformPoint(movePos);
        }
    }
    

    如果你试试这个,你会发现当你点击图片时,图片会跳转到点击的位置。您需要在移动图像时添加偏移位置来解决此问题。只需在鼠标按钮按下时获取鼠标位置。拖动时使用它添加到当前鼠标位置。这可以通过实现IBeginDragHandler 和使用OnBeginDrag 函数来完成。

    完整的 UI 图像移动偏移:

    public class Draggable : MonoBehaviour, IDragHandler, IBeginDragHandler
    {
        public Canvas parentCanvas;
        Vector3 Offset = Vector3.zero;
    
    
        public void OnBeginDrag(PointerEventData eventData)
        {
            Vector2 pos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out pos);
            Offset = transform.position - parentCanvas.transform.TransformPoint(pos);
        }
    
        public void OnDrag(PointerEventData eventData)
        {
            Vector2 movePos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out movePos);
            transform.position = parentCanvas.transform.TransformPoint(movePos) + Offset;
        }
    }
    

    【讨论】:

    • 没有改变对象的比例或位置,因为我刚刚创建了一个全新的统一项目,添加了一个简单的Image 对象,并且只添加了这个脚本,它做的事情完全一样。如果这很重要,那就是我的画布prntscr.com/c2eiz3
    • 我不知道发生了什么。压缩该简单项目,然后在此处提供指向它的链接。我去看看。
    • 我会在问题中发布。
    • 没关系。会让你知道我找到了
    • 你有什么发现吗?
    【解决方案2】:

    @OP 我相信问题可能是您将 position.z 设置为 0 但在您的情况下 position.z 等于 localPosition.z 中的 -708.4954

    为了测试,Debug.Log(position.z) 看看它是否真的为零或将 localPosition.z 设置为零而不是位置。

    对于 GUI 对象,inspector 中的位置将始终为 localPosition,因为它们是画布的强制子级。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多