Exemplo n.º 1
0
        void DrawLayerElement(Rect rect, int index, bool selected, bool focused)
        {
            rect.y = rect.y + kElementPadding;
            var rectButton = new Rect((rect.x + kElementPadding), rect.y, kElementToggleWidth, kElementToggleWidth);
            var rectImage  = new Rect((rectButton.x + kElementToggleWidth), rect.y, kElementImageWidth, kElementImageHeight);
            var rectObject = new Rect((rectImage.x + kElementImageWidth + 10), rect.y, kElementObjectFieldWidth, kElementObjectFieldHeight);

            if (m_PaletteLayers.Count > 0 && m_PaletteLayers.ElementAtOrDefault(index) != null)
            {
                m_PaletteLayers[index].IsSelected = EditorGUI.Toggle(rectButton, m_PaletteLayers[index].IsSelected);

                EditorGUILayout.BeginHorizontal();
                List <TerrainLayer> existLayers = m_PaletteLayers.Select(l => l.AssignedLayer).ToList();
                TerrainLayer        oldLayer    = m_PaletteLayers[index].AssignedLayer;
                Texture2D           icon        = null;
                if (m_PaletteLayers[index].AssignedLayer != null)
                {
                    icon = AssetPreview.GetAssetPreview(m_PaletteLayers[index].AssignedLayer.diffuseTexture);
                }
                GUI.Box(rectImage, icon);
                EditorGUI.BeginChangeCheck();
                m_PaletteLayers[index].AssignedLayer = EditorGUI.ObjectField(rectObject, m_PaletteLayers[index].AssignedLayer, typeof(TerrainLayer), false) as TerrainLayer;
                EditorGUILayout.EndHorizontal();

                if (EditorGUI.EndChangeCheck())
                {
                    if (m_PaletteLayers[index].AssignedLayer == null)
                    {
                        m_PaletteLayers.RemoveAt(index);
                        TerrainToolboxLayer.RemoveLayerFromTerrain(m_SelectedTerrain.terrainData, index);
                        m_SelectedTerrainLayer = null;
                    }
                    else if (existLayers.Contains(m_PaletteLayers[index].AssignedLayer) && m_PaletteLayers[index].AssignedLayer != oldLayer)
                    {
                        EditorUtility.DisplayDialog("Error", "Layer exists. Please select a different layer.", "OK");
                        m_PaletteLayers[index].AssignedLayer = oldLayer;
                    }
                    else
                    {
                        if (m_SelectedTerrain.terrainData.terrainLayers.Length < m_PaletteLayers.Count)
                        {
                            TerrainToolboxLayer.AddLayerToTerrain(m_SelectedTerrain.terrainData, m_PaletteLayers[index].AssignedLayer);
                        }
                        else
                        {
                            int            layersLength = m_SelectedTerrain.terrainData.terrainLayers.Length;
                            TerrainLayer[] layers       = m_SelectedTerrain.terrainData.terrainLayers;
                            layers[index]          = m_PaletteLayers[index].AssignedLayer;
                            m_SelectedTerrainLayer = m_PaletteLayers[index].AssignedLayer;
                            m_SelectedTerrain.terrainData.terrainLayers = layers;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 void RemoveSelectedLayerElements()
 {
     for (int i = m_PaletteLayers.Count - 1; i >= 0; i--)
     {
         if (m_PaletteLayers[i].IsSelected)
         {
             if (m_PaletteLayers[i].AssignedLayer != null)
             {
                 TerrainToolboxLayer.RemoveLayerFromTerrain(m_SelectedTerrain.terrainData, i);
             }
             m_PaletteLayers.RemoveAt(i);
         }
     }
     m_SelectedTerrainLayer = null;
     AssetDatabase.Refresh();
 }
        public void RemoveTerrainLayer_UndoShouldCorrectSplatTextures()
        {
            var terrainData = new TerrainData();

            terrainData.alphamapResolution = 16;
            terrainData.terrainLayers      = new[]
            {
                new UnityEngine.TerrainLayer(), new UnityEngine.TerrainLayer(), new UnityEngine.TerrainLayer()
            };
            int layerCount = 3;

            float[,,] map = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, layerCount];
            for (int i = 0; i < layerCount; i++)
            {
                for (int y = 0; y < terrainData.alphamapHeight; y++)
                {
                    for (int x = 0; x < terrainData.alphamapWidth; x++)
                    {
                        map[x, y, 0] = 0;
                    }
                }
            }
            // set magic pixels on all the layers so we can track what should be restored
            map[0, 0, 0] = 1.0f;
            map[0, 1, 1] = 1.0f;
            map[0, 2, 2] = 1.0f;

            terrainData.SetAlphamaps(0, 0, map);
            var terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent <Terrain>();

            // the package uses this function because it performs some prepass prior to deletion
            TerrainToolboxLayer.RemoveLayerFromTerrain(terrainData, 1);
            Undo.PerformUndo();
            terrainData.SyncTexture(TerrainData.AlphamapTextureName);
            var newMap = terrainData.GetAlphamaps(0, 0, terrainData.alphamapResolution, terrainData.alphamapResolution);

            for (int i = 0; i < layerCount; i++)
            {
                Assert.That(newMap[0, i, i], Is.EqualTo(1.0f));
            }
            GameObject.DestroyImmediate(terrain.gameObject);
        }
Exemplo n.º 4
0
        void RemoveLayerElement()
        {
            if (m_PaletteLayers.ElementAtOrDefault(m_LayerList.index) == null)
            {
                return;
            }

            m_PaletteLayers.RemoveAt(m_LayerList.index);

            if (m_SelectedTerrain.terrainData.terrainLayers.Length > m_LayerList.index)
            {
                TerrainToolboxLayer.RemoveLayerFromTerrain(m_SelectedTerrain.terrainData, m_LayerList.index);
            }

            m_LayerList.index = m_PaletteLayers.Count - 1;
            if (m_LayerList.index >= 0 && m_LayerList.index < m_PaletteLayers.Count)
            {
                m_SelectedTerrainLayer = m_PaletteLayers[m_LayerList.index].AssignedLayer;
            }
            else
            {
                m_SelectedTerrainLayer = null;
            }
        }