我正致力于模块化太空飞船,可由玩家自定义。船上有硬点,有模块,可以容纳更多的模块等等。
此时硬点的代码看起来非常简单;
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
仅包含模块化对象的类型。从场景中的存储库加载(我知道它很乱,我以后会解决它。)
答案 0 :(得分:1)
感谢CùĐứcHiếu我发现我正在寻找错误的区域。在解压缩期间,我循环通过实际预制,而不是变换。固定代码:
foreach (Transform child in 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++;
}
}
}