我正在尝试在Unity中创建程序创建的地牢。 我让它创建了地牢,并在每个预制墙,地板等预制件上附加了一个脚本。每个预制件都附加到一个父对象上,以方便删除和重建整个地牢。该脚本包含详细信息:
public class WallPrefab
{
public bool use;
private int roomId;
public WallTypes type; //enum = torch, switch, item etc
public WallDirections direction; //direction wall is facing
public GameObject myPrefab; //nested
}
预制件还包含一个空的游戏对象,用作火炬,开关等的安装点。
myPrefab
prefab //prefab for the actual 3d object
wallmount //empty game object to store location and rotation details
....
我的问题: 1.如何获取墙的类型?
int childs = transform.childCount;
for (var i = childs - 1; i >= 0; i--)
{
c = transform.GetChild(i).gameObject;
Debug.Log("child:" + c.name);
//WallTypes g = c.GetComponentInChildren<WallPrefab>().type;
WallTypes g = c.transform.GetComponentInChildren<WallSettings>().type;
(已编辑,可为我的问题的这一部分插入有效的解决方案!) 该代码不起作用,因为Unity抱怨:
ArgumentException:GetComponent要求所请求的组件 “ WallPrefab”源自MonoBehaviour或Component,或者是 界面。
一旦获得安装点,我知道如何获取位置和旋转,但是实际上如何获得安装点?
答案 0 :(得分:1)
问题1似乎很能说明问题。
问题2看起来更有趣。
如果我对您的理解正确,那么您就有一个脚本,可以在指定的安装点上“创建”割炬。假设这些坐骑点是空的游戏对象,并且您不想直接将火炬添加到预制件中(如果我们在此处详细了解地牢+坐骑点的确切生成方式,将非常好),我将添加一个脚本在预制件上,添加GameObject实例变量,然后使用检查器将安装点拖动到脚本上。那么我将使用“ gameobjectVariable.transform.position”来获取位置。
类似:
public class prefabClassWithMountPoints{
GameObject mountpoint; //drag mount point from inspector
GameObject torchGameObject; //drag torch prefab here
public void createTorch(){
GameObject torchObject = Instantiate(torchGameObject, mountpoint.transform.position, Quaternion.identity);
//...
}
//rest of code
}
这是假设安装点不是在预制件中随机生成的。