0

我的想法是在我的游戏中创建一个 Color lerp。我有一个堆栈,每个新生成的对象都应该以不同的颜色生成。可悲的是,我不知道如何在堆栈中应用它。

我的代码:

private void ColorMesh(Mesh mesh)
{
    Vector3[] vertices = mesh.vertices;
    Color32[] colors = new Color32[vertices.Length];

    float f = Mathf.Sin(Time.deltaTime * 0.25f);

    for (int i = 0; i < vertices.Length; i++)
        colors[i] = Lerp4(gameColors[0], gameColors[1], gameColors[2], gameColors[3], f);
    mesh.colors32 = colors;
}

private Color32 Lerp4(Color32 a, Color32 b, Color32 c, Color32 d, float t)
{
    if (t < 0.33f)
        return Color.Lerp(a, b, t / 0.33f);
    else if (t < 0.66f)
        return Color.Lerp(b, c, (t - 0.33f) / 0.33f);
    else
        return Color.Lerp(c, d, (t - 0.66f) / 0.66f);
}

public void CreateTiles(int amount)
{
    for (int i = 0; i < amount; i++)
    {
        //Creates a tile and adds it to the stack
        tileEmpty.Push(Instantiate(tilePrefabs[0]));
        tileEmpty.Peek().name = "TileEmpty";
        tileEmpty.Peek().SetActive(false);
    }

我现在如何应用颜色更改?我知道它必须在我的 for 循环中的 tileEmpty.Push... 之后,但是如何?我尝试过的所有东西都GetComponent<MeshFilter>().mesh;不起作用......我所有生成的对象都有相同的颜色。

我的完整脚本如下所示。

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

public class SpawnScript : MonoBehaviour
{

    public Color32[] MColors;

    public GameObject[] tilePrefabs;
    public GameObject currentTile;

    private static SpawnScript instance;

    private Stack<GameObject> tileEmpty = new Stack<GameObject>();
    public Stack<GameObject> TileEmpty
    {
        get { return tileEmpty; }
        set { tileEmpty = value; }
    }

    public static SpawnScript Instance
    {
        get
        {
            if (instance == null) //Finds the instance if it doesn't exist
            {
                instance = GameObject.FindObjectOfType<SpawnScript>();
            }

            return instance;
        }
    }

    void Start()
    {


        //Creates 100 tiles
        CreateTiles(20);

        //Spawns 50 tiles when the game starts
        for (int i = 0; i < 10; i++)
        {
            SpawnTile();
        }

    }

    public void CreateTiles(int amount)
    {

        for (int i = 0; i < amount; i++)
        {

            GameObject Tile = Instantiate<GameObject>(tilePrefabs[0]);

            //Creates a tile and adds it to the stack
            tileEmpty.Push(Tile);
          //  ColorMesh(tilePrefabs[0].transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh);
         //With this all tiles have the same color  

            //Sets the name of the visibility of the tiles
            tileEmpty.Peek().name = "TileEmpty";
            tileEmpty.Peek().SetActive(false);
        }
    }

    public void SpawnTile()
    {
        //If we are out of tiles, then we need to create more
        if (tileEmpty.Count == 0)
        {
            CreateTiles(10);
        }

        //Generating a random number between 0 and 1
        //I am using this because I want to add an other tile later
        int randomIndex = Random.Range(0, 1);
        if (randomIndex == 0) //If the random number is one then spawn a left tile
        {
            GameObject tmp = tileEmpty.Pop();
            tmp.SetActive(true);  
            tmp.transform.position = currentTile.transform.GetChild(0).transform.GetChild(0).position;
            currentTile = tmp;
        }
    }

    public void ColorMesh(Mesh mesh)
    {
        Vector3[] vertices = mesh.vertices;
        Color32[] colors = new Color32[vertices.Length];

        float f = Mathf.Sin(Time.deltaTime * 0.25f);

        for (int i = 0; i < vertices.Length; i++)
            colors[i] = Lerp4(MColors[0], MColors[1], MColors[2], MColors[3], f);
        mesh.colors32 = colors;
    }

     public Color32 Lerp4(Color32 a, Color32 b, Color32 c, Color32 d, float t)
      {
          if (t < 0.33f)
              return Color.Lerp(a, b, t / 0.33f);
          else if (t < 0.66f)
              return Color.Lerp(b, c, (t - 0.33f) / 0.33f);
          else
              return Color.Lerp(c, d, (t - 0.66f) / 0.66f);
      }    
}

这条线是问题所在:

ColorMesh(tilePrefabs[0].transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh);
4

1 回答 1

1

在此处输入图像描述

using UnityEngine;
using System.Collections;

public class CubeGenerate : MonoBehaviour {

    public GameObject MCube;
    public Stack MCubeStack = new Stack();
    public Color[] MColors;
    public int MCubeCount;

    private int _mColorCount
    {
        get
        {
            return MColors.Length;
        }
    }
    // Use this for initialization
    void Awake () {
        StartCoroutine(GenerateCube());
    }

    IEnumerator GenerateCube() {
        for (int i = 0;i< MCubeCount; i++)
        {
            GameObject Cube = Instantiate<GameObject>(MCube);
            Cube.GetComponent<MeshRenderer>().material.color = Lerp4(MColors[0], MColors[1], MColors[2], MColors[3],i);
            Cube.name = i.ToString();
            Cube.transform.position = new Vector3(0f, 0.25f * i, 0f);
            MCubeStack.Push(Cube);
            yield return new WaitForEndOfFrame();
        }
    }

    private Color32 Lerp4(Color32 a, Color32 b, Color32 c, Color32 d, int t)
    {
        float r = (float)(_mColorCount - 1);

        if (t <= MCubeCount / r)
            return Color.Lerp(a, b, (float)t / r);
        else if (t < MCubeCount / r * 2f)
            return Color.Lerp(b, c, (t - MCubeCount / r) / 3f  );
        else
            return Color.Lerp(c, d, (t - MCubeCount / r * 2f) / (_mColorCount - 1));
    }
}
于 2016-07-22T02:02:56.947 回答