您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

75 行
1.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PopulateScene : MonoBehaviour {
public GameObject m_ObjectPrefab; //
public Transform m_CenterPoint;
public Material m_Material;
public int m_GridWidth = 16;
public float m_Spacing = 0.6f;
public static bool m_DifferentColors = true;
private GameObject[] m_Objects; //
// Use this for initialization
void Start()
{
m_Objects = new GameObject[m_GridWidth * m_GridWidth];
int iIndex = 0;
for (int z = 0; z < m_GridWidth; z++)
{
for (int x = 0; x < m_GridWidth; x++)
{
// int i = z * m_CubesRow + x;
float fX = (x - (m_GridWidth / 2)) * 2;
float fY = 0.0f;
float fZ = (z - (m_GridWidth / 2)) * 2;
Vector3 vPos = new Vector3(fX * m_Spacing, fY, fZ * m_Spacing);
vPos += m_CenterPoint.position;
m_Objects[iIndex] = Instantiate(m_ObjectPrefab, vPos, m_CenterPoint.rotation) as GameObject;
if (m_DifferentColors)
{
Renderer renderer = m_Objects[iIndex].GetComponent<Renderer>();
Material mat = Instantiate(m_Material);
Color oColor = new Color(Random.value, Random.value, Random.value, 1.0f);
mat.color = oColor;
renderer.material = mat;
}
iIndex++;
}
}
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
GUI.changed = false;
m_DifferentColors = GUI.Toggle(new Rect(10, 10, 200, 20), m_DifferentColors, "One color per object");
if (GUI.changed)
{
SceneManager.LoadScene("BasicBatching");
}
}
}