forked from cgvr/DeltaVR
manage spawned object node hierarchy better
This commit is contained in:
@@ -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<ArcheryTarget> 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<ArcheryTarget>();
|
||||
target.endPosition = targetEndPosition.position;
|
||||
@@ -146,6 +154,19 @@ public class ArcheryRange : NetworkBehaviour
|
||||
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()
|
||||
{
|
||||
|
||||
@@ -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}
|
||||
<PrefabId>k__BackingField: 5
|
||||
<PrefabId>k__BackingField: 8
|
||||
<SpawnableCollectionId>k__BackingField: 0
|
||||
_scenePathHash: 0
|
||||
<SceneId>k__BackingField: 0
|
||||
|
||||
@@ -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<Rigidbody>();
|
||||
spawnedObject.transform.parent = modelSpawnPoint;
|
||||
spawnedObject.transform.position = modelSpawnPoint.position;
|
||||
|
||||
|
||||
@@ -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<GameObject> SpawnModel(string modelPath, bool addRigidBody)
|
||||
public async Task<GameObject> 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<MeshCollider>();
|
||||
collider.convex = true;
|
||||
MeshRenderer renderer = spawnedObjectBody.GetComponent<MeshRenderer>();
|
||||
renderer.material.SetFloat("metallicFactor", 0);
|
||||
|
||||
if (addRigidBody)
|
||||
{
|
||||
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
return spawnedObject;
|
||||
spawnedObjectBody.name = objectName;
|
||||
return spawnedObjectBody;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user