1
0
forked from cgvr/DeltaVR

manage spawned object node hierarchy better

This commit is contained in:
2025-12-16 13:30:08 +02:00
parent 5796887f79
commit 90781191b7
4 changed files with 45 additions and 25 deletions

View File

@@ -1,10 +1,11 @@
using _PROJECT.Scripts.Bow;
using FishNet.Component.Transforming;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using _PROJECT.Scripts.Bow;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using TMPro; using TMPro;
using Unity.XR.CoreUtils; using Unity.XR.CoreUtils;
using UnityEngine; using UnityEngine;
@@ -21,6 +22,8 @@ public class ArcheryRange : NetworkBehaviour
public GameObject targetPrefab; public GameObject targetPrefab;
public StartTarget startTarget; public StartTarget startTarget;
public GameObject archeryTargetPointsText;
public Vector3 minRandomOffset = new Vector3(0f, -2f, -5f); public Vector3 minRandomOffset = new Vector3(0f, -2f, -5f);
public Vector3 maxRandomOffset = new Vector3(0f, 2f, 5f); public Vector3 maxRandomOffset = new Vector3(0f, 2f, 5f);
public float roundLength = 60f; public float roundLength = 60f;
@@ -130,14 +133,19 @@ public class ArcheryRange : NetworkBehaviour
async private Task<ArcheryTarget> SpawnTarget(Vector3 randomPos) async private Task<ArcheryTarget> SpawnTarget(Vector3 randomPos)
{ {
var targetObject = Instantiate(targetPrefab, randomPos, Quaternion.identity, null); GameObject targetObject;
string targetModelPath = modelGenerationBox.LastModelPath; if (modelGenerationBox.LastModelPath == null)
{
// Replace target prefab's child with the generated model // spawn default UFO
GameObject targetReplacement = await PipelineManager.Instance.SpawnModel(targetModelPath, false); targetObject = Instantiate(targetPrefab, randomPos, Quaternion.identity, null);
Destroy(targetObject.transform.GetChild(0).gameObject); } else
targetReplacement.transform.parent = targetObject.transform; {
targetReplacement.transform.position = targetObject.transform.position; // 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<ArcheryTarget>(); ArcheryTarget target = targetObject.GetComponent<ArcheryTarget>();
target.endPosition = targetEndPosition.position; target.endPosition = targetEndPosition.position;
@@ -146,6 +154,19 @@ public class ArcheryRange : NetworkBehaviour
return target; return target;
} }
private void InitializeArcherytargetObject(GameObject targetObject)
{
ArcheryTarget archeryTarget = targetObject.AddComponent<ArcheryTarget>();
archeryTarget.pointsText = archeryTargetPointsText;
Rigidbody rigidbody = targetObject.AddComponent<Rigidbody>();
rigidbody.useGravity = false;
rigidbody.isKinematic = true;
targetObject.AddComponent<NetworkObject>();
targetObject.AddComponent<NetworkTransform>();
}
public void ResetRange() public void ResetRange()
{ {

View File

@@ -224,7 +224,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96 m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96 m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1 m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0 m_PresetInfoIsWorld: 1
--- !u!114 &1237163760934993280 --- !u!114 &1237163760934993280
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -286,7 +286,7 @@ MonoBehaviour:
_initializeOrder: 0 _initializeOrder: 0
_defaultDespawnType: 1 _defaultDespawnType: 1
NetworkObserver: {fileID: 0} NetworkObserver: {fileID: 0}
<PrefabId>k__BackingField: 5 <PrefabId>k__BackingField: 8
<SpawnableCollectionId>k__BackingField: 0 <SpawnableCollectionId>k__BackingField: 0
_scenePathHash: 0 _scenePathHash: 0
<SceneId>k__BackingField: 0 <SceneId>k__BackingField: 0

View File

@@ -48,7 +48,8 @@ public class ModelGenerationBox : MonoBehaviour
string modelPath = await PipelineManager.Instance.GenerateModelAsync(inputPrompt); string modelPath = await PipelineManager.Instance.GenerateModelAsync(inputPrompt);
lastModelPath = modelPath; lastModelPath = modelPath;
GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath, true); GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath);
spawnedObject.AddComponent<Rigidbody>();
spawnedObject.transform.parent = modelSpawnPoint; spawnedObject.transform.parent = modelSpawnPoint;
spawnedObject.transform.position = modelSpawnPoint.position; spawnedObject.transform.position = modelSpawnPoint.position;

View File

@@ -1,5 +1,6 @@
using GLTFast; using GLTFast;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
@@ -75,30 +76,27 @@ public class PipelineManager : MonoBehaviour
}); });
} }
public async Task<GameObject> SpawnModel(string modelPath, bool addRigidBody) public async Task<GameObject> SpawnModel(string modelPath)
{ {
var gltf = new GltfImport(); var gltf = new GltfImport();
bool loadSuccess = await gltf.Load(modelPath); bool loadSuccess = await gltf.Load(modelPath);
if (loadSuccess) 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) if (spawnSuccess)
{ {
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform; Transform spawnedObjectWorldTransform = spawningParent.transform.GetChild(0).transform;
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject; GameObject spawnedObjectBody = spawnedObjectWorldTransform.GetChild(0).transform.gameObject;
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>(); MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
collider.convex = true; collider.convex = true;
MeshRenderer renderer = spawnedObjectBody.GetComponent<MeshRenderer>(); MeshRenderer renderer = spawnedObjectBody.GetComponent<MeshRenderer>();
renderer.material.SetFloat("metallicFactor", 0); renderer.material.SetFloat("metallicFactor", 0);
if (addRigidBody) spawnedObjectBody.name = objectName;
{ return spawnedObjectBody;
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
}
return spawnedObject;
} }
} }