private IEnumerator DoGenerate() { if (m_Units != null && m_Units.Length > 0) { for (int iUnit = 0; iUnit < m_Units.Length; iUnit++) { if (iUnit % 5 == 0) { yield return(null); } UnitData unitData = m_Units[iUnit]; StarTmplete starTmplete = unitData.m_StarTmplete; GameObject obj = UnityEditor.PrefabUtility.InstantiatePrefab(starTmplete.m_Templete) as GameObject; obj.transform.SetParent(transform); obj.transform.localPosition = unitData.m_Position; obj.transform.localEulerAngles = unitData.m_Rotation; } } yield return(null); if (!string.IsNullOrEmpty(m_AssetPath) && m_Parent != null) { PrefabUtility.SaveAsPrefabAssetAndConnect(m_Parent.gameObject, m_AssetPath, InteractionMode.AutomatedAction); } }
private void Init() { if (m_TotalCount > 0) { float totalWeight = 0; int[] unitCounts = new int[m_StarMapTempletes.Count]; for (int iTemplate = 0; iTemplate < m_StarMapTempletes.Count; iTemplate++) { totalWeight += m_StarMapTempletes[iTemplate].m_Weight; unitCounts[iTemplate] = 0; } m_Units = new UnitData[m_TotalCount]; for (int iTotal = 0; iTotal < m_TotalCount; iTotal++) { float randomWeight = UnityEngine.Random.Range(0, totalWeight); int templateIndex = 0; for (int iTemplate = 0; iTemplate < m_StarMapTempletes.Count; iTemplate++) { randomWeight -= m_StarMapTempletes[iTemplate].m_Weight; if (randomWeight < 0) { templateIndex = iTemplate; break; } } StarTmplete starTmplete = m_StarMapTempletes[templateIndex]; Vector3 position = m_Path.EvaluateInBezier_LocalSpace((iTotal + 1.0f) / m_TotalCount); position.x += Random.Range(-m_OffestX, m_OffestX); position.y += Random.Range(-m_OffestY, m_OffestY); position.z += Random.Range(-m_OffestZ, m_OffestZ); Vector3 rotation = Vector3.zero; if (starTmplete.m_RandX) { rotation.x = Random.Range(0f, 360f); } if (starTmplete.m_RandY) { rotation.y = Random.Range(0f, 360f); } if (starTmplete.m_RandZ) { rotation.z = Random.Range(0f, 360f); } UnitData data = new UnitData(); data.m_Position = position; data.m_Rotation = rotation; data.m_StarTmplete = starTmplete; m_Units[iTotal] = data; } } m_GenerateEnum = DoGenerate(); }