Skip to content

Commit

Permalink
Added files for part 24; Loading and Saving
Browse files Browse the repository at this point in the history
  • Loading branch information
b3agz committed Apr 1, 2020
1 parent f829b29 commit df5ace5
Show file tree
Hide file tree
Showing 229 changed files with 32,041 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Data.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes/Desert.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 604863bef2f81714b90b56811bb2841f, type: 3}
m_Name: Desert
m_EditorClassIdentifier:
biomeName: Desert
offset: 6545
scale: 0.058
terrainHeight: 10
terrainScale: 0.05
surfaceBlock: 4
subSurfaceBlock: 4
majorFloraIndex: 1
majorFloraZoneScale: 1.06
majorFloraZoneThreshold: 0.75
majorFloraPlacementScale: 7.5
majorFloraPlacementThreshold: 0.8
placeMajorFlora: 1
majorFloraMaxTreeHeight: 3
majorFloraMinTreeHeight: 2
lodes:
- nodeName: Dirt
blockID: 5
minHeight: 1
maxHeight: 255
scale: 0.1
threshold: 0.5
noiseOffset: 0
- nodeName: Sand
blockID: 4
minHeight: 30
maxHeight: 60
scale: 0.2
threshold: 0.6
noiseOffset: 500
8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes/Desert.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes/Forest.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 604863bef2f81714b90b56811bb2841f, type: 3}
m_Name: Forest
m_EditorClassIdentifier:
biomeName: Forest
offset: 87544
scale: 0.17
terrainHeight: 80
terrainScale: 0.3
surfaceBlock: 5
subSurfaceBlock: 5
majorFloraIndex: 0
majorFloraZoneScale: 1.3
majorFloraZoneThreshold: 0.384
majorFloraPlacementScale: 5
majorFloraPlacementThreshold: 0.755
placeMajorFlora: 1
majorFloraMaxTreeHeight: 16
majorFloraMinTreeHeight: 5
lodes:
- nodeName: Dirt
blockID: 5
minHeight: 1
maxHeight: 255
scale: 0.1
threshold: 0.5
noiseOffset: 0
- nodeName: Sand
blockID: 4
minHeight: 30
maxHeight: 60
scale: 0.2
threshold: 0.6
noiseOffset: 500
8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes/Forest.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions 24-loading-and-saving/Assets/Data/Biomes/Grasslands.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 604863bef2f81714b90b56811bb2841f, type: 3}
m_Name: Grasslands
m_EditorClassIdentifier:
biomeName: Grasslands
offset: 1234
scale: 0.042
terrainHeight: 22
terrainScale: 0.15
surfaceBlock: 3
subSurfaceBlock: 5
majorFloraIndex: 0
majorFloraZoneScale: 1.3
majorFloraZoneThreshold: 0.6
majorFloraPlacementScale: 15
majorFloraPlacementThreshold: 0.8
placeMajorFlora: 1
majorFloraMaxTreeHeight: 12
majorFloraMinTreeHeight: 5
lodes:
- nodeName: Dirt
blockID: 5
minHeight: 1
maxHeight: 255
scale: 0.1
threshold: 0.5
noiseOffset: 0
- nodeName: Sand
blockID: 4
minHeight: 30
maxHeight: 60
scale: 0.2
threshold: 0.6
noiseOffset: 500

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions 24-loading-and-saving/Assets/Editor/AtlasPacker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class AtlasPacker : EditorWindow {

int blockSize = 16; // Block size in pixels.
int atlasSizeInBlocks = 16;
int atlasSize;

Object[] rawTextures = new Object[256];
List<Texture2D> sortedTextures = new List<Texture2D>();
Texture2D atlas;

[MenuItem ("Minecraft Clone/Atlas Packer")] // Create a menu item to show the window.

public static void ShowWindow() {

EditorWindow.GetWindow(typeof(AtlasPacker));

}

private void OnGUI() {

atlasSize = blockSize * atlasSizeInBlocks;

GUILayout.Label("Minecraft Cone Texture Atlas Packer", EditorStyles.boldLabel);

blockSize = EditorGUILayout.IntField("Block Size", blockSize);
atlasSizeInBlocks = EditorGUILayout.IntField("Atlas Size (in blocks)", atlasSizeInBlocks);

GUILayout.Label(atlas);

if (GUILayout.Button("Load Textures")) {

LoadTextures();
PackAtlas();

Debug.Log("Atlas Packer: Textures loaded.");

}

if (GUILayout.Button("Clear Textures")) {

atlas = new Texture2D(atlasSize, atlasSize);
Debug.Log("Atlas Packer: Textures cleared.");

}

if (GUILayout.Button("Save Atlas")) {

byte[] bytes = atlas.EncodeToPNG();
try {
File.WriteAllBytes(Application.dataPath + "/Textures/Packed_Atlas.png", bytes);
Debug.Log("Atlas Packer: Atlas file sucessfully saved. " + bytes.Length);
} catch {
Debug.Log("Atlas Packer: Couldn't save atlas to file.");
}

}

}

void LoadTextures () {

// Editor variables don't get wiped so we need to clear this list or it will keep growing.
sortedTextures.Clear();

rawTextures = Resources.LoadAll("AtlasPacker", typeof(Texture2D));

// Loop through each one and perform any checks we want to perform.
int index = 0;
foreach (Object tex in rawTextures) {

// Convert the current object into a texture and check that it's the correct size before adding.
Texture2D t = (Texture2D)tex;
if (t.width == blockSize && t.height == blockSize)
sortedTextures.Add(t);
else
Debug.Log("Asset Packer: " + tex.name + " incorrect size. Texture not loaded.");

index++;

}

Debug.Log("Atlas Packer: " + sortedTextures.Count + " successfully loaded.");

}

void PackAtlas () {

atlas = new Texture2D(atlasSize, atlasSize);
Color[] pixels = new Color[atlasSize * atlasSize];

for (int x = 0; x < atlasSize; x++) {
for (int y = 0; y < atlasSize; y++) {

// Get the current block that we're looking at.
int currentBlockX = x / blockSize;
int currentBlockY = y / blockSize;

int index = currentBlockY * atlasSizeInBlocks + currentBlockX;

// Get the pixel in the current block.
int currentPixelX = x - (currentBlockX * blockSize);
int currentPixelY = y - (currentBlockY * blockSize);

if (index < sortedTextures.Count)
pixels[(atlasSize - y - 1) * atlasSize + x] = sortedTextures[index].GetPixel(x, blockSize - y - 1);
else
pixels[(atlasSize - y - 1) * atlasSize + x] = new Color(0f, 0f, 0f, 0f);

}
}

atlas.SetPixels(pixels);
atlas.Apply();

}

}
11 changes: 11 additions & 0 deletions 24-loading-and-saving/Assets/Editor/AtlasPacker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 24-loading-and-saving/Assets/Fonts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit df5ace5

Please sign in to comment.