- Home /
How can i find the current active/enabled true camera and sort list of cameras make the active camera first ?
void Start()
{
cameras = new List<Camera>();
cameras.AddRange(GameObject.FindObjectsOfType<Camera>());
cameras.Sort()
}
In this case the Main Camera is the active one when running the game. So i want first to find the current active camera when running the game and then sort the List so the active camera will be first in the List.
Answer by FortisVenaliter · Aug 25, 2017 at 03:08 PM
Why do you need it in a list? How many cameras do you have?
Typically, you'll set the main camera to have the "Main Camera" tag (this is default in a new scene). If that's the case, you can find the main camera at any time by using the static property Camera.main.
I'm not sure I understand your need for the list though. How is it supposed to sort? From most important to least? In that case, you'll need to add some metadata to the cameras to indicate their importance.
Either way, the way to do that would be to write a custom comparison function that matches the Comparison delegate. Then you can pass that in as an argument to the list's Sort() function.
Answer by AbominationGames · Aug 25, 2017 at 03:02 PM
You'll have to iterate through your list to find out if it is active in your heirarcy. https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html
void SortCameras() {
for (int i = 0; i < cameras.Length; i ++) {
listItem item = cameras[i];
if (cameras[i].activeInHeirarchy) {
// Code to remove and add
}
}
As far as the moving it to the front, from what I've gathered from Stack is that using a linked list may be easier. It has a function, .AddFirst() that is supposed to be more efficient.
Here are a few links that may help you: C# Sorted List Return Items from a List Move Member to Front of List And here are a couple that cover the activeInHeirarchy: Check Active in Heirarcy Unity Documentation: Active in Heirarchy
This may not be the best or most efficient way of doing it, but it does seem like it should work to me. Hopefully this helps or gets you set in the correct direction.
No.....
List is not a linked list, it's an arraylist. See here. And there's no reason for a stack here.
AddFirst() is a Linq extension method, so it's rarely a performance boost.
Finally, removing and adding is one of the least efficient ways to sort, especially when the List class has a Sort() method! Granted, that uses shifts which aren't as efficient as sorting a proper linked list, but it's still fine in 99% of cases, especially when you aren't doing it in Update(). Don't roll your own when the API is already implemented!
Ah good to know, I'm still learning all of it so not that proficient. I just recently finished my Java classes for school and well, academia $$anonymous$$ches you the bare $$anonymous$$imum in order to get their money out of you and give you credit for your course lol. I think I've learned more on my own and through trying to build Unity games than I have through most of my classes. Then I always find out that I know absolutely nothing still when it comes to program$$anonymous$$g every time I try something new. Well, not really nothing, but just the tip of the iceberg.
Yep, you're only really a programmer when you realize that the iceberg never ends and no matter how far you travel, it'll still look like you're on the tip :) Learning is success in this industry.
Your answer
Follow this Question
Related Questions
How can i use a bool to decide when to move the character or not ? 2 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers