【发布时间】:2019-11-04 20:46:43
【问题描述】:
我在 Unity 3d 中制作一个简单的游戏,基本上你有一个背景对象,上面有更多游戏对象,你要做的就是将这些游戏对象拖到一些垃圾桶中,只要你用正确的对象说对象被删除,游戏的理念是当您将所有对象放入正确的罐中时,您就赢得了游戏
我已经完成了所有的拖拽和验收,但我不知道如何制作脚本,这样当没有更多对象时,你就赢了。
我一直试图在一个空的游戏对象上使用层次结构,它是你必须销毁的对象的父对象,但我不知道如何实际删除对象以便代码可以注册它,有什么帮助吗?
拖拽代码和我用于垃圾桶的代码:
using UnityEngine;
using UnityEngine.EventSystems;
public class arrastrar : MonoBehaviour, IDragHandler
{
public float z = 0.0f;
public void OnDrag(PointerEventData eventData)
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = z;
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
}
}
还有垃圾桶里的代码
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class botarBasura: MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "cubo")
Destroy(other.gameObject);
} else if (other.gameObject.name == "escombros")
{
other.gameObject.transform.position = new Vector3(-1, 2.5f, -2); //If its not the correct trash can it returns the object to the starting position
}
} else if ( other.gameObject.name == "botellas")
{
other.gameObject.transform.position = new Vector3(0, 2.5f, -2);
}
}
}
【问题讨论】:
-
所有游戏对象都有一个继承的
.Destroy()方法,您可以使用它来销毁它们。不过,我建议禁用它们。一般来说,最好将您的对象池化并在不活动时隐藏它们,将它们返回到池中。您希望尽可能避免在游戏过程中实例化。一些实例化/销毁在性能方面并不明显,但是当您开始处理数百或数千个游戏对象并且您的垃圾收集器正在拉加超时时,性能可能会变得相当粗糙。最好尽早习惯池化的概念,因为它不太复杂 -
可拖动对象上是否有某种只有它们才有的组件/脚本/标签?
-
是的@Ruzihm 他们有,他们有一个 Drag 脚本,好吧,顾名思义就是用来拖动对象的,我把它附在下面
-
@DetectivePikachu 除非你有数百个对象,否则池是多余的,这里可能只是不必要的复杂性