Explode onclick CMS fix

This commit is contained in:
Tim
2025-07-17 21:08:08 +02:00
parent 9fea95c8c1
commit 071c470d52
4 changed files with 128 additions and 39 deletions

View File

@@ -7852,6 +7852,7 @@ MonoBehaviour:
baseModel: {fileID: 1272917964} baseModel: {fileID: 1272917964}
text: {fileID: 1785144091} text: {fileID: 1785144091}
cms: {fileID: 1509914124} cms: {fileID: 1509914124}
explode: 0
--- !u!114 &465093188 --- !u!114 &465093188
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -12851,6 +12852,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
ModelManager: {fileID: 1519836175} ModelManager: {fileID: 1519836175}
Exploder: {fileID: 465093187}
meshFilter: {fileID: 698127113} meshFilter: {fileID: 698127113}
meshRenderer: {fileID: 698127112} meshRenderer: {fileID: 698127112}
meshCollider: {fileID: 698127117} meshCollider: {fileID: 698127117}
@@ -21753,6 +21755,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
ModelManager: {fileID: 1519836175} ModelManager: {fileID: 1519836175}
Exploder: {fileID: 465093187}
meshFilter: {fileID: 1272917971} meshFilter: {fileID: 1272917971}
meshRenderer: {fileID: 1272917970} meshRenderer: {fileID: 1272917970}
meshCollider: {fileID: 1272917966} meshCollider: {fileID: 1272917966}

View File

@@ -9,6 +9,7 @@ using UnityEngine;
public class ModelBehaviour : MonoBehaviour, IResettable public class ModelBehaviour : MonoBehaviour, IResettable
{ {
public ModelManager ModelManager; public ModelManager ModelManager;
public Exploder Exploder;
Model _model; Model _model;
internal Model Model internal Model Model
@@ -21,6 +22,7 @@ public class ModelBehaviour : MonoBehaviour, IResettable
{ {
UpdateModel(value); UpdateModel(value);
_model = value; _model = value;
LateUpdateModel();
} }
} }
readonly List<GameObject> _children = new List<GameObject>(); readonly List<GameObject> _children = new List<GameObject>();
@@ -82,7 +84,7 @@ public class ModelBehaviour : MonoBehaviour, IResettable
{ {
return; return;
} }
interactable.WhenSelect.AddListener(OnSelect); interactable.WhenSelect.AddListener(OnSelect); //TODO: the root of all "frequently called", which is wrong
lateInited = true; lateInited = true;
} }
} }
@@ -138,18 +140,25 @@ public class ModelBehaviour : MonoBehaviour, IResettable
} }
} }
public void UpdateChild(int childNum, string id) void LateUpdateModel()
{ {
if (childNum >= _children.Count) Debug.Log($"LateUpdateModel");
{ Exploder.HandleModelChange();
Debug.LogWarning($"Index {childNum} out of bounds {_children.Count}");
} }
Model.Ports[childNum].Unapply(_children[childNum].transform);
public void UpdateChild(int portNum, string id) //onclick change model
{
if (portNum >= _children.Count)
{
Debug.LogWarning($"Index {portNum} out of bounds {_children.Count}");
}
Model.Ports[portNum].Unapply(_children[portNum].transform);
//set new //set new
var cmb = _children[childNum].GetComponent<ChildModelBehaviour>(); var cmb = _children[portNum].GetComponent<ChildModelBehaviour>();
cmb.ChildModel = ModelManager.GetById(id); cmb.ChildModel = ModelManager.GetById(id);
cmb.Parent = this; cmb.Parent = this;
Model.Ports[childNum].Apply(_children[childNum].transform); Model.Ports[portNum].Apply(_children[portNum].transform);
Exploder.HandleModelChange();
} }
/// <summary> /// <summary>

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@@ -13,8 +14,8 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler
List<ExploderModel> models = new List<ExploderModel>(); List<ExploderModel> models = new List<ExploderModel>();
static readonly int finalTick = 60 * 2; // 60fps * 2sek static readonly int finalTick = 60 * 2; // 60fps * 2sek, quest3 isn't 60fps but doesn't matter
bool explode = false; public bool explode = false;
int tick = finalTick + 1; int tick = finalTick + 1;
public void OnPointerDown(PointerEventData eventData) public void OnPointerDown(PointerEventData eventData)
@@ -27,30 +28,39 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler
} }
void Update() void Update()
{
try
{ {
if (tick <= finalTick) if (tick <= finalTick)
{ {
if(explode) if (explode)
for (int i = 0; i < models.Count; i++) for (int i = 0; i < models.Count; i++)
{ {
models[i].tf.localPosition = models[i].tf.localPosition =
MathUtil.EaseBoth(models[i].initPos, models[i].goalPos, MathUtil.EaseBoth(models[i].initPos,
models[i].goalPos,
(tick + 0f) / finalTick); //0f for floating point div (tick + 0f) / finalTick); //0f for floating point div
} }
else else
for (int i = 0; i < models.Count; i++) for (int i = 0; i < models.Count; i++)
{ {
models[i].tf.localPosition = models[i].tf.localPosition =
MathUtil.EaseBoth(models[i].goalPos, models[i].initPos, MathUtil.EaseBoth(models[i].goalPos,
models[i].initPos,
(tick + 0f) / finalTick); //0f for floating point div (tick + 0f) / finalTick); //0f for floating point div
} }
tick++; tick++;
} }
} }
catch (Exception e)
void Explode()
{ {
if (!explode) // before switching to true Debug.LogError($"Fixing ModelList Explode after {e}"); //happens when user changes model while animation
CleanModelList();
}
}
void PopulateModelList()
{ {
models.Clear(); models.Clear();
foreach (Transform child in baseModel.GetComponentsInChildren<Transform>()) foreach (Transform child in baseModel.GetComponentsInChildren<Transform>())
@@ -61,15 +71,73 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler
continue; //only for basemodel continue; //only for basemodel
} }
Port port = cmb.GetParentPort(); Port port = cmb.GetParentPort();
models.Add(new ExploderModel(port, cmb.ChildModel, child)); models.Add(new ExploderModel(port, cmb.ChildModel, cmb));
} }
} }
void UpdateModelList()
{
List<ExploderModel> modelss = new List<ExploderModel>();
foreach (var exploModel in models)
{
if (exploModel.cmb == null)
{
continue; //skip dead ends
}
if (exploModel.model != exploModel.cmb.ChildModel) //update
{
Debug.LogWarning($"Updating Model List from {exploModel.model.NameId} to {exploModel.cmb.ChildModel.NameId}");
// add children + itself
foreach (Transform child in exploModel.cmb.GetComponentsInChildren<Transform>())
{
var cmb = child.GetComponent<ChildModelBehaviour>();
if (cmb == null)
{
continue;
}
Port port = cmb.GetParentPort();
modelss.Add(new ExploderModel(port, cmb.ChildModel, cmb));
}
continue;
}
modelss.Add(exploModel);
}
models = modelss;
}
void CleanModelList()
{
models.RemoveAll(model => model.cmb == null || model.tf == null);
}
void ForceSetPos()
{
foreach (var exploModel in models)
{
exploModel.tf.localPosition = explode ? exploModel.goalPos : exploModel.initPos;
}
}
void Explode()
{
if (!explode) // before switching to true
{
PopulateModelList();
}
else
{
CleanModelList();
}
text.text = explode ? "Explode" : "Unexplode"; text.text = explode ? "Explode" : "Unexplode";
//cms.SetActive(explode);
explode = !explode; explode = !explode;
tick = 0; tick = 0;
} }
public void HandleModelChange()
{
UpdateModelList();
ForceSetPos();
}
public void ResetThis() public void ResetThis()
{ {
if (explode) if (explode)

View File

@@ -1,19 +1,28 @@
using UnityEngine; using Oculus.Interaction;
using UnityEngine;
public class ExploderModel public class ExploderModel
{ {
public Port port; public Port port;
public ChildModel model; public ChildModel model;
public Transform tf; //pointer to cmb pos public ChildModelBehaviour cmb;
public Transform tf;
public Vector3 initPos; public Vector3 initPos;
public Vector3 goalPos; public Vector3 goalPos;
public ExploderModel(Port port, ChildModel model, Transform tf) public ExploderModel(Port port, ChildModel model, ChildModelBehaviour cmb)
{ {
this.port = port; this.port = port;
this.model = model; this.model = model;
this.tf = tf; this.cmb = cmb;
CalculatePos();
}
public void CalculatePos()
{
tf = cmb.transform;
initPos = tf.localPosition; initPos = tf.localPosition;
goalPos = tf.localPosition + port.ExplodeDirection; goalPos = tf.localPosition + port.ExplodeDirection;
} }