C# 实例化并设置为子游戏对象的父对象
我有一个多层次的游戏对象层次结构,定义如下(如图所示)。下面的脚本我附加到GameObjectC# 实例化并设置为子游戏对象的父对象,c#,unity3d,C#,Unity3d,我有一个多层次的游戏对象层次结构,定义如下(如图所示)。下面的脚本我附加到GameObjectMain。如何通过搜索Main层次结构中的字符串来实例化gameObject并将其设置为gameObject05的子对象 public string ParentObjName = "05"; public void Instantiate () { GameObject 06 = UnityEditor.AssetDatabase.Loa
Main
。如何通过搜索Main
层次结构中的字符串来实例化gameObject并将其设置为gameObject05
的子对象
public string ParentObjName = "05";
public void Instantiate () {
GameObject 06 = UnityEditor.AssetDatabase.LoadAssetAtPath ("Assets/Resources/06.prefab", typeof (GameObject)) as GameObject;
GameObject go = (GameObject) GameObject.Instantiate (06 , Vector3.zero, Quaternion.identity);
go.transform.parent = ParentObjName.GameObject.transform; //Add to GameObject 05
}
如果游戏对象
Main
是预制,或者如果在编辑器时它已经存在于场景中,则最好参考转换容器05
public Transform Container05;
它消除了Unity必须搜索其已具有引用的特定变换的事实
如果不可能(例如,您正在运行时实例化Main及其子级),您可以搜索以下命名子级:
Transform container = GameObject.Find("05").transform;
只有当你只有一个名为“05”的游戏对象时,它才起作用,否则你会得到其中一个
最后一种方法是递归地在层次结构中搜索您的孩子。
参见derHugo answer,了解良好的递归实现
最后,当你实例化你的游戏对象时,你可以通过使用genericinstantiate
方法直接传递给父对象,避免施放
Transform container05 = FindTransformByNameRecursively("05",transform);
GameObject prefab06 = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Resources/06.prefab", typeof(GameObject)) as GameObject;
var new06 = Instantiate<GameObject>(prefab06, Vector3.zero, Quaternion.identity, container05);
transformcontainer05=findtransformbyname递归(“05”,Transform);
GameObject Prefact06=UnityEdit.AssetDatabase.LoadAssetPath(“资产/资源/06.Prefact”,typeof(GameObject))作为GameObject;
var new06=实例化(prefact06,Vector3.zero,Quaternion.identity,container05);
如果您已经有了对Main
的引用,那么您所需要的就是递归的,例如
public static class TransformExtensions
{
public static Transform TryFindRecursive(this Transform transform, string childName, out Transform result)
{
// Searches for childName in the first level children of the current transform
result = transform.Find(childName);
// I we found one return immediately
if(result)
{
return true;
}
// Otherwise iterate through the first level children of the current transform
foreach(var child in transform)
{
// Search for childName recursive
if(TryFindRecursive(child, childName, out result))
{
// If we found one return
return true;
}
}
return false;
}
}
if(!MainReference.TryFindRecursive(ParentObjName, out var parentTransform)
{
Debug.LogError($"No object found under \"{MainReference.name}\" that is called \"{ParentObjName}\"!");
return;
}
go.transform.parent = parentTransform;
那么假设你在某处有参考资料
Transform MainReference;
你可以像使用它一样使用它
public static class TransformExtensions
{
public static Transform TryFindRecursive(this Transform transform, string childName, out Transform result)
{
// Searches for childName in the first level children of the current transform
result = transform.Find(childName);
// I we found one return immediately
if(result)
{
return true;
}
// Otherwise iterate through the first level children of the current transform
foreach(var child in transform)
{
// Search for childName recursive
if(TryFindRecursive(child, childName, out result))
{
// If we found one return
return true;
}
}
return false;
}
}
if(!MainReference.TryFindRecursive(ParentObjName, out var parentTransform)
{
Debug.LogError($"No object found under \"{MainReference.name}\" that is called \"{ParentObjName}\"!");
return;
}
go.transform.parent = parentTransform;
通常,您应该存储此引用,而不是每次都反复运行此递归搜索