【发布时间】:2016-11-29 08:15:15
【问题描述】:
我正在开发模块化太空船,可由玩家自定义。一艘船有挂载点,它可以容纳模块,而这些模块又可以容纳更多的模块等等。
此时挂点的代码看起来很简单;
public class Hardpoint : MonoBehaviour {
public GameObject holds; //this holds the prefab
public ComponentObject.Type[] canHold;
private GameObject heldInstance; //this holds an instance of the prefab
public void SpawnComponent() {
Clear();
heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
heldInstance.transform.SetParent(transform);
}
public void RollThroughDecompression(CompressedComponent c) {
heldInstance.GetComponent<ComponentObject>().Decompress(c);
}
public void Clear() {
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
}
}
然而,这一切都像是一个预制件。因为我收到的错误消息是:
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
和
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
此时我完全迷失了。谁能指出我为什么会不断出现这些错误的正确方向?
编辑: 一些截图。
预期结果:
实际结果:
这一切都始于EmptyHardpoint。如您所见,它确实生成了驾驶舱,并将EmptyHardpoint 设置为父级。但这就是乐趣结束的地方。进一步 Gameobjects 被当作预制件处理。
解压代码:
public void Decompress(CompressedComponent c) {
componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
componentNumber = c.componentNumber;
UpdateHardPoints();
GameObject[] typeRepository = GetRepository(componentType);
//update children
int point = 0;
foreach (Transform child in typeRepository[componentNumber].transform)
{
Hardpoint hardpoint = child.GetComponent<Hardpoint>();
if (hardpoint != null) {
Debug.Log("Hardpoint found in " + child.transform.parent.name);
if (c.hardpoints[point] != null) {
//get the hardpoint's repository
GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
//set the hardpoint to hold this object
hardpoint.holds = hardpointRepo[c.hardpoints[point].componentNumber];
hardpoint.SpawnComponent();
hardpoint.RollThroughDecompression(c.hardpoints[point]);
point++;
}
}
}
}
CompressedComponent 仅保存模块化对象的类型。从场景中的存储库加载(我知道它很乱,我稍后会解决。)
【问题讨论】:
-
您能否向我们展示导致这些错误的示例船的场景层次结构的屏幕截图?这些错误是否仅在您调用
Clear()时引发? -
你
Instantiate你的Hardpoint了吗? -
@ Serlite
Clear()只抛出第一条错误消息,我在设置父级时得到第二条。 @Cù Đức Hiếu 我真的不知道说实话。我正在使用网络和作为播放器,我使用带有附加脚本的hardpoint来控制移动。 -
那么我有充分的理由怀疑你的
Hardpoint是prefab。当然,GameObject上的大多数操作在Prefab上被设计为禁用。您可以继续调查以揭开我的怀疑,因为使用提供的信息很难从我这边进一步调试。 -
在编辑器中它确实说(克隆)所以我怀疑它被实例化了。我将在问题中添加更多代码和屏幕截图。感谢您迄今为止的帮助。
标签: unity3d