Unity VR Optimization : Object Pooling
Vložit
- čas přidán 31. 01. 2023
- In order to get the most out of our performance, we need to know how to use Object Pooling, which I cover in this video!
One problem we can run into when developing our games can occur when we Instantiate and Destroy hundreds of Objects in a short period of time. A good example of this would be having a gun that generates hundreds of bullets when we shoot a gun. Instead of losing tons of CPU cycles to this wasteful process, we can use Object Pooling!
🍤Check Out My Patreon🍤
/ fistfullofshrimp
Written Tutorial
fistfullofshrimp.com/unity-vr...
Machine Gun Asset
assetstore.unity.com/packages...
Unity Object Pooling Documentation
docs.unity3d.com/ScriptRefere...
Music Credits
------------------------------
Floating by Smith The Mister Hey Yeah, by Smith The Mister
Smith The Mister Smith The Mister
Free Download / Stream: [No Copyright Music] Floating by Smith The Mister · Hip Hop & Rap + Calm
Music promoted by Audio Library Floating - Smith The Mister (No Copyright Music)
------------------------------
Fantastic Video!!!
Adding it to my useful Unity Videos Playlist
Glad you liked it! I don't know why, but this video was a struggle to get out, so hearing it's helpful means a lot!!!
Looking forward to see more on UnityVR optimization, design patterns and S.O.L.I.D principles. Your videos are amazing!
That's very kind of you to say. Diving deeper into design patterns would be a blast. If you haven't checked it out yet, you might want to pick up Game Design Patterns by Robert Nystrom. Thanks again for watching! Link to the book listed below if you're interested.
amzn.to/43q7el2
3:26 Unity just added (2021.3.17) a bunch of combined GetPositionAndRotation and SetPositionAndRotation functions that are supposedly more efficient than just setting each individually, so that might even improve performance a bit more!
I didn't know about this! I'll definitely check it out!! Thanks you for mentioning it.
Man I wish I could give you 1000 views each time I watched your videos. So so insightful and useful videos for people like me who are just starting with VR dev in Unity.
I just had few questions, dont know if you will see the comment or not. But here it goes anyway.
I am trying to use my Quest 2 controller buttons in a way how we used to do button clicks in keyboard, like Input.GetKeyDown or Input.GetKeyUp. What would be the exact process to make that happen, since in the current state I can just get button click, thats it.
Anyway, thanks a lot again for your VR Optimisation series, it has helped me a lot
Glad you've been enjoying the optimization series! I've always wanted to learn more about optimizing, so what better way than making a whole bunch of CZcams videos about it!? lol.
As for your controller question, I found a forum post that might cover what you're looking for found here.
forum.unity.com/threads/xr-input-quest-how-to-get-button-inputs-on-oculus-quest.871993/
You might also get some useful info and ideas from my Input Data video
czcams.com/video/Kh_94glqO-0/video.html
Sorry I can't get into greater detail due to time constraints, but hopefully this pushes you in a helpful direciton!
Cheers! 🍤
I did my own in December before I realised Unity already gives you this functionality. Here's what I came up with: -
public class ProjectileManager : SingletonBase
{
private static List projectiles;
private void Awake()
{
projectiles = new List();
}
public GameObject CreateProjectile(GameObject prefab, Transform spawnPoint)
{
GameObject newProjectile;
if (projectiles.Count == 0)
newProjectile = Instantiate(prefab, spawnPoint.position, spawnPoint.rotation);
else
{
var lastIndex = (projectiles.Count - 1);
newProjectile = projectiles[lastIndex];
projectiles.RemoveAt(lastIndex);
newProjectile.transform.SetPositionAndRotation(spawnPoint.position, spawnPoint.rotation);
newProjectile.gameObject.SetActive(true);
}
newProjectile.GetComponent().Init(3, 15);
return newProjectile;
}
public void ReclaimProjectile(GameObject projectile)
{
projectile.SetActive(false);
projectiles.Add(projectile);
}
}
Oh well, you live and learn :D
I honestly didn't know Unity cooked up their own solutions until I started writing on this subject for this video. I always thought you had to cook up your own! At least you have a uber deep understanding of the Object Pooling design pattern! Cheers Arashi!!! 🍤
@@FistFullofShrimp Well, I've done it now so I ain't changing it Thanks for the illuminating (if somewhat depressing - given the circumstances) video on the matter!
I have few questions?
1.If I add object pooling and those object is all in some area in the Sean wean is not in use ?
For example let say that I weapon that spown object with object pool this object need to by always in this Sean even wean the weapon don't shooting ?
can you show us how to make a gun script for Ar's or two handed weapons?
What would i do if the velocity direction and magnitude was dependent on the actual object that is spawning it?
For anyone wondering, you can do bullet.Function(position, direction);
Put this in the get function
And in the bullet script, you can do
public void Function(Vector3 position, Vector3 direction){
"Insert code here"
}
Could you make a tut on Iap on oculus app lab
Funny you mention this. Was going to do a few videos on publishing to Itch.io and Sidequest. I think I can add on one for Oculus App Lab too!
@@FistFullofShrimp Thx!
me: tying to escape from war in Ukraine somewhere in vr and youtube
youtube: here is a machine gun and billions of bullets 😢
My apologies. In the future, I will try to make a shrimp gun that shoots shrimp.
please get a new mic
Subscribe to my Patreon and make these dreams come true!! Seriously though, what about the audio was off? I'm always looking to improve thing, so let me know!