Files
Simulation/Assets/Scripts/SimulationModels/SimulationModel.cs

101 lines
2.6 KiB
C#
Raw Normal View History

2025-05-26 18:06:25 +09:00
using UnityEngine;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
2025-05-12 18:00:37 +09:00
public class SimulationModel : MonoBehaviour, IClickable
{
2025-05-12 18:00:37 +09:00
public string modelName;
public string modelType;
public string modelID;
2025-05-26 18:06:25 +09:00
public string nodeID;
private bool isQuitting = false;
2025-05-12 18:00:37 +09:00
void Awake()
{
FitCollider();
2025-05-26 18:06:25 +09:00
}
private void OnEnable()
{
DataManager.I.AddModel(this);
2025-05-26 18:06:25 +09:00
StartCoroutine(RunSimulationCoroutine());
}
private void OnDisable()
{
if (isQuitting) return;
DataManager.I.RemoveModel(this);
2025-05-26 18:06:25 +09:00
StopAllCoroutines();
}
void OnApplicationQuit()
{
isQuitting = true;
2025-05-12 18:00:37 +09:00
}
public void OnClick()
{
Debug.Log("Clicked : " + gameObject.name);
}
void FitCollider()
{
var renderers = GetComponentsInChildren<Renderer>();
if (renderers.Length == 0) return;
Bounds bounds = renderers[0].bounds;
foreach (var r in renderers)
bounds.Encapsulate(r.bounds);
BoxCollider collider = GetComponent<BoxCollider>();
if (!collider) collider = gameObject.AddComponent<BoxCollider>();
collider.center = transform.InverseTransformPoint(bounds.center);
collider.size = transform.InverseTransformVector(bounds.size);
}
2025-05-26 18:06:25 +09:00
protected virtual IEnumerator RunSimulationCoroutine()
{
yield return null;
}
public object GetJsonValue(JToken token, IEnumerable<string> path)
{
foreach (var key in path)
{
if (token == null) return null;
token = token[key];
}
return token;
}
2025-05-27 14:53:01 +09:00
public JArray GetJsonArray(JToken token, IEnumerable<string> path)
{
foreach (var key in path)
{
if (token == null) return null;
token = token[key];
}
return token as JArray;
}
public int GetJsonIntValue(JToken token, IEnumerable<string> path)
{
int value = 0;
foreach (var key in path)
{
if (token == null) return 0;
token = token[key];
}
if (token != null && int.TryParse(token.ToString(), out value))
{
return value;
}
return 0;
}
public float GetJsonFloatValue(JToken token, IEnumerable<string> path)
{
float value = 0;
foreach (var key in path)
{
if (token == null) return 0;
token = token[key];
}
if (token != null && float.TryParse(token.ToString(), out value))
{
return value;
}
return 0;
}
}