Beispiel #1
0
    private GameObject SpawnObjectEditor(Color32 colour, Vector3 position, PrefabDataList m_PrefabList)
    {
        if (colour.a <= 0)
        {
            return(null);
        }

        for (int prefabIndex = 0; prefabIndex < m_PrefabList.GetDataList.Length; prefabIndex++)
        {
            if (m_PrefabList.GetDataList[prefabIndex].m_Colour.Equals(colour))
            {
                // Find the parent object to attach the prefab to.
                Transform parentObject = gameObject.transform.Find(m_PrefabList.GetDataList[prefabIndex].m_Parent.ToString());
                if (parentObject == null)
                {
                    // If the parent can't be found, create one.
                    parentObject = new GameObject(m_PrefabList.GetDataList[prefabIndex].m_Parent.ToString()).transform;
                    parentObject.SetParent(gameObject.transform);
                }

                // Instantiate the prefab at the position.
                GameObject prefabObject = (GameObject)Instantiate(m_PrefabList.GetDataList[prefabIndex].m_Prefab, position, Quaternion.identity, parentObject);
                return(prefabObject);
            }
        }

        return(null);
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        EditorGUILayout.BeginVertical("box");
        {
            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField("Prefab", GUILayout.Width(40.0f));
                m_PrefabData = (PrefabDataList)EditorGUILayout.ObjectField(m_PrefabData, typeof(PrefabDataList), false);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField("Level", GUILayout.Width(40.0f));
                m_LevelTexture = (Texture2D)EditorGUILayout.ObjectField(m_LevelTexture, typeof(Texture2D), false);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("Generate Level"))
                {
                    ((LevelManager)target).GenerateLevel(m_LevelTexture, m_PrefabData);
                }
                else if (GUILayout.Button("Reset Level"))
                {
                    ((LevelManager)target).ResetLevel();
                }
            }
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndVertical();
    }
Beispiel #3
0
    public void GenerateLevel(Texture2D levelTexture, PrefabDataList prefabList)
    {
        Color32[] pixels = levelTexture.GetPixels32();

        int width  = levelTexture.width;
        int height = levelTexture.height;

        for (int yIndex = 0; yIndex < height; yIndex++)
        {
            for (int xIndex = 0; xIndex < width; xIndex++)
            {
                int index = xIndex + (yIndex * width);

                Vector3 position = new Vector3(xIndex - (width * 0.5f), 0.0f, yIndex - (height * 0.5f));
                SpawnObjectEditor(pixels[index], position, prefabList);
            }
        }

        SetupLevel();
    }