2017-07-25 64 views
0
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); 
    } 
} 

開始我內心想的名字沒有標註「螺旋槳」 我有4級孩子的對象添加子對象:「Propeller1」,「Propeller2」,「Propeller3」,「Propeller4」如何按名稱查找所有特定的兒童gameobjects並將它們添加到列表中?

我想將這4個對象添加到allObjects中 因此,它只會旋轉該腳本附加到的這個對象的4個指定者。由於我克隆的對象更多,我不想使用FindByTag。

回答

2

如果您要添加的遊戲物體的所有孩子的腳本內容:由

你可以找到GameObjects:

private void Start() 
    { 
     allObjects = new List<GameObject>(); 
     foreach(Transform child in transform) 
     { 
      allObjects.Add(child.gameObject) 
     } 
    } 

的情況下,其他的選項,你想從其他gameObjects添加子名稱。甚至還有孩子使用這條路。例如:

aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger"); 

如果你的孩子有你給的例子名字,你可以這樣做:

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 
} 
1

如果這只是一次電話我會建議使用這個片段:

// 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); 
    } 
} 
相關問題