Unity2D找茬游戏
2020-03-24 本文已影响0人
李狗多
设置背景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();
}
}