Unity2D找茬游戲

設置背景back,通過使用

RectTransformUtility.ScreenPointToLocalPointInRectangle

點擊背景時實例化錯誤Spite,正確的地方用Button實現(xiàn)。

    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();
        }
    }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容