【问题标题】:Change color of multiple game objects in Unity 5在 Unity 5 中更改多个游戏对象的颜色
【发布时间】:2016-04-01 03:05:56
【问题描述】:

我想在 Unity 中使用单个脚本更改多个游戏对象的颜色。我有点迷失在如何做到这一点。我是 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 的新手,我很乐意接受任何帮助,如果它不友好就更好了。

谢谢

【问题讨论】:

    标签: c# colors gameobject


    【解决方案1】:

    您可以通过几种不同的方式来执行此操作。

    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. 如果少于 20 个,您可以创建一个列表,然后在将列表的大小更改为您拥有的对象数量后,将每个变换拖放到检查器中。

      /*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();
    }
    

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

    【讨论】:

    • 这会改变全局颜色而不是单个 GO 的颜色。为了证明这一点,将相同的着色器颜色附加到另一个 GO。运行时会看到两个 GO 的颜色相同。这会改变材质颜色而不是 GO 颜色本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2022-11-24
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多