From 90781191b7ecf15919804cd613b990b427334f04 Mon Sep 17 00:00:00 2001 From: henrisel Date: Tue, 16 Dec 2025 13:30:08 +0200 Subject: [PATCH] manage spawned object node hierarchy better --- .../Components/Bow/Scripts/ArcheryRange.cs | 43 ++++++++++++++----- .../_PROJECT/Prefabs/XR/Bow/ScorePopup.prefab | 4 +- .../ModeGeneration/ModelGenerationBox.cs | 3 +- .../Scripts/ModeGeneration/PipelineManager.cs | 20 ++++----- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Assets/_PROJECT/Components/Bow/Scripts/ArcheryRange.cs b/Assets/_PROJECT/Components/Bow/Scripts/ArcheryRange.cs index f6e4af5e..3700e170 100644 --- a/Assets/_PROJECT/Components/Bow/Scripts/ArcheryRange.cs +++ b/Assets/_PROJECT/Components/Bow/Scripts/ArcheryRange.cs @@ -1,10 +1,11 @@ +using _PROJECT.Scripts.Bow; +using FishNet.Component.Transforming; +using FishNet.Object; +using FishNet.Object.Synchronizing; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using _PROJECT.Scripts.Bow; -using FishNet.Object; -using FishNet.Object.Synchronizing; using TMPro; using Unity.XR.CoreUtils; using UnityEngine; @@ -21,6 +22,8 @@ public class ArcheryRange : NetworkBehaviour public GameObject targetPrefab; public StartTarget startTarget; + public GameObject archeryTargetPointsText; + public Vector3 minRandomOffset = new Vector3(0f, -2f, -5f); public Vector3 maxRandomOffset = new Vector3(0f, 2f, 5f); public float roundLength = 60f; @@ -130,14 +133,19 @@ public class ArcheryRange : NetworkBehaviour async private Task SpawnTarget(Vector3 randomPos) { - var targetObject = Instantiate(targetPrefab, randomPos, Quaternion.identity, null); - string targetModelPath = modelGenerationBox.LastModelPath; - - // Replace target prefab's child with the generated model - GameObject targetReplacement = await PipelineManager.Instance.SpawnModel(targetModelPath, false); - Destroy(targetObject.transform.GetChild(0).gameObject); - targetReplacement.transform.parent = targetObject.transform; - targetReplacement.transform.position = targetObject.transform.position; + GameObject targetObject; + if (modelGenerationBox.LastModelPath == null) + { + // spawn default UFO + targetObject = Instantiate(targetPrefab, randomPos, Quaternion.identity, null); + } else + { + // spawn generated model + targetObject = await PipelineManager.Instance.SpawnModel(modelGenerationBox.LastModelPath); + targetObject.transform.position = randomPos; + targetObject.transform.rotation = Quaternion.identity; + InitializeArcherytargetObject(targetObject); + } ArcheryTarget target = targetObject.GetComponent(); target.endPosition = targetEndPosition.position; @@ -146,6 +154,19 @@ public class ArcheryRange : NetworkBehaviour return target; } + private void InitializeArcherytargetObject(GameObject targetObject) + { + ArcheryTarget archeryTarget = targetObject.AddComponent(); + archeryTarget.pointsText = archeryTargetPointsText; + + Rigidbody rigidbody = targetObject.AddComponent(); + rigidbody.useGravity = false; + rigidbody.isKinematic = true; + + targetObject.AddComponent(); + targetObject.AddComponent(); + } + public void ResetRange() { diff --git a/Assets/_PROJECT/Prefabs/XR/Bow/ScorePopup.prefab b/Assets/_PROJECT/Prefabs/XR/Bow/ScorePopup.prefab index c7a3abf8..8d940ec5 100644 --- a/Assets/_PROJECT/Prefabs/XR/Bow/ScorePopup.prefab +++ b/Assets/_PROJECT/Prefabs/XR/Bow/ScorePopup.prefab @@ -224,7 +224,7 @@ MonoBehaviour: m_FallbackScreenDPI: 96 m_DefaultSpriteDPI: 96 m_DynamicPixelsPerUnit: 1 - m_PresetInfoIsWorld: 0 + m_PresetInfoIsWorld: 1 --- !u!114 &1237163760934993280 MonoBehaviour: m_ObjectHideFlags: 0 @@ -286,7 +286,7 @@ MonoBehaviour: _initializeOrder: 0 _defaultDespawnType: 1 NetworkObserver: {fileID: 0} - k__BackingField: 5 + k__BackingField: 8 k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 diff --git a/Assets/_PROJECT/Scripts/ModeGeneration/ModelGenerationBox.cs b/Assets/_PROJECT/Scripts/ModeGeneration/ModelGenerationBox.cs index 20358f79..a42f7d87 100644 --- a/Assets/_PROJECT/Scripts/ModeGeneration/ModelGenerationBox.cs +++ b/Assets/_PROJECT/Scripts/ModeGeneration/ModelGenerationBox.cs @@ -48,7 +48,8 @@ public class ModelGenerationBox : MonoBehaviour string modelPath = await PipelineManager.Instance.GenerateModelAsync(inputPrompt); lastModelPath = modelPath; - GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath, true); + GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath); + spawnedObject.AddComponent(); spawnedObject.transform.parent = modelSpawnPoint; spawnedObject.transform.position = modelSpawnPoint.position; diff --git a/Assets/_PROJECT/Scripts/ModeGeneration/PipelineManager.cs b/Assets/_PROJECT/Scripts/ModeGeneration/PipelineManager.cs index 30fa0fd3..9980ac2e 100644 --- a/Assets/_PROJECT/Scripts/ModeGeneration/PipelineManager.cs +++ b/Assets/_PROJECT/Scripts/ModeGeneration/PipelineManager.cs @@ -1,5 +1,6 @@ using GLTFast; using System.Diagnostics; +using System.IO; using System.Threading.Tasks; using UnityEngine; @@ -75,30 +76,27 @@ public class PipelineManager : MonoBehaviour }); } - public async Task SpawnModel(string modelPath, bool addRigidBody) + public async Task SpawnModel(string modelPath) { var gltf = new GltfImport(); bool loadSuccess = await gltf.Load(modelPath); if (loadSuccess) { - GameObject spawnedObject = new GameObject("spawned model"); + string objectName = Path.GetFileName(modelPath); + GameObject spawningParent = new GameObject("Parent-" + objectName); - bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform); + bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawningParent.transform); if (spawnSuccess) { - Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform; - GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject; + Transform spawnedObjectWorldTransform = spawningParent.transform.GetChild(0).transform; + GameObject spawnedObjectBody = spawnedObjectWorldTransform.GetChild(0).transform.gameObject; MeshCollider collider = spawnedObjectBody.AddComponent(); collider.convex = true; MeshRenderer renderer = spawnedObjectBody.GetComponent(); renderer.material.SetFloat("metallicFactor", 0); - if (addRigidBody) - { - spawnedObjectMainTransform.gameObject.AddComponent(); - } - - return spawnedObject; + spawnedObjectBody.name = objectName; + return spawnedObjectBody; } }