diff options
Diffstat (limited to 'SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIPropBar.cs')
| -rw-r--r-- | SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIPropBar.cs | 97 | 
1 files changed, 97 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIPropBar.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIPropBar.cs new file mode 100644 index 0000000..d50aed1 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIPropBar.cs @@ -0,0 +1,97 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class UIPropBar : MonoBehaviour +{ +	public PropWidget m_PropTempalte; + +	public UISimpleGrid m_PropGrid; + +	public Text m_TextName; + +	private List<PropWidget> m_Props = new List<PropWidget>(); + +	private int m_CurrentIndex = 0; + +	public void Set() +	{ +		m_PropTempalte.gameObject.SetActive(false); + +		for (int i = 0; i < PlayerManager.Instance.props.Count; ++i) +		{ +			PropWidget widget = MakePropWidget(PlayerManager.Instance.props[i]); +			m_Props.Add(widget); +		} + +		SelectPropWidget(0); +	} + +	bool SwitchToLeft() +	{ +		return Input.GetButtonDown("LeftProp"); +	} + +	bool SwitchToRight() +	{ +		return Input.GetButtonDown("RightProp"); +	} + +	bool UseProp() +	{ +		return Input.GetButtonDown("Fire3"); +	} + +	PropWidget MakePropWidget(PropBase prop) +	{ +		PropWidget widget = Instantiate<PropWidget>(m_PropTempalte); +		widget.transform.SetParent(m_PropGrid.transform); +		widget.gameObject.SetActive(true); +		widget.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0); +#if UNITY_EDITOR +		widget.name = "prop (" + prop.name + ")"; +#endif + +		PropWidgetParam param = new PropWidgetParam(); +		//param.onSelected = OnSelectPropWidget; +		param.prop = prop; +		widget.Set(param); +		return widget; +	} + +	protected void Update() +	{ +		if (SwitchToLeft()) +		{ +			int newIndex = Mathf.Clamp(m_CurrentIndex - 1, 0, m_Props.Count - 1); +			SelectPropWidget(newIndex); +		} +		if (SwitchToRight()) +		{ +			int newIndex = Mathf.Clamp(m_CurrentIndex + 1, 0, m_Props.Count - 1); +			SelectPropWidget(newIndex); +		} +		if (UseProp()) +		{ +			m_Props[m_CurrentIndex].OnUseCallback(); +			PlayerManager.Instance.UseProp(m_Props[m_CurrentIndex].prop); +		} +	} + +	void SelectPropWidget(int index) +	{ +		if (index < 0 || index > m_Props.Count - 1) +		{ +			return; +		} +		m_Props[m_CurrentIndex].OnDeselectCallback(); +		m_Props[index].OnSelectCallback(); + +		m_TextName.text = m_Props[index].prop.name; +		m_TextName.gameObject.SetActive(false); + +		m_CurrentIndex = index; +	} + +}  | 
