maj la-2029
This commit is contained in:
parent
4aa4ab2914
commit
16fa9782e0
@ -31,7 +31,7 @@ C'est une version simple avec des assets téléchargées sur Sketchfab, le tout
|
||||
|
||||
La plateforme Quest étant limité en ressources ([Spécifications Oculus Quest](https://vr-compare.com/headset/oculusquest)), il fallait trouver un moyen d'optimiser le chargement des GameObjects en cours de niveau, j'ai donc implémenté le principe "d'object pooler".
|
||||
|
||||
Le principe s'articule autour de deux scripts fonctionnant de concert.
|
||||
Le principe s'articule autour de deux scripts fonctionnant de concert:
|
||||
|
||||
- Le premier script, l'ObjectPooler charge en début de scene un nombre défini d'instances de Gameobjects afin que ceux-ci soient disponibles et ainsi éviter des ralentissements en plein niveau.
|
||||
|
||||
@ -39,20 +39,134 @@ Le principe s'articule autour de deux scripts fonctionnant de concert.
|
||||
|
||||
<!--
|
||||

|
||||
|
||||
##### objectpooler.cs
|
||||
|
||||
```cs
|
||||
|
||||
```
|
||||
|
||||
##### objectpooler.cs
|
||||
|
||||
```cs
|
||||
|
||||
```
|
||||
-->
|
||||
|
||||
##### ObjectPooler.cs
|
||||
|
||||
```cs
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class ObjectPoolItem
|
||||
{
|
||||
public int amountToPool;
|
||||
public GameObject objectToPool;
|
||||
public bool shouldExpand;
|
||||
}
|
||||
|
||||
public class ObjectPooler : MonoBehaviour
|
||||
{
|
||||
public static ObjectPooler SharedInstance;
|
||||
|
||||
public List<GameObject> pooledObjects;
|
||||
|
||||
public List<ObjectPoolItem> itemsToPool;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
SharedInstance = this;
|
||||
}
|
||||
|
||||
void Start ()
|
||||
{
|
||||
pooledObjects = new List<GameObject>();
|
||||
foreach (ObjectPoolItem item in itemsToPool)
|
||||
{
|
||||
for (int i = 0; i < item.amountToPool; i++)
|
||||
{
|
||||
GameObject obj = (GameObject)Instantiate(item.objectToPool);
|
||||
obj.SetActive(false);
|
||||
pooledObjects.Add(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject GetPooledObject(string tag)
|
||||
{
|
||||
for (int i = 0; i < pooledObjects.Count; i++) {
|
||||
if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].tag == tag) {
|
||||
return pooledObjects[i];
|
||||
}
|
||||
}
|
||||
foreach (ObjectPoolItem item in itemsToPool) {
|
||||
if (item.objectToPool.tag == tag) {
|
||||
if (item.shouldExpand) {
|
||||
GameObject obj = (GameObject)Instantiate(item.objectToPool);
|
||||
obj.SetActive(false);
|
||||
pooledObjects.Add(obj);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
##### ObjectLoader.cs
|
||||
|
||||
```cs
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public class ObjectLoader : MonoBehaviour
|
||||
{
|
||||
public string prefabName;
|
||||
|
||||
private Transform spawnPoint;
|
||||
|
||||
private GameObject clone;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
spawnPoint = this.transform;
|
||||
}
|
||||
|
||||
void OnTriggerEnter(Collider col)
|
||||
{
|
||||
if(col.transform.tag == "Player")
|
||||
{
|
||||
LoadObject();
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerExit(Collider col)
|
||||
{
|
||||
if(col.transform.tag == "Player")
|
||||
{
|
||||
UnloadObject();
|
||||
}
|
||||
}
|
||||
|
||||
void LoadObject()
|
||||
{
|
||||
// GameObject clone = Instantiate(prefab, spawnPoint.position, spawnPoint.rotation);
|
||||
// this.clone = clone;
|
||||
GameObject clone = ObjectPooler.SharedInstance.GetPooledObject(prefabName);
|
||||
if(clone != null)
|
||||
{
|
||||
clone.transform.position = spawnPoint.transform.position;
|
||||
clone.transform.rotation = spawnPoint.transform.rotation;
|
||||
clone.SetActive(true);
|
||||
this.clone = clone;
|
||||
}
|
||||
}
|
||||
|
||||
void UnloadObject()
|
||||
{
|
||||
if(clone != null)
|
||||
{
|
||||
clone.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### V 2.00
|
||||
|
||||
Suite aux critiques positive sur SideQuest, j'ai décidé de pousser un peu plus loin la complexité des mécanismes en scriptant un objectif:
|
||||
|
Loading…
x
Reference in New Issue
Block a user