代码之家  ›  专栏  ›  技术社区  ›  NAYIR55

更改Unity 5中多个游戏对象的颜色

  •  1
  • NAYIR55  · 技术社区  · 10 年前

    我想用一个脚本改变Unity中多个游戏对象的颜色。我不知道该怎么做。我是Unity的新手,这对我来说是一种基本的训练。

    统一版本:5.3.4

    观察到的行为:

    将相同的脚本添加到其他游戏对象中,并全部更改为相同的颜色

    预期行为:

    单独更改游戏对象的颜色

    尝试的内容列表:

    使用-FindGameObject-

    尝试使用-GameObject访问材料-

    同时尝试了两种方法

    在多个脚本中思考以实现我想要的结果

    这是密码

    C编号:

    using UnityEngine;
    using System.Collections;
    
    public class ChangeColor : MonoBehaviour 
    {
       //If I change these variables to -GameObject-
       //It blocks me to access the renderer
       //Making the variables public doesn't work either
       private Renderer cube;
       private Renderer sphere;
    
    void Start () 
    {
        //Tried here the -FindGameObjectWithTag-
        cube = GetComponent<Renderer>();
        sphere = GetComponent<Renderer>();
    }
    
    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            //Tried here the -FindGameObjectWithTag-
            cube.material.color = Color.red;
        }
    
        if(Input.GetKeyDown(KeyCode.S))
        {
            //Tried here the -FindGameObjectWithTag-
            sphere.material.color = Color.green;
        }
      }
    }
    

    也许我做错了什么,正如我所说的,我是Unity的新手,我很乐意接受任何帮助,如果这对我来说更好的话。

    谢谢

    1 回复  |  直到 10 年前
        1
  •  0
  •   outis    7 年前

    有几种不同的方法可以做到这一点。

    1. 如果它们都在同一个父对象下,没有其他对象,则可以循环通过子对象并更改它们的颜色

      GameObject Parent = GameObject.Find("ParentObject");
      for( int x = 0; x > Parent.transform.childCount; x++);
      {
        Parent.transform.GetChild(x).GetComponent<Renderer>().material.color = Color.red;
      }
      
    2. 如果少于20ish,您可以创建一个列表,并在将列表的大小更改为您拥有的对象数量后,将每个变换拖放到检查器中。

      /*Make sure you have Using "System.Collections.Generic" at the top */
      //put this outside function so that the inspector can see it
      public List<Transform> Objs;
      // in your function put this (when changing the color)
      foreach(Transform Tform in Objs){
        Tform.GetComponent<Renderer>().material.color = Color.red;
      }
      
    3. 类似于2,如果你想在代码中完成这一切,你可以做

      //Outside function
      public List<Transform> Objs;
      //inside function
      Objs.Add(GameObject.Find("FirstObject").transform);
      Objs.Add(GameObject.Find("SecondObject").transform);
      //... keep doing this
      //now do the same foreach loop as in 2
      
    4. 你可以通过标签进行搜索(如果你有很多对象)(我想这会花费更长的时间,因为它会通过每个组件,但我没有证据支持这一点)

      //Outside function
      public GameObject[] Objs;
      //inside function
      Objs = GameObject.FindGameObjectsWithTag("ATagToChangeColor");
      
      foreach(Transform Tform in Objs){
        Tform.GetComponent<Renderer>().material.color = Color.red();
      }
      

    关于这个评论:

    //If I change these variables to -GameObject-
    //It blocks me to access the renderer
    //Making the variables public doesn't work either
    

    如果您将其设置为GameObject类型,则只需执行以下操作即可轻松访问渲染器:

    public GameObject cube;
    public void Start(){
        cube.GetComponent<Renderer>().material.color = Color.red();
    }
    

    并且将变量公开允许unity的检查员查看变量,以便您可以在unity编辑器中更改它们,而无需打开脚本。