首页 > 解决方案 > UI 按钮和克隆对象

问题描述

我是新来的单位,我还不是很了解,而且我还在尝试从很多方面弄清楚,所以请不要发誓。我决定制作俄罗斯方块的第一个项目遇到了问题。我正在尝试通过 UI Button 来控制图形,问题是该控件不适用于我通过 Instantiate 创建的克隆,但是,对于原始对象,如果我只是将其转移到舞台上,则该控件可以工作悄悄。如何使 UI 按钮对克隆起作用?提前感谢您的回复。

using System.Collections.Generic;
using UnityEngine;


public class Group : MonoBehaviour
{
    float lastFall = 0;


    // Start is called before the first frame update
    void Start()
    {
        if (!isValidGridPos())
        {
            Debug.Log("GAME OVER");
            Destroy(gameObject);
        }      
    }

    // Update is called once per frame
    void Update()
    {
        //Move Left
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(1, 0, 0);
            }
        }



        // Move Right
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.position += new Vector3(1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(-1, 0, 0);
            }
        }

        // Rotate
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            transform.Rotate(0, 0, -90);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.Rotate(0, 0, 90);
            }
        }

        //Down
        else if (Input.GetKeyDown(KeyCode.DownArrow) ||
                 Time.time - lastFall >= 1)
        {

            transform.position += new Vector3(0, -1, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            { 
                transform.position += new Vector3(0, 1, 0);
                Playfield.deleteFullRows();
                FindObjectOfType<Spawner>().SpawnNext();
                enabled = false;
            }
            lastFall = Time.time;
        }
    }


    bool isValidGridPos()
    {
        foreach (Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);

            if (!Playfield.insideBorder(v))
            {
                return false;
            }

            if (Playfield.grid[(int)v.x, (int)v.y] != null &&
                Playfield.grid[(int)v.x, (int)v.y].parent != transform)
            {
                return false;
            }
        }
        return true;
    }

    void updateGrid()
    {
        for (int y = 0; y < Playfield.h; ++y)
            for (int x = 0; x < Playfield.w; ++x)
                if (Playfield.grid[x, y] != null)
                    if (Playfield.grid[x, y].parent == transform)
                        Playfield.grid[x, y] = null;

        foreach (Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);
            Playfield.grid[(int)v.x, (int)v.y] = child;
        }
    }

    public void MoveLeft()
    {
        transform.position += new Vector3(-1, 0, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(1, 0, 0);
        }
    }

    public void MoveRight()
    {      
        transform.position += new Vector3(1, 0, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(-1, 0, 0);
        }
        
    }

    public void Rotate()
    {            
        transform.Rotate(0, 0, -90);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.Rotate(0, 0, 90);
        }
        
    }

    public void MoveDown()
    {
        transform.position += new Vector3(0, -1, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(0, 1, 0);
            Playfield.deleteFullRows();
            FindObjectOfType<Spawner>().SpawnNext();
            enabled = false;
        }

    }
}

标签: c#unity3d

解决方案


嗨,我检查了您所说的内容,如果我理解了,您会丢失对新对象的引用并且无法移动它我制作了一个示例代码,您可以尝试检查一下

public Transform currentObject;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (currentObject != null)
    {
        if (Input.GetKey(KeyCode.A))
        {
            currentObject.position += new Vector3(-1, 0, 0)*Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))
        {
            currentObject.position += new Vector3(1, 0, 0) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.W))
        {
            currentObject.position += new Vector3(0, 1, 0) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            currentObject.position += new Vector3(0, -1, 0) * Time.deltaTime;
        }
    }
}
public void SetCurrentObj(Transform obj){     currentObject = obj;}

//每10秒创建一次立方体,你可以控制它

public GameObject prefab;
public float tik;
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if(Time.time>tik)
    {
        GameObject temp = Instantiate(prefab,transform.position,Quaternion.identity);
        FindObjectOfType<Controller>().SetCurrentObj(temp.transform);
        tik = Time.time + 10;
    }
}

推荐阅读