2016-07-21 10 views
0

내 생각은 내 게임에서 색 얼룩을 만드는 것이었다. 나는 스택을 가지고 있으며 각각의 새로운 스폰 된 객체는 다른 색상으로 스폰되어야합니다. 슬프게도 나는 이것을 스택에 적용하는 방법을 모른다.Unity에서 GetComponent를 사용하여 Unity C#으로 인스턴스화 하시겠습니까?

내 코드 :

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); 
    } 

가 어떻게 지금 색상 변경 사항을 적용합니까? tileEmpty.Push ... 내 for 루프 이후에 있어야한다는 것을 알고 있지만 어떻게? 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); 
+1

크리스, 당신은 메시와 장난하는 이유가있다?! 오브젝트의 색상을 변경하려면 ".color"를 변경하는 것뿐입니다 ...?!?! – Fattie

+0

https://youtu.be/lWFp8Qy9BD8 어쩌면이게 내가 시도하고있는 것의 아이디어를 얻는 데 도움이 될 것입니다. 객체를 생성 할 때마다 다른 색상으로 부드럽게 색상을 변경하려고합니다. – ChrisD

+0

'MeshFilter'는 **입니다. ** 색상에 책임이 없습니다. 'MeshRenderer'가 있습니다. Yu Liu의 답변보기. –

답변

1

enter image description here

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)); 
    } 
} 
+1

오류와 저의 가난한 영어에 대해 유감스럽게 생각합니다. Stackoverflow를 처음 사용합니다. –