From 95781d5afdcb95008d01c9bef8e44fa78614509f Mon Sep 17 00:00:00 2001 From: harith-abeysinghe Date: Sun, 10 Dec 2023 21:05:40 +0530 Subject: [PATCH] Fixed Random Object Generation --- AR Demo/Assets/Scenes/GameScreen.unity | 3 +- .../Assets/Scripts/RandomObjectGeneration.cs | 71 ++++++------------- AR Demo/ProjectSettings/ProjectSettings.asset | 4 +- 3 files changed, 26 insertions(+), 52 deletions(-) diff --git a/AR Demo/Assets/Scenes/GameScreen.unity b/AR Demo/Assets/Scenes/GameScreen.unity index 4fb7ddf..3951d00 100644 --- a/AR Demo/Assets/Scenes/GameScreen.unity +++ b/AR Demo/Assets/Scenes/GameScreen.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -1615,6 +1615,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: e48110cbb4151e04e864b9042406bf52, type: 3} m_Name: m_EditorClassIdentifier: + arPlaneManager: {fileID: 1659859600} gameObjectToInstantiate: {fileID: 2912470072421854214, guid: 38e04c9aa6f1ad040a42c61783ed74d2, type: 3} --- !u!1 &1703699202 diff --git a/AR Demo/Assets/Scripts/RandomObjectGeneration.cs b/AR Demo/Assets/Scripts/RandomObjectGeneration.cs index 8f2fa90..8d4ea27 100644 --- a/AR Demo/Assets/Scripts/RandomObjectGeneration.cs +++ b/AR Demo/Assets/Scripts/RandomObjectGeneration.cs @@ -1,74 +1,47 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.UI; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; [RequireComponent(typeof(ARRaycastManager))] public class RandomObjectGeneration : MonoBehaviour { + [SerializeField] private ARPlaneManager arPlaneManager; + + + public GameObject gameObjectToInstantiate; private ARRaycastManager raycastManager; + private List spawnedObjects = new List(); private List hitList = new List(); + private GameObject placedObject; + private void Awake() { raycastManager = GetComponent(); + arPlaneManager= GetComponent(); + arPlaneManager.planesChanged += PlaneChanged; } - void Start() + private void PlaneChanged(ARPlanesChangedEventArgs args) { - StartCoroutine(GenerateObjects()); - } - - IEnumerator GenerateObjects() - { - int maxObjects = 3; - - while (true) + if (args.added != null && placedObject == null) { - if (spawnedObjects.Count < maxObjects) - { - bool objectSpawned = false; - - if (raycastManager.Raycast(new Vector2(Screen.width * 0.5f, Screen.height * 0.5f), hitList, TrackableType.PlaneWithinPolygon)) - { - var hitPose = hitList[0].pose; - - Vector3 randomOffset = new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f)); - Vector3 spawnPosition = hitPose.position + randomOffset; - - bool positionValid = true; - foreach (GameObject obj in spawnedObjects) - { - if (Vector3.Distance(obj.transform.position, spawnPosition) < 0.3f) - { - positionValid = false; - break; - } - } - - if (positionValid) - { - GameObject newObject = Instantiate(gameObjectToInstantiate, spawnPosition, hitPose.rotation); - spawnedObjects.Add(newObject); - objectSpawned = true; - } - } - - if (!objectSpawned) - { - // If no object was spawned in the current iteration, yield for a shorter duration before retrying - yield return new WaitForSeconds(0.1f); - } - } - else - { - // If max objects reached, yield for a longer duration before checking again - yield return new WaitForSeconds(0.5f); - } + ARPlane aRPlane = args.added[0]; + placedObject = Instantiate(gameObjectToInstantiate, aRPlane.transform.position, Quaternion.identity); + } } + void Start() + { + + } + + + } diff --git a/AR Demo/ProjectSettings/ProjectSettings.asset b/AR Demo/ProjectSettings/ProjectSettings.asset index 5588f31..5392bc8 100644 --- a/AR Demo/ProjectSettings/ProjectSettings.asset +++ b/AR Demo/ProjectSettings/ProjectSettings.asset @@ -138,12 +138,12 @@ PlayerSettings: loadStoreDebugModeEnabled: 0 bundleVersion: 1.0 preloadedAssets: - - {fileID: 4800000, guid: c9f956787b1d945e7b36e0516201fc76, type: 3} - - {fileID: 4800000, guid: 0945859e5a1034c2cb6dce53cb4fb899, type: 3} - {fileID: 11400000, guid: 1eb95d77acb39444e8cf44313496a874, type: 2} - {fileID: -6654145819923878069, guid: 58f40b12bbc864f3c96c6505a9a1e1e3, type: 2} - {fileID: 11400000, guid: fdea7d63f5e36d34880021adf1f629d5, type: 2} - {fileID: 4310551847955826307, guid: 6bb381a8ba31f114d9724357633b4625, type: 2} + - {fileID: 4800000, guid: c9f956787b1d945e7b36e0516201fc76, type: 3} + - {fileID: 4800000, guid: 0945859e5a1034c2cb6dce53cb4fb899, type: 3} metroInputSource: 0 wsaTransparentSwapchain: 0 m_HolographicPauseOnTrackingLoss: 1