Unity2D找茬游戏

设置背景back,通过使用

RectTransformUtility.ScreenPointToLocalPointInRectangle

点击背景时实例化错误Spite,正确的地方用Button实现。

    public Canvas canvas;
    public Image back;
    public Image image;
    public AudioSource cuowu;
    public bool button;
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 _pos = Vector2.one;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(back.transform as RectTransform,
                Input.mousePosition, canvas.worldCamera, out _pos);
            Debug.Log("pos:" + _pos);
            Image img = Instantiate(image);
            img.rectTransform.SetParent(canvas.transform);
            Vector3 position = new Vector3(_pos.x + Screen.width / 2, _pos.y + Screen.height / 2, 0);
            img.rectTransform.position = position;
            cuowu.Play();
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容