新创建的克隆预制件应具有与主色相同的颜色 [英] New created clone prefab should have the same color as the main one

查看:100
本文介绍了新创建的克隆预制件应具有与主色相同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要创建一个prefab.当我按r时,prefab的颜色为红色. 现在,我按下按钮c并克隆prefab.现在,克隆的颜色默认为白色.但是第一个prefab具有红色,因此克隆也应具有相同的颜色. 我尝试获取prefab的颜色,然后将其提供给克隆,不幸的是未成功.

First I'm creating a prefab. When I press r the color of the prefab is red. Now, I press the button c and the prefab is being cloned. The color of the clone is now white as default. But the first prefab has the color red so the clone should also have the same color. I tried to get the color of the prefab and then give it to the clone, unfortunately unsuccessfully.

我的问题是,现在如何为预制件的克隆提供与主预制件相同的颜色?例如,当我创建三个预制件的克隆并按b时,所有四个预制件应为蓝色.

My question is now, how can I give the clone of the prefab the same color as the main prefab? When I create three clones of prefabs and I press b for example, all four prefabs should be blue.

在下面查看我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class globalControl : MonoBehaviour
{
    public GameObject prefabInstance;
    List<Object> prefabInstanceClones = new List<Object>();
    GameObject capsule, sphere, cylinder;

    private void Start()
    {
        capsule = GameObject.Find("Capsule");
        sphere = GameObject.Find("Sphere");
        cylinder = GameObject.Find("Cylinder");
    }
    void Update()
    {
        Debug.Log(prefabInstanceClones.Count);

        if (Input.GetKeyDown("c"))
        {
            prefabInstanceClones.Add(Instantiate(prefabInstance, transform.position, Quaternion.identity));
        }

        if (Input.GetKeyDown("d"))
        {
            var last = prefabInstanceClones[prefabInstanceClones.Count - 1];
            prefabInstanceClones.Remove(last);
            Destroy(last);
        }


        if (Input.GetKeyDown("r"))
        {
            capsule.GetComponent<Renderer>().material.color = Color.red;
            sphere.GetComponent<Renderer>().material.color = Color.red;
            cylinder.GetComponent<Renderer>().material.color = Color.red;
        } else if (Input.GetKeyDown("b"))
        {
            capsule.GetComponent<Renderer>().material.color = Color.blue;
            sphere.GetComponent<Renderer>().material.color = Color.blue;
            cylinder.GetComponent<Renderer>().material.color = Color.blue;
        } else if (Input.GetKeyDown("g"))
        {
            capsule.GetComponent<Renderer>().material.color = Color.green;
            sphere.GetComponent<Renderer>().material.color = Color.green;
            cylinder.GetComponent<Renderer>().material.color = Color.green;
        }
    }

}

先谢谢您!期待您的回音. :)

Thank you in advance! Looking forward to hearing from you. :)

推荐答案

  public GameObject prefabInstance;
List<Object> prefabInstanceClones = new List<Object>();
//GameObject capsule, sphere, cylinder;

private void Start() {
    //capsule = GameObject.Find("Capsule");
    //sphere = GameObject.Find("Sphere");
    //cylinder = GameObject.Find("Cylinder");
}
void Update() {
    Debug.Log(prefabInstanceClones.Count);

    if (Input.GetKeyDown("c")) {
        prefabInstanceClones.Add(Instantiate(prefabInstance, transform.position, Quaternion.identity));
    }

    if (Input.GetKeyDown("d")) {
        var last = prefabInstanceClones[prefabInstanceClones.Count - 1];
        prefabInstanceClones.Remove(last);
        Destroy(last);
    }


    if (Input.GetKeyDown("r")) {

        prefabInstance.GetComponent<Renderer>().sharedMaterial.color = Color.red;
    } else if (Input.GetKeyDown("b")) {

        prefabInstance.GetComponent<Renderer>().sharedMaterial.color = Color.blue;

    } else if (Input.GetKeyDown("g")) {

        prefabInstance.GetComponent<Renderer>().sharedMaterial.color = Color.green;

    }
}

您可以使用().sharedMaterial此属性影响所有预制件的颜色.

You can use ().sharedMaterial this property effect all prefabs color.

这篇关于新创建的克隆预制件应具有与主色相同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆