Finished load and save mechanic, v1.3.0
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Linq;
|
||||
|
||||
public class NoiseGenerator
|
||||
{
|
||||
@@ -230,7 +232,7 @@ public class NoiseGenerator
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
|
||||
public string saveTile(GameObject tile)
|
||||
public void saveTile(GameObject tile, string path)
|
||||
{
|
||||
string result = "";
|
||||
Vector3[] vertices = tile.GetComponent<MeshFilter>().mesh.vertices;
|
||||
@@ -247,13 +249,38 @@ public class NoiseGenerator
|
||||
result = result + "\r\n},\"colors\": {\r\n";
|
||||
for (int i = 0; i < colors.Length; i++)
|
||||
{
|
||||
result = result + FileHandler.generateJSON("color" + i, "\"" + colors[i].r + "/" + colors[i].g + "/" + colors[i].b + "\"");
|
||||
result = result + FileHandler.generateJSON("color" + i, "\"" + colors[i].r + "/" + colors[i].g + "/" + colors[i].b + "/" + colors[i].a + "\"");
|
||||
if (i < colors.Length - 1)
|
||||
{
|
||||
result = result + ",\r\n";
|
||||
}
|
||||
}
|
||||
result = result + "\r\n}";
|
||||
return result;
|
||||
result = result + "\r\n}\r\n}";
|
||||
FileHandler.saveNoise(result, path);
|
||||
}
|
||||
|
||||
public void loadTile(GameObject tile, JToken jsonVertices, JToken jsonColors)
|
||||
{
|
||||
var jsonData = JObject.Parse(jsonColors.ToString()).Children();
|
||||
List<JToken> colorTokens = jsonData.Children().ToList();
|
||||
jsonData = JObject.Parse(jsonVertices.ToString()).Children();
|
||||
List<JToken> verticeTokens = jsonData.Children().ToList();
|
||||
Color32[] colors = new Color32[colorTokens.Count];
|
||||
Vector3[] vertices = new Vector3[verticeTokens.Count];
|
||||
JToken current;
|
||||
string[] parts;
|
||||
for(int i = 0; i < colorTokens.Count;i++)
|
||||
{
|
||||
current = colorTokens[i];
|
||||
parts = current.Value<string>().Split('/');
|
||||
colors[i] = new Color32(byte.Parse(parts[0]), byte.Parse(parts[1]), byte.Parse(parts[2]), byte.Parse(parts[3]));
|
||||
}
|
||||
for (int i = 0; i < verticeTokens.Count; i++)
|
||||
{
|
||||
current = verticeTokens[i];
|
||||
parts = current.Value<string>().Split('/');
|
||||
vertices[i] = new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
|
||||
}
|
||||
applyMesh(tile, vertices, tile.GetComponent<MeshFilter>().mesh, colors);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user