unity3d如何改变实例化预制件的颜色

时间:2018-02-17 18:11:09

标签: c# unity3d colors

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

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 () {

}

}

我的预制件:
this is my prefab

修改的:
请注意,我的预制件不使用默认着色器。这是一个问题吗?

这是预制的着色器:

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

1 个答案:

答案 0 :(得分:1)

通过渲染器设置颜色

    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种材料,一种是黄色,一种是黑色。然后使用我提供的第二个替代解决方案作为代码在运行时更改材料。而不是我写的地方#34; yourNewMaterial&#34;它应该是黑色材料的材料变量的名称。

您不应该使用默认着色器,这不是问题。