unity3d 为什么在texture2d上移动鼠标时,mouseX和mouseY值始终为0?

icomxhvb  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(138)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Texture2DRaycastHit : MonoBehaviour
{
    private Texture2D texture2DSpriteWithClouds;
    private Vector2 mousePos = new Vector2();
    private RectTransform rect;

    // Start is called before the first frame update
    void Start()
    {
        var rawImage = GetComponent<RawImage>();
        rect = rawImage.GetComponent<RectTransform>();
        texture2DSpriteWithClouds = rawImage.texture as Texture2D;
        Color[] texture2dWithCloudsPixels = texture2DSpriteWithClouds.GetPixels();
    }

    // Update is called once per frame
    void Update()
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,
            Input.mousePosition, Camera.main, out mousePos);

        print("Mouse Pos X " + mousePos.x + " Mouse Pos Y " + mousePos.y);
    }
}

脚本被附加到具有原始图像的对象,并且原始图像源图像是texture2D。
在纹理域中是s1。

hmae6n7t

hmae6n7t1#

解决方案是在画布(原始图像在画布下)中将“渲染模式”更改为“屏幕空间-摄影机”,然后将摄影机指定给“渲染摄影机”字段。

相关问题