-
Notifications
You must be signed in to change notification settings - Fork 0
/
PositionSerialize.cs
90 lines (77 loc) · 2.79 KB
/
PositionSerialize.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
//This is a debug script, full version will capture image of screen instead.
//Needed coordinates to save some interesting faces to use as -
// - reference images in the actual game.
public class PositionSerialize : MonoBehaviour
{
[SerializeField]
GameObject armature;
string filePath;
string directory = "Exported Positions/";
int lastFileIndex = 1;
string modelName;
int boneCount;
void Start ()
{
modelName = armature.transform.parent.gameObject.name;
filePath = directory + modelName + lastFileIndex + ".txt";
boneCount = armature.transform.childCount;
////If file already exists, find last file count & set filePath to last file.
if (File.Exists(filePath))
{
string[] files = Directory.GetFiles(directory);
string lastFile = files[files.Length - 1].Split('.')[0];
lastFileIndex = int.Parse(lastFile.Substring(lastFile.Length - 1));
filePath = directory + modelName + lastFileIndex + ".txt";
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.V))
WritePositions();
else
{
if (Input.GetKeyDown(KeyCode.G))
ReadPositions();
}
}
void WritePositions()
{
//If file already exists, find last file count & add increased count at end of file name.
if (File.Exists(filePath))
{
string[] files = Directory.GetFiles(directory);
string lastFile = files[files.Length - 1].Split('.')[0];
lastFileIndex = int.Parse(lastFile.Substring(lastFile.Length - 1));
lastFileIndex++;
filePath = directory + modelName + lastFileIndex + ".txt";
}
StreamWriter writer = new StreamWriter(filePath, true);
for (int i = 0; i < boneCount; i++)
{
if (!armature.transform.GetChild(i).gameObject.name.Contains("Base"))
writer.WriteLine(armature.transform.GetChild(i).position);
}
writer.Close();
}
void ReadPositions()
{
StreamReader reader = new StreamReader(filePath);
//childCount-1 because base bone is excluded.
for(int i = 0; i < boneCount-1; i++)
{
Vector3 readPos = new Vector3();
string[] stringCoordinates = reader.ReadLine().Split('(', ',', ')');
readPos.x = float.Parse(stringCoordinates[1]);
readPos.y = float.Parse(stringCoordinates[2]);
readPos.z = float.Parse(stringCoordinates[3]);
armature.transform.GetChild(i).position = readPos;
//DEBUG - SHOW READ POSITIONS
Debug.Log(readPos);
}
reader.Close();
}
}