unity3d如何更改实例化预制件的颜色 [英] unity3d how to change color of instantiated prefab

查看:1467
本文介绍了unity3d如何更改实例化预制件的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个统一的预制件,我想实例化它,并将实例化的预制件的颜色更改为黑色.预制件的默认颜色是黄色.我的目标是将颜色更改为随机颜色,但现在只是黑色.
稍后再创建随机颜色.但是有一个问题:颜色不会改变.我已经检查了一些可在互联网上找到的解决方案,但是它们都不适合我.
这是我的代码:

In unity, I have a prefab, and I want to instantiate it and change colors of instantiated prefabs to black. The prefab's default color is yellow. My goal is to change color to random color, but now it's just black.
I'll make random colors later. But there is a problem: the color doesn't change. I've checked some solutions fount on the internet, but none of them works for me.
Here is my code:

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

public class starsGenerator : MonoBehaviour {

public float square_size;
public int stars_in_square;
public int galaxy_size;
public Transform star;


void generateGalaxy() {
    float rootSquareX = -(square_size/2.0f);
    float rootSquareY = (square_size/2.0f);



    for(int i = 0; i < galaxy_size; i++)
    {
        for (int j = 0; j < galaxy_size; j++)
        {
            Color color = Color.black;

            float sqx = rootSquareX + (i * square_size);
            float sqy = rootSquareY + (j * square_size);

            for(int k = 1; k <= stars_in_square; k++)
            {
                float strx = Random.Range(sqx + 0.2f, sqx + square_size - 0.2f);
                float stry = Random.Range(sqy + 0.2f, sqy + square_size - 0.2f);

                Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f);


                Transform instd = Instantiate(star, new Vector3(strx, stry, 4.44f), Quaternion.identity);

                instd.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red);
                // also instd.GetComponent<MeshRenderer>().material.color = color; doesn't work
            }
        }
    }
}

// Use this for initialization
void Start () {
    generateGalaxy();

}

// Update is called once per frame
void Update () {

}

}

我的预制件:

And my prefab:

编辑:
请注意,我的预制件不使用默认着色器.这有问题吗?

EDIT:
Please note that my prefab doesn't use default shader. Is this a problem?

这是预制的着色器:

Shader "Custom/GlowShader" 
{
 Properties 
 {
  _ColorTint("Color Tint", Color) = (1, 1, 1, 1)
  _MainTex("Base (RGB)", 2D) = "white" {}
  _BumpMap("Normal Map", 2D) = "bump" {}
  _RimColor("Rim Color", Color) = (1, 1, 1, 1)
  _RimPower("Rim Power", Range(1.0, 6.0)) = 3.0

 }
 SubShader {

  Tags { "RenderType"="Opaque" }

  CGPROGRAM
  #pragma surface surf Lambert

  struct Input {

   float4 color : Color;
   float2 uv_MainTex;
   float2 uv_BumpMap;
   float3 viewDir;

  };

  float4 _ColorTint;
  sampler2D _MainTex;
  sampler2D _BumpMap;
  float4 _RimColor;
  float _RimPower;

  void surf (Input IN, inout SurfaceOutput o) 
  {


   IN.color = _ColorTint;
   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color;
   o.Normal = UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap));

   half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
   o.Emission = _RimColor.rgb * pow(rim, _RimPower);


  }
  ENDCG
 } 
 FallBack "Diffuse"
}

推荐答案

通过渲染器设置颜色

    GameObject go = Instantiate(_ShieldPrefab);
    Renderer rend = go.GetComponent<Renderer>();
    rend.material.color = Color.black;

或者更改材质.

    GameObject go = Instantiate(thePrefabYouInstantiate);
    Renderer rend = go.GetComponent<Renderer>();
    rend.material = yourNewMaterial;

我的意思是.制作2种材料,一种是黄色的,一种是黑色的.然后使用我提供的第二种替代解决方案作为代码在运行时更改材质.而不是我写"yourNewMaterial"的地方,它应该是黑色材料的材料变量的名称.

What I mean is. Make 2 materials, one that is yellow, one that is black. Then use the 2nd alternative solution I provided as code to change the material during runtime. Just instead of where I wrote "yourNewMaterial" it should be the name of your material variable for the black material.

不使用默认着色器应该没问题.

It shouldn't be a problem that you don't use the default shader.

这篇关于unity3d如何更改实例化预制件的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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