如何按名称找到所有特定的子游戏对象并将它们添加到列表中?
How can I find all the specific child gameobjects by name and add them to a list?
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
[Header("Global Rotation")]
[Space(5)]
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public List<GameObject> allObjects;
[Space(5)]
[Header("Rotation Mode")]
[LabeledBool("Global Rotation", "Individual Rotation")]
[SerializeField]
bool _rotationMode = true;
[Header("Individual Rotation")]
[Space(3)]
public SpinableObject[] individualObjects;
private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform t in transform)
{
}
}
// Update is called once per frame
void Update()
{
if (_rotationMode == false)
{
foreach (var spinner in individualObjects)
spinner.RotateObject();
}
else
{
for (int i = 0; i < allObjects.Count; i++)
{
RotateAllObjects(allObjects[i].transform);
}
}
}
private void RotateAllObjects(Transform t)
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
在“开始”中,我想按名称而不是标签添加子对象 "Propeller"
我有 4 个子对象:"Propeller1"、"Propeller2"、"Propeller3"、"Propeller4"
我想将这 4 个对象添加到 allObjects
所以它只会旋转脚本附加到的这个对象的 4 个属性。因为我要将对象克隆到更多对象,所以我不想使用 FindByTag。
如果要添加包含脚本的游戏对象的所有children:
private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform child in transform)
{
allObjects.Add(child.gameObject)
}
}
如果您想从其他游戏对象添加 children 的其他选项:
您可以按名称查找游戏对象。甚至 children 使用路径。例如:
aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
如果您的 children 有您举的例子的名字,您可以这样做:
String Prefix = Propeller;
String nameGO;
for(int i = 1; i < 4; i++)
{
nameGO = Propeller + i;
GameObject mGameObject = transform.Find("PathToChildren/nameGO");
//Now rotate or do something with it
}
如果这只是一次性调用,我建议使用此代码段:
// names you want to search for
string[] searchForNames = new string[] { "Propeller1" , "Propeller2" , "Propeller3" , "Propeller4" };
// list of objects that matches the search
List<GameObject> wantedObjects = new List<GameObject>();
// placeholder for all objects in the current scent
List<GameObject> allObjects = new List<GameObjects>();
// retrieve all objects from active scene ( wont retrieve objects marked with DontDestroyOnLoad from other scenes )
SceneManager.GetActiveScene()GetRootGameObjects( allObjects );
// iterate through all objects found in the current scene
foreach(GameObject obj in allObjects)
{
// check if name is contained by searchForNames array
if(searchForNames.Contains(obj.name))
{
// add to the matching list
wantedObjects.Add(obj);
}
}
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
[Header("Global Rotation")]
[Space(5)]
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public List<GameObject> allObjects;
[Space(5)]
[Header("Rotation Mode")]
[LabeledBool("Global Rotation", "Individual Rotation")]
[SerializeField]
bool _rotationMode = true;
[Header("Individual Rotation")]
[Space(3)]
public SpinableObject[] individualObjects;
private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform t in transform)
{
}
}
// Update is called once per frame
void Update()
{
if (_rotationMode == false)
{
foreach (var spinner in individualObjects)
spinner.RotateObject();
}
else
{
for (int i = 0; i < allObjects.Count; i++)
{
RotateAllObjects(allObjects[i].transform);
}
}
}
private void RotateAllObjects(Transform t)
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
在“开始”中,我想按名称而不是标签添加子对象 "Propeller" 我有 4 个子对象:"Propeller1"、"Propeller2"、"Propeller3"、"Propeller4"
我想将这 4 个对象添加到 allObjects 所以它只会旋转脚本附加到的这个对象的 4 个属性。因为我要将对象克隆到更多对象,所以我不想使用 FindByTag。
如果要添加包含脚本的游戏对象的所有children:
private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform child in transform)
{
allObjects.Add(child.gameObject)
}
}
如果您想从其他游戏对象添加 children 的其他选项:
您可以按名称查找游戏对象。甚至 children 使用路径。例如:
aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
如果您的 children 有您举的例子的名字,您可以这样做:
String Prefix = Propeller;
String nameGO;
for(int i = 1; i < 4; i++)
{
nameGO = Propeller + i;
GameObject mGameObject = transform.Find("PathToChildren/nameGO");
//Now rotate or do something with it
}
如果这只是一次性调用,我建议使用此代码段:
// names you want to search for
string[] searchForNames = new string[] { "Propeller1" , "Propeller2" , "Propeller3" , "Propeller4" };
// list of objects that matches the search
List<GameObject> wantedObjects = new List<GameObject>();
// placeholder for all objects in the current scent
List<GameObject> allObjects = new List<GameObjects>();
// retrieve all objects from active scene ( wont retrieve objects marked with DontDestroyOnLoad from other scenes )
SceneManager.GetActiveScene()GetRootGameObjects( allObjects );
// iterate through all objects found in the current scene
foreach(GameObject obj in allObjects)
{
// check if name is contained by searchForNames array
if(searchForNames.Contains(obj.name))
{
// add to the matching list
wantedObjects.Add(obj);
}
}