-
-
Save PixHammer/0360f1b3ff47888a5dd2e96d8d97f470 to your computer and use it in GitHub Desktop.
Helper to deep-copy a TerrainData object in Unity3D - Updated for Unity 2021.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Provides means to deep-copy a TerrainData object because Unitys' built-in "Instantiate" method | |
/// will miss some things and the resulting copy still shares data with the original. | |
/// </summary> | |
public class TerrainDataCloner { | |
/// <summary> | |
/// Creates a real deep-copy of a TerrainData | |
/// </summary> | |
/// <param name="original">TerrainData to duplicate</param> | |
/// <returns>New terrain data instance</returns> | |
public static TerrainData Clone(TerrainData original) { | |
TerrainData dup = new TerrainData(); | |
dup.alphamapResolution = original.alphamapResolution; | |
dup.baseMapResolution = original.baseMapResolution; | |
dup.detailPrototypes = CloneDetailPrototypes(original.detailPrototypes); | |
dup.enableHolesTextureCompression = original.enableHolesTextureCompression; | |
dup.heightmapResolution = original.heightmapResolution; | |
dup.size = original.size; | |
dup.terrainLayers = original.terrainLayers; | |
dup.wavingGrassAmount = original.wavingGrassAmount; | |
dup.wavingGrassSpeed = original.wavingGrassSpeed; | |
dup.wavingGrassStrength = original.wavingGrassStrength; | |
dup.wavingGrassTint = original.wavingGrassTint; | |
dup.SetAlphamaps(0, 0, original.GetAlphamaps(0, 0, original.alphamapWidth, original.alphamapHeight)); | |
dup.SetDetailResolution(original.detailResolution, original.detailResolutionPerPatch); | |
for (int n = 0; n < original.detailPrototypes.Length; n++) { | |
dup.SetDetailLayer(0, 0, n, original.GetDetailLayer(0, 0, original.detailWidth, original.detailHeight, n)); | |
} | |
dup.SetHeights(0, 0, original.GetHeights(0, 0, original.heightmapResolution, original.heightmapResolution)); | |
dup.SetHoles(0, 0, original.GetHoles(0, 0, original.holesResolution, original.holesResolution)); | |
dup.treePrototypes = CloneTreePrototypes(original.treePrototypes); | |
dup.treeInstances = CloneTreeInstances(original.treeInstances); | |
return dup; | |
} | |
/// <summary> | |
/// Deep-copies an array of detail prototype instances | |
/// </summary> | |
/// <param name="original">Prototypes to clone</param> | |
/// <returns>Cloned array</returns> | |
static DetailPrototype[] CloneDetailPrototypes(DetailPrototype[] original) { | |
DetailPrototype[] protoDuplicate = new DetailPrototype[original.Length]; | |
for (int n = 0; n < original.Length; n++) { | |
protoDuplicate[n] = new DetailPrototype { | |
dryColor = original[n].dryColor, | |
healthyColor = original[n].healthyColor, | |
holeEdgePadding = original[n].holeEdgePadding, | |
maxHeight = original[n].maxHeight, | |
maxWidth = original[n].maxWidth, | |
minHeight = original[n].minHeight, | |
minWidth = original[n].minWidth, | |
noiseSeed = original[n].noiseSeed, | |
noiseSpread = original[n].noiseSpread, | |
prototype = original[n].prototype, | |
prototypeTexture = original[n].prototypeTexture, | |
renderMode = original[n].renderMode, | |
useInstancing = original[n].useInstancing, | |
usePrototypeMesh = original[n].usePrototypeMesh | |
}; | |
} | |
return protoDuplicate; | |
} | |
/// <summary> | |
/// Deep-copies an array of tree prototype instances | |
/// </summary> | |
/// <param name="original">Prototypes to clone</param> | |
/// <returns>Cloned array</returns> | |
static TreePrototype[] CloneTreePrototypes(TreePrototype[] original) { | |
TreePrototype[] protoDuplicate = new TreePrototype[original.Length]; | |
for (int n = 0; n < original.Length; n++) { | |
protoDuplicate[n] = new TreePrototype { | |
bendFactor = original[n].bendFactor, | |
prefab = original[n].prefab, | |
}; | |
} | |
return protoDuplicate; | |
} | |
/// <summary> | |
/// Deep-copies an array of tree instances | |
/// </summary> | |
/// <param name="original">Trees to clone</param> | |
/// <returns>Cloned array</returns> | |
static TreeInstance[] CloneTreeInstances(TreeInstance[] original) { | |
TreeInstance[] treeInst = new TreeInstance[original.Length]; | |
System.Array.Copy(original, treeInst, original.Length); | |
return treeInst; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment