如何在Unity3D中删除GameObject并添加不同类型的对象

时间:2018-08-10 01:44:07

标签: c# unity3d unityscript game-development

我一直在Unity3D中弄乱,遇到障碍 我不能跳。 我正在尝试制作一个允许 我放置并删除GameObjects,而Im也在尝试创建ui,以便可以在不同类型的对象之间进行选择。

提前谢谢! http://pastebin.com/G8pz4ufr

using UnityEngine;

public class CubePlacer : MonoBehaviour
{
    private Grid grid;

    private void Awake()
    {
        grid = FindObjectOfType<Grid>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitInfo;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitInfo))
            {
                PlaceObjectNear(hitInfo.point);
            }
        }
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit hitInfo;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitInfo))
            {
                RemoveObjectNear(hitInfo.point);
            }
        }
    }

    public void PlaceObjectNear(Vector3 clickPoint)
    {
        var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
        GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;
    }
    public void RemoveObjectNear(Vector3 clickPoint)
    {
        var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
        GameObject.Destroy(GameObject, PrimitiveType.Cube);
    }
}

0 个答案:

没有答案