- Home /
Loading layers using a slider
Hi guys, I'm brand new to Unity and C#, so please excuse my lack of familiarity with writing code. I currently have a script that loads layers by listening for a key down. The layers are tagged so only certain ones are loaded for certain keys. At the moment I only have one layer, but there will be more, so there are a few spare bits commented out in the code.
This has worked for me so far, so I'd like to use this same principle to load layers using a slider, so that certain layers are loaded when the slider reaches a certain value. The slider value range is between 1314 and 1600 and I'm trying to illustrate to the user how something changed over time. I am trying to adapt the code I'm already using (below, as it currently stands, working perfectly and listening for GetKeyDown) so that the input is the current slider value at the if statement, but I don't seem to be able to retrieve the slider value.
I've tried several combinations of declaring variables and public classes and getting errors every time. I know it should be something to do with the slider value being >= X but I can't quite get there. I'm sure there's something simple I'm missing. Would anyone be able to help or point me in the right direction?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class layerLoad2 : MonoBehaviour {
//declare ints to hold the layer mask bits
int LoggiaDeiLanziLayerIndex;
//also an int to hold the current mask and to hold
//the new layer mask each time
int currentCullingMask;
int newLayerMask;
bool LoggiaDeiLanziShown = true;
//bool treesShown = true;
// Use this for initialization
void Start () {
LoggiaDeiLanziLayerIndex = LayerMask.NameToLayer("LoggiaDei_Lanzi");
}
void showLayer(int showLayerIndex){
currentCullingMask = GetComponent<Camera>().cullingMask;
newLayerMask = 0;
newLayerMask |= 1 << showLayerIndex;
GetComponent<Camera>().cullingMask = (currentCullingMask | newLayerMask);
}
void hideLayer(int hideLayerIndex){
currentCullingMask = GetComponent<Camera>().cullingMask;
newLayerMask = 2147483647;
newLayerMask &= ~(1 << hideLayerIndex);
GetComponent<Camera>().cullingMask = (currentCullingMask & newLayerMask);
}
public Slider
// Update is called once per frame
void Update () {
//listen for specific keys to change the layers
if (Input.GetKeyDown(KeyCode.T)) {
//check the status
if(LoggiaDeiLanziShown){
//if Shown then hide
hideLayer(LoggiaDeiLanziLayerIndex);
LoggiaDeiLanziShown = false;
}
else{
//treesShown is hidden so show it!
showLayer(LoggiaDeiLanziLayerIndex);
LoggiaDeiLanziShown = true;
}
}
}
}