Semi-proper warehouse scrolling using bezier curves
This commit is contained in:
+26
-61
@@ -5,83 +5,48 @@ using UnityEditor;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class WarehouseRow : MonoBehaviour {
|
||||
public GameObject[] Prefabs;
|
||||
public bool ShowInEditor = false;
|
||||
public string Category;
|
||||
|
||||
BezierCurve curve;
|
||||
|
||||
List<GameObject> furnitures;
|
||||
Transform furniture;
|
||||
|
||||
float offset = 0;
|
||||
|
||||
// Use this for initialization
|
||||
void Awake() {
|
||||
curve = transform.Find("Curve").GetComponent<BezierCurve>();
|
||||
furniture = transform.Find("Furniture");
|
||||
}
|
||||
|
||||
void Start() {
|
||||
curve = GetComponent<BezierCurve>();
|
||||
furnitures = new List<GameObject>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playmodeStateChanged += StateChange;
|
||||
#endif
|
||||
spawnPrefabs();
|
||||
// Set up text
|
||||
transform.Find("Text/Number").GetComponent<TextMesh>().text = (transform.GetSiblingIndex() + 1).ToString();
|
||||
transform.Find("Text/Category").GetComponent<TextMesh>().text = Category;
|
||||
}
|
||||
|
||||
public void OnEnable() {
|
||||
spawnPrefabs();
|
||||
}
|
||||
//void spawnPrefabs() {
|
||||
// if (furnitures.Count == 0) {
|
||||
// for (int i = 0; i < Prefabs.Length; i++) {
|
||||
// var prefab = Prefabs[i];
|
||||
|
||||
void OnDisable() {
|
||||
foreach (var item in furnitures) {
|
||||
GameObject.DestroyImmediate(item);
|
||||
}
|
||||
furnitures.Clear();
|
||||
}
|
||||
// GameObject furniture = Instantiate(prefab);
|
||||
// furniture.transform.SetParent(transform);
|
||||
// furniture.layer = 8;
|
||||
// furnitures.Add(furniture);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
void OnDestroy() {
|
||||
foreach (var item in furnitures) {
|
||||
GameObject.DestroyImmediate(item);
|
||||
}
|
||||
furnitures.Clear();
|
||||
}
|
||||
|
||||
void StateChange() {
|
||||
if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) {
|
||||
OnDisable();
|
||||
}
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
foreach (var item in furnitures) {
|
||||
GameObject.DestroyImmediate(item);
|
||||
}
|
||||
furnitures.Clear();
|
||||
spawnPrefabs();
|
||||
}
|
||||
|
||||
void spawnPrefabs() {
|
||||
if (furnitures.Count == 0) {
|
||||
for (int i = 0; i < Prefabs.Length; i++) {
|
||||
var prefab = Prefabs[i];
|
||||
|
||||
GameObject furniture = Instantiate(prefab);
|
||||
furniture.transform.SetParent(transform);
|
||||
furnitures.Add(furniture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {
|
||||
if (Application.isPlaying) {
|
||||
offset += Time.deltaTime;
|
||||
//offset += Time.deltaTime;
|
||||
}
|
||||
|
||||
for (int i = 0; i < furnitures.Count; i++) {
|
||||
var furniture = furnitures[i];
|
||||
|
||||
var distance = (float)i / (furnitures.Count - 1) * curve.length + offset;
|
||||
foreach (Transform t in furniture) {
|
||||
var i = t.GetSiblingIndex();
|
||||
var distance = (float)i / (furniture.childCount) * curve.length + offset;
|
||||
distance = Mathf.Repeat(distance, curve.length);
|
||||
var pos = curve.GetPointAtDistance(distance);
|
||||
furniture.transform.position = pos;
|
||||
t.position = pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user