Files
MI-38/Heli_with_panels/Assets/Scripts/Panel2D/ButtonCheckable.cs
2022-07-04 13:15:35 +03:00

166 lines
5.2 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<<<<<<< HEAD
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCheckable : MonoBehaviour
{
Dictionary<string, GameObject> buttonStatesList = new Dictionary<string, GameObject>();
// int posCount;
// int currentPos;
GameObject goUp;
GameObject goDown;
public bool isChecked = false;
public bool isStarted = false;
Image goUp_Img=null, goDown_Img=null;
SpriteRenderer goUp_SR = null, goDown_SR = null;
Manager man;
void Start()
{
man = GameObject.Find("Manager").GetComponent<Manager>();
// Debug.Log(go.name);
Transform[] buttonStates = this.GetComponentsInChildren<Transform>();
// Debug.Log(btnList.Length);
// goUp = new GameObject();
// goDown = new GameObject();
for (int i = 0; i < buttonStates.Length; i++)
{
if (i == 0) continue;
if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 2), "up") == 0) // this.name.Length - 3
goUp = buttonStates[i].gameObject;
if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 4), "down") == 0) // this.name.Length - 3
goDown = buttonStates[i].gameObject;
}
goUp_Img = goUp.GetComponent<Image>();
goUp_SR = goUp.GetComponent<SpriteRenderer>();
goDown_Img = goDown.GetComponent<Image>();
goDown_SR = goDown.GetComponent<SpriteRenderer>();
if (goUp_Img != null) goUp_Img.enabled = !isChecked;
if (goUp_SR != null) goUp_SR.enabled = !isChecked;
if (goDown_Img != null) goDown_Img.enabled = isChecked;
if (goDown_SR != null) goDown_SR.enabled = isChecked;
isStarted = true;
}
void OnMouseDown()
{
if (man.mode == "scedit" || man.mode == "play")
return;
OnCheck();
}
public void OnCheck()
{
if (goUp == null || goDown == null) Start();
if (goUp == null || goDown == null) return;
try
{
isChecked = !isChecked;
if (goUp_Img != null) goUp_Img.enabled = !isChecked;
if (goUp_SR != null) goUp_SR.enabled = !isChecked;
if (goDown_Img != null) goDown_Img.enabled = isChecked;
if (goDown_SR != null) goDown_SR.enabled = isChecked;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
public void SetState(int st)
{
if (st == 0) isChecked = true; else isChecked = false;
OnCheck();
}
}
=======
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCheckable : MonoBehaviour
{
Dictionary<string, GameObject> buttonStatesList = new Dictionary<string, GameObject>();
// int posCount;
// int currentPos;
GameObject goUp;
GameObject goDown;
public bool isChecked = false;
public bool isStarted = false;
Image goUp_Img=null, goDown_Img=null;
SpriteRenderer goUp_SR = null, goDown_SR = null;
Manager man;
void Start()
{
man = GameObject.Find("Manager").GetComponent<Manager>();
// Debug.Log(go.name);
Transform[] buttonStates = this.GetComponentsInChildren<Transform>();
// Debug.Log(btnList.Length);
// goUp = new GameObject();
// goDown = new GameObject();
for (int i = 0; i < buttonStates.Length; i++)
{
if (i == 0) continue;
if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 2), "up") == 0) // this.name.Length - 3
goUp = buttonStates[i].gameObject;
if (string.Compare(buttonStates[i].name.Substring(buttonStates[i].name.Length - 4), "down") == 0) // this.name.Length - 3
goDown = buttonStates[i].gameObject;
}
goUp_Img = goUp.GetComponent<Image>();
goUp_SR = goUp.GetComponent<SpriteRenderer>();
goDown_Img = goDown.GetComponent<Image>();
goDown_SR = goDown.GetComponent<SpriteRenderer>();
if (goUp_Img != null) goUp_Img.enabled = !isChecked;
if (goUp_SR != null) goUp_SR.enabled = !isChecked;
if (goDown_Img != null) goDown_Img.enabled = isChecked;
if (goDown_SR != null) goDown_SR.enabled = isChecked;
isStarted = true;
}
void OnMouseDown()
{
if (man.mode == "scedit" || man.mode == "play")
return;
OnCheck();
}
public void OnCheck()
{
if (goUp == null || goDown == null) Start();
if (goUp == null || goDown == null) return;
try
{
isChecked = !isChecked;
if (goUp_Img != null) goUp_Img.enabled = !isChecked;
if (goUp_SR != null) goUp_SR.enabled = !isChecked;
if (goDown_Img != null) goDown_Img.enabled = isChecked;
if (goDown_SR != null) goDown_SR.enabled = isChecked;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
public void SetState(int st)
{
if (st == 0) isChecked = true; else isChecked = false;
OnCheck();
}
}
>>>>>>> 3b1b9479a46e90d056b92897ea9f8422b25fc052