I know there is a lot of posts and such about this, but I have yet to find anything that solves my problem. I am trying to create an RGBA for my app, and I need to update the sliders with new values during certain events (such as key-presses), but I cannot do that, so the original color ends up staying. I am trying to make an app that emulates call of duty’s emblem editor, and I need to be able to grab the original layer’s colors and apply that to the sliders so that the color does not change when it is not needed(which is really annoying because then the colors end up staying the same from layer transition of A to B and so on). Lines 195-208 I am attempting to modify the slider’s values ( I tried Slider.value and Slider.normalizedValue so far).
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public enum MoveType{
Move,
Scale,
Rotate
}
public class AppManager : MonoBehaviour {
public List<Icon> layers;
public List<GameObject> layerObj;
public List<GameObject> icons;
public GameObject badgeCenter;
public GameObject editorPanel;
public Camera appCam;
public bool updateIcons = false;
public bool updateUI = true;
public int selectedSlot = 0;
public int previousSlot;
public int layerMax = 16;
public float centerX;
public float centerY;
public float centerZ;
public float sensitivity = 1f;
public Text layersUI;
public Text moveTypeUI;
public Text selectedUI;
public Text xUI;
public Text yUI;
public Text scaleXUI;
public Text scaleYUI;
public Text RotateUI;
public Slider senseSlide;
public Slider colorRUI;
public Slider colorGUI;
public Slider colorBUI;
public Slider colorAUI;
public MoveType moveType = MoveType.Move;
void Start () {
layers = new List<Icon>();
icons = new List<GameObject> ();
Object[] subIcons = Resources.LoadAll ("BadgeIcons", typeof(GameObject));
foreach (GameObject g in subIcons) {
icons.Add (g);
}
editorPanel.SetActive (false);
layerMax -= 1;
centerX = badgeCenter.transform.position.x;
centerY = badgeCenter.transform.position.y;
centerZ = badgeCenter.transform.position.z;
}
void Update () {
IconControls ();
UpdateIcon ();
sensitivity = senseSlide.value;
MoveTypeUIChange ();
}
public void AddLayer(){
if (layers.Count <= layerMax) {
layers.Add (new Icon (0, 0, 0, 10, 10, 0, 0, 255, 255, 255, 255));
selectedSlot = layers.Count - 1;
GameObject temp = Instantiate(icons[0]) as GameObject;
layerObj.Add (temp);
Vector3 tempScale = new Vector3 (10, 10, 0);
layerObj [selectedSlot].transform.localScale += tempScale;
layers [selectedSlot].scaleX = layerObj [selectedSlot].transform.localScale.x;
layers [selectedSlot].scaleY = layerObj [selectedSlot].transform.localScale.y;
layerObj [selectedSlot].transform.SetParent (badgeCenter.transform);
layerObj [selectedSlot].transform.position = badgeCenter.transform.position;
editorPanel.SetActive (true);
}
}
public void DeleteLayer(){
if (layers.Count >= 1) {
updateUI = false;
Destroy(layerObj[selectedSlot]);
layers.RemoveAt (selectedSlot);
layerObj.RemoveAt (selectedSlot);
updateUI = true;
selectedSlot -= 1;
if (selectedSlot < 0) {
selectedSlot = 0;
}
}
}
public void UpdateEditorPanel(){
if (updateUI) {
//UI
layersUI.text = layers.Count.ToString ();
//Values
selectedUI.text = selectedSlot.ToString ();
xUI.text = layers [selectedSlot].iconX.ToString ();
yUI.text = layers [selectedSlot].iconY.ToString ();
scaleXUI.text = layers [selectedSlot].scaleX.ToString ();
scaleYUI.text = layers [selectedSlot].scaleY.ToString ();
RotateUI.text = layers [selectedSlot].rotateX.ToString ();
}
}
public void IconControls(){
//INPUTS FOR MODIFYING SELECTED LAYER/SLOT
if (layers.Count >= 1) {
UpdateEditorPanel ();
if (moveType == MoveType.Move) {
if (Input.GetKey (KeyCode.UpArrow)) {
layers [selectedSlot].iconY += 1 * sensitivity;
} else if (Input.GetKey (KeyCode.DownArrow)) {
layers [selectedSlot].iconY -= 1 * sensitivity;
}
if (Input.GetKey(KeyCode.RightArrow)){
layers[selectedSlot].iconX -= 1 * sensitivity;
} else if (Input.GetKey(KeyCode.LeftArrow)){
layers[selectedSlot].iconX += 1 * sensitivity;
}
} else if (moveType == MoveType.Scale) {
if (Input.GetKey (KeyCode.UpArrow)) {
layers [selectedSlot].scaleY += 1 * sensitivity;
} else if (Input.GetKey (KeyCode.DownArrow)) {
layers [selectedSlot].scaleY -= 1 * sensitivity;
}
if (Input.GetKey(KeyCode.RightArrow)){
layers[selectedSlot].scaleX -= 1 * sensitivity;
} else if (Input.GetKey(KeyCode.LeftArrow)){
layers[selectedSlot].scaleX += 1 * sensitivity;
}
} else if (moveType == MoveType.Rotate) {
/*if (Input.GetKey (KeyCode.UpArrow)) {
layers [selectedSlot].rotateY += 1 * sensitivity;
} else if (Input.GetKey (KeyCode.DownArrow)) {
layers [selectedSlot].rotateY -= 1 * sensitivity;
}*/
if (Input.GetKey(KeyCode.RightArrow)){
layers[selectedSlot].rotateX -= 1 * sensitivity;
} else if (Input.GetKey(KeyCode.LeftArrow)){
layers[selectedSlot].rotateX += 1 * sensitivity;
}
}
//UI TO LAYER SETTINGS
layers[selectedSlot].colorR = (int)colorRUI.value;
layers[selectedSlot].colorG = (int)colorGUI.value;
layers[selectedSlot].colorB = (int)colorBUI.value;
layers[selectedSlot].colorA = (int)colorAUI.value;
}
//=======================GENERAL KEY INPUTS=============================
//Add Layer
if (Input.GetKeyUp (KeyCode.Plus) || Input.GetKeyUp (KeyCode.KeypadPlus) || Input.GetKeyUp(KeyCode.Insert)) {
AddLayer ();
}
//Delete Current Layer
if (Input.GetKeyUp (KeyCode.Minus) || Input.GetKeyUp (KeyCode.Delete) || Input.GetKeyUp (KeyCode.KeypadMinus)) {
DeleteLayer ();
}
//Change Transform Type
if (Input.GetKeyUp (KeyCode.Alpha1)) {
moveType = MoveType.Move;
}
if (Input.GetKeyUp (KeyCode.Alpha2)) {
moveType = MoveType.Scale;
}
if (Input.GetKeyUp (KeyCode.Alpha3)) {
moveType = MoveType.Rotate;
}
//Change Selected Layer/Slot
if (Input.GetKeyUp (KeyCode.RightBracket)) {
selectedSlot += 1;
if (selectedSlot >= layers.Count) {
selectedSlot -= 1;
colorRUI.normalizedValue = (float)layers [selectedSlot].colorR;
colorGUI.normalizedValue = (float)layers [selectedSlot].colorG;
colorBUI.normalizedValue = (float)layers [selectedSlot].colorB;
colorAUI.normalizedValue = (float)layers [selectedSlot].colorA;
}
}
if (Input.GetKeyUp (KeyCode.LeftBracket)) {
selectedSlot -= 1;
if (selectedSlot < 0) {
selectedSlot += 1;
colorRUI.normalizedValue = (float)layers [selectedSlot].colorR;
colorGUI.normalizedValue = (float)layers [selectedSlot].colorG;
colorBUI.normalizedValue = (float)layers [selectedSlot].colorB;
colorAUI.normalizedValue = (float)layers [selectedSlot].colorA;
}
}
}
public void UpdateIcon(){
if (layers.Count >= 1) {
if (moveType == MoveType.Move) {
Vector3 tempLocation = new Vector3 (centerX + layers [selectedSlot].iconX, centerY + layers [selectedSlot].iconY, centerZ - (layerMax - selectedSlot));
layerObj [selectedSlot].transform.position = tempLocation;
}
if (moveType == MoveType.Scale) {
Vector3 tempScale = new Vector3 (layers [selectedSlot].scaleX, layers [selectedSlot].scaleY, 1);
layerObj [selectedSlot].transform.localScale = tempScale;
}
if (moveType == MoveType.Rotate) {
Vector3 tempRotate = new Vector3 (0,0, layers [selectedSlot].rotateX);
layerObj [selectedSlot].transform.eulerAngles = tempRotate;
}
Renderer rend = layerObj [selectedSlot].GetComponent<Renderer>();
rend.material.color = new Color32 (byte.Parse(layers [selectedSlot].colorR.ToString()), byte.Parse(layers [selectedSlot].colorG.ToString()), byte.Parse(layers [selectedSlot].colorB.ToString()), byte.Parse(layers [selectedSlot].colorA.ToString()));
}
}
public void MoveTypeUIChange(){
switch (moveType){
case MoveType.Move:
moveTypeUI.text = "[MOVE]";
break;
case MoveType.Scale:
moveTypeUI.text = "[SCALE]";
break;
case MoveType.Rotate:
moveTypeUI.text = "[ROTATE]";
break;
}
}
public void Quit(){
Application.Quit ();
}
}