I was switching between 2 GameObjects with a bool switch. Now I have 3 options. Whats the best way to select the one I want, Case Switch?
enum, http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.71).aspx and then a switch case would be the fastest and best implementation
So I discovered I could manage as many game objects as I wanted by using gameObject.activeSelf easier that I could maintain a bool for just 2 of them. But not before exploring enums, and I know i’m missing something important. Here is where I was at…
using UnityEngine;
using System.Collections;
enum CameraType
{
FirstPerson,
OrbitCamera,
OverShoulder,
DocumentCamera
};
public class MainGUI : MonoBehaviour
{
public GameObject advancedPlayer;
public GameObject kgfOrbitcam;
public GameObject shoulderCamera;
public GameObject docCamera;
//a bunch of other stuff
static GameObject ActiveCamera(CameraType cameraType)
{
switch (cameraType)
{
case CameraType.FirstPerson:
{
return advancedPlayer;
}
case CameraType.OrbitCamera:
{
return kgfOrbitcam;
}
case CameraType.OverShoulder:
{
return shoulderCamera;
}
case CameraType.DocumentCamera:
{
return docCamera;
}
default:
{
return null;
}
}
}
}
But i’m not seeing how I can use this to my advantage. The examples were too simple, they only showed the structure, not any practical use. I have a lot of heavy redundant code I believe I could clean up with enums, but i’m not sure how. Foe example, while I am experimenting I usually end up with something like this…
if(GUILayout.Button ("First",GUILayout.Width(width/4)))
{
advancedPlayer.SetActive(true);
kgfOrbitcam.SetActive(false);
shoulderCamera.SetActive(false);
docCamera.SetActive(false);
controller.SetState("Freeze", false);
camera.SetState("Freeze", false);
RestoreCam();
}
times 4 buttons. well, you get the idea. Can you give me an example of how to use enums? Thanks.
why are you using 4 cameras?
You can use enums just like basic conditional statements and assignments.
eg - Js
CameraType = CameraType.FirstPerson; // I'm in a button
if(CameraType==CameraType.FirstPerson) {
// Wash my car - lol
}
@ JamesLeeNZ - True dat.
@ Op - It would be much easier to just reposition the one cam with the enum.
Examples: [quote=“Jacksendary, post:2, topic: 506627, username:Jacksendary”]
http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.71).aspx
[/quote]
Remember you can have a variable for your enum type, Just as Black mantis shows.
http://www.topofsteel.com/Unity/JTCC-23_6-7-13-2_web.html
it’s a really rough build, my lightmaps and dynamic lighting are a mess. And I’m actually only using 3 cameras, I recycle the orbit cam for the 3rd person (shoulder). Right click for the menu.
@Black Mantis
I think i’m accomplishing exactly the same thing with…
advancedPlayer.SetActive(true);
if (advancedPlayer.activeSelf)
{
//Wash my car - ;)
}
@jakckie0100
I do appreciate your original response! And looked through the Msdn examples, great if I was making a calendar or concerned about to long numbers I’m not clear how using enums would help me with better organizing my code. I don’t see how I could use them to simplify turning on and off game objects (in my case camera prefabs). Back to my original post about using a bool to control 2 cameras, can I use enums to simplify turning off the unwanted cameras and on the one I do want? I worked out a method where I pass it the name of the camera I want, select case returns the one I want. It consolidates the code, but I couldn’t apply enums in a way I thought bettered it. I am really just trying to learn.
Thank you!