-
Notifications
You must be signed in to change notification settings - Fork 0
/
HitBoxEditor.cs
176 lines (166 loc) · 6.26 KB
/
HitBoxEditor.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json.Converters;
using System;
public class Rootobject
{
public Hitbox[] hitboxes { get; set; }
}
public class Hitbox
{
public string name { get; set; }
public int beginFrame { get; set; }
public int duration { get; set; }
public float damage { get; set; }
public float hitStun { get; set; }
public float[] launchVector { get; set; }
public float[] rotation { get; set; }
public float[] scale { get; set; }
public float[] offset { get; set; }
}
public class HitBoxEditor : EditorWindow
{
string nameField = "HBox";
private Vector3 launchVectorField;
private int beginFrameField;
private int durationField;
private float damageField;
private float hitStunField;
private Vector3 scaleField = new Vector3(1,1,1);
private Vector3 offsetField;
private Vector3 rotationField;
string pathField = @"C:\";
[SerializeField]
Transform charecterField;
int time = 0;
int maxTimeField = 1;
bool playMode = false;
public void Update()
{
if (playMode) {
time++;
if (time > maxTimeField)
{
time = 0;
}
UpdateHitboxesToTime();
}
}
// Add menu item named "My Window" to the Window menu
[MenuItem("Window/HitBoxEditor")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(HitBoxEditor));
}
void OnGUI()
{
GUILayout.Label("HBox", EditorStyles.boldLabel);
nameField = EditorGUILayout.TextField("Name Field", nameField);
launchVectorField = EditorGUILayout.Vector3Field("LaunchVector", launchVectorField);
beginFrameField = EditorGUILayout.IntField("Begin Frame", beginFrameField);
durationField = EditorGUILayout.IntField("Duration", durationField);
damageField = EditorGUILayout.FloatField("Damage", damageField);
hitStunField = EditorGUILayout.FloatField("HitStun", hitStunField);
scaleField = EditorGUILayout.Vector3Field("scaleVector", scaleField);
offsetField = EditorGUILayout.Vector3Field("offsetVector", offsetField);
rotationField = EditorGUILayout.Vector3Field("rotationVector", rotationField);
if (GUILayout.Button("Creat HBox"))
{
CreateHitbox(nameField, launchVectorField, beginFrameField, durationField, damageField, hitStunField, scaleField, offsetField, rotationField);
Debug.Log("hitbox created");
}
pathField = EditorGUILayout.TextField("Path Field", pathField);
if (GUILayout.Button("Export JSON"))
{
var hboxArray = Resources.FindObjectsOfTypeAll(typeof(HBox));
string json = "{\"hitboxes\":[";
foreach (var hbox in hboxArray)
{
json += hbox.ToString() + ", ";
}
if (hboxArray.Length > 0)
{
json = json.Substring(0, json.Length - 2);
}
json += "]}";
File.WriteAllText(pathField, json);
Debug.Log("hitbox exported");
}
if (GUILayout.Button("Import JSON"))
{
string json = File.ReadAllText(pathField);
Rootobject root = JsonConvert.DeserializeObject<Rootobject>(json);
foreach(var hbox in root.hitboxes)
{
CreateHitbox(hbox.name,
new Vector3( hbox.launchVector[0], hbox.launchVector[1], hbox.launchVector[2]),
hbox.beginFrame,
hbox.duration,
hbox.damage,
hbox.hitStun,
new Vector3(hbox.scale[0], hbox.scale[1], hbox.scale[2]),
new Vector3(hbox.offset[0], hbox.offset[1], hbox.offset[2]),
new Vector3(hbox.rotation[0], hbox.rotation[1], hbox.rotation[2]));
}
Debug.Log("hitbox imported");
}
maxTimeField = EditorGUILayout.IntField(maxTimeField);
var tempTIme = time;
time = EditorGUILayout.IntSlider(time,0,maxTimeField);
if (tempTIme != time)
{
UpdateHitboxesToTime();
}
playMode = EditorGUILayout.Toggle(playMode);
charecterField = (Transform) EditorGUILayout.ObjectField(charecterField, typeof(Transform), true);
}
void UpdateHitboxesToTime()
{
var hboxArray = Resources.FindObjectsOfTypeAll(typeof(HBox));
foreach (HBox hbox in hboxArray)
{
if (hbox.GetComponent<HBox>().beginFrame <= time && time - hbox.GetComponent<HBox>().beginFrame <= hbox.GetComponent<HBox>().duration)
{
Debug.Log("enabled");
hbox.gameObject.SetActive(true);
}
else
{
Debug.Log("disabled");
hbox.gameObject.SetActive(false);
}
}
if (charecterField != null) {
charecterField.GetComponent<animateInEditor>().SetAnimPercent((float)time/ (float)maxTimeField);
}
}
void CreateHitbox(String name, Vector3 launchVector, int beginFrame, int duration, float damage, float hitstun, Vector3 scale, Vector3 offset, Vector3 rotation)
{
GameObject instance = Instantiate(Resources.Load("HBox", typeof(GameObject))) as GameObject;
instance.name = name;
instance.GetComponent<HBox>().launchVector = launchVector;
instance.GetComponent<HBox>().beginFrame = beginFrame;
instance.GetComponent<HBox>().duration = duration;
instance.GetComponent<HBox>().damage = damage;
instance.GetComponent<HBox>().hitStun = hitstun;
instance.transform.localScale = scale;
instance.transform.position = offset;
instance.transform.eulerAngles = rotation;
}
/*int getMaxTime()
{
var hboxArray = FindObjectsOfType<HBox>();
int maxTime = 0;
foreach (var hbox in hboxArray)
{
maxTime = (hbox.beginFrame + hbox.duration > maxTime) ? (hbox.beginFrame + hbox.duration) : (maxTime);
}
return maxTime;
}*/
}