using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DropDownBoxController : MonoBehaviour {
public GameObject m_btnClose;
public GameObject m_btnOpen;
private UIButton m_btnCloseDescript;
public GameObject m_btnCloseDefaultDescript;
public UIButton m_selectedButtonPrefabs;
public UIButton m_noSelectButtonPrefabs;
public GameObject m_childButtonRoot;
public GameObject m_childButtonBac;
private List<UIButton> m_childBtnSelectedList;
private List<UIButton> m_childBtnNoSelectList;
public List<UIButton> m_childBtnDescriptList;
private List<string> m_invokeFunctionList;
private List<System.Object> m_invokeFunctionParamList;
private string m_invokeOpenedFunc;
private System.Object m_invokeOpenedFuncParam;
private GameObject m_delegate;
private bool m_bOpened;
private int m_curSelectedIndex;
private bool m_bHaveParam;
// Use this for initialization
void Awake () {
m_selectedButtonPrefabs.gameObject.SetActive(false);
m_noSelectButtonPrefabs.gameObject.SetActive(false);
m_btnCloseDefaultDescript.SetActive(false);
m_childBtnSelectedList = new List<UIButton>();
m_childBtnNoSelectList = new List<UIButton>();
m_invokeFunctionList = new List<string>();
m_invokeFunctionParamList = new List<System.Object>();
m_childButtonBac.SetActive(false);
for (int i = 0; i < m_childBtnDescriptList.Count; i++) {
m_childBtnDescriptList[i].gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update () {
}
public void Init(GameObject delegateObj,List<string> invokeFunctionList)
{
m_delegate = delegateObj;
m_invokeFunctionList = invokeFunctionList;
InitButton();
Reset();
m_bHaveParam = false;
}
public void Init(GameObject delegateObj,List<string> invokeFunctionList,string invokeOpenedFunc)
{
m_delegate = delegateObj;
m_invokeFunctionList = invokeFunctionList;
m_invokeOpenedFunc = invokeOpenedFunc;
InitButton();
Reset();
m_bHaveParam = false;
}
public void Init(GameObject delegateObj,List<string> invokeFunctionList,List<System.Object> invokeFunctionParam,string invokeOpenedFunc,System.Object invokeOpenedFuncParam)
{
m_delegate = delegateObj;
m_invokeFunctionList = invokeFunctionList;
m_invokeFunctionParamList = invokeFunctionParam;
m_invokeOpenedFunc = invokeOpenedFunc;
m_invokeOpenedFuncParam = invokeOpenedFuncParam;
InitButton();
Reset();
m_bHaveParam = true;
}
public Vector3 m_childButtonOffset;
public Vector3 m_childDecorateOffset;
public Vector3 m_firstChildButtonLocalPosition;
public Vector3 m_firstChildDecorateLocalPosition;
public Vector3 m_btnOpenPositionOffset;
private void InitButton()
{
for (int i = 0; i < m_invokeFunctionList.Count; i++) {
UIButton selectedButton = ((GameObject)Instantiate(m_selectedButtonPrefabs.gameObject)).GetComponent<UIButton>();
selectedButton.methodToInvoke = "Invoke_Button" + i;
selectedButton.transform.parent = m_childButtonRoot.transform;
selectedButton.transform.localPosition = m_firstChildButtonLocalPosition + i * m_childButtonOffset;
m_childBtnSelectedList.Add(selectedButton);
}
for (int i = 0; i < m_invokeFunctionList.Count; i++) {
UIButton noSelectButton = ((GameObject)Instantiate(m_noSelectButtonPrefabs.gameObject)).GetComponent<UIButton>();
noSelectButton.methodToInvoke = "Invoke_Button" + i;
noSelectButton.transform.parent = m_childButtonRoot.transform;
noSelectButton.transform.localPosition = m_firstChildButtonLocalPosition + i * m_childButtonOffset;
m_childBtnNoSelectList.Add(noSelectButton);
}
for (int i = 0; i < m_invokeFunctionList.Count; i++) {
m_childBtnDescriptList[i].transform.parent = m_childButtonRoot.transform;
m_childBtnDescriptList[i].transform.localPosition = m_firstChildDecorateLocalPosition + i * m_childDecorateOffset;
m_childBtnDescriptList[i].gameObject.SetActive(true);
}
m_btnOpen.transform.parent = m_childButtonRoot.transform;
m_btnOpen.transform.localPosition = (m_firstChildButtonLocalPosition + m_invokeFunctionList.Count * m_childButtonOffset) + m_btnOpenPositionOffset;
}
public void Reset()
{
m_bOpened = false;
m_curSelectedIndex = -1;
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
}
//外部设定
public void SetDisplay(bool isOpen,int index)
{
m_bOpened = isOpen;
if (index != -2) {
m_curSelectedIndex = index;
}
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
}
private void SetButtonDisplay(bool isOpen,int index)
{
m_btnClose.SetActive(!isOpen);
m_btnOpen.SetActive(isOpen);
m_childButtonBac.SetActive(isOpen);
if (isOpen) {
for (int i = 0; i < m_childBtnNoSelectList.Count; i++) {
if (index == i) {
m_childBtnNoSelectList[i].gameObject.SetActive(false);
m_childBtnSelectedList[i].gameObject.SetActive(true);
}
else
{
m_childBtnNoSelectList[i].gameObject.SetActive(true);
m_childBtnSelectedList[i].gameObject.SetActive(false);
}
}
}
m_childButtonRoot.SetActive(isOpen);
}
public void Invoke_Button0()
{
m_curSelectedIndex = 0;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button1()
{
m_curSelectedIndex = 1;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button2()
{
m_curSelectedIndex = 2;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button3()
{
m_curSelectedIndex = 3;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button4()
{
m_curSelectedIndex = 4;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button5()
{
m_curSelectedIndex = 5;
InvokeButton(m_curSelectedIndex);
}
public void Invoke_Button6()
{
m_curSelectedIndex = 6;
InvokeButton(m_curSelectedIndex);
}
public void InvokeButton(int buttonIndex)
{
if (m_invokeFunctionList != null && m_invokeFunctionList.Count >= buttonIndex + 1) {
//触发回调
if (m_delegate != null) {
if (!m_bHaveParam) {
m_delegate.SendMessage(m_invokeFunctionList[buttonIndex]);
}
else
{
m_delegate.SendMessage(m_invokeFunctionList[buttonIndex],m_invokeFunctionParamList[buttonIndex]);
}
m_bOpened = true;
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
Close();
}
else
{
DebugError();
}
}
else
{
DebugError();
}
}
private void InvokeOpenedButton()
{
if (m_delegate != null) {
if (!m_bHaveParam) {
m_delegate.SendMessage(m_invokeOpenedFunc);
}
else
{
m_delegate.SendMessage(m_invokeOpenedFunc,m_invokeOpenedFuncParam);
}
m_bOpened = true;
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
Close();
}
else
{
DebugError();
}
}
//修改按钮内容
private void UpdateButtonDescript(bool isOpen,int index)
{
if (!isOpen) {
if(m_btnCloseDescript != null)
{
Destroy(m_btnCloseDescript.gameObject);
}
GameObject gameOjb = null;
if (index == -1) {
gameOjb = m_btnCloseDefaultDescript;
}
else
{
gameOjb = m_childBtnDescriptList[index].gameObject;
}
m_btnCloseDescript = ((GameObject)Instantiate(gameOjb)).GetComponent<UIButton>();
m_btnCloseDescript.transform.parent = m_btnClose.transform;
m_btnCloseDescript.transform.localPosition = new Vector3(0,0,-0.5f);
m_btnCloseDescript.gameObject.SetActive(true);
}
else
{
for (int i = 0; i < m_childBtnDescriptList.Count; i++) {
if (index == i) {
m_childBtnDescriptList[i].renderer.material.color = new Color(255/255.0f,27/255.0f,27/255.0f,1);
}
else
{
m_childBtnDescriptList[i].renderer.material.color = new Color(0/255.0f,255/255.0f,41/255.0f,1);
}
}
}
}
public void Invoke_Open()
{
m_bOpened = true;
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
}
public void Invoke_Close()
{
InvokeOpenedButton();
}
public void Close()
{
m_bOpened = false;
SetButtonDisplay(m_bOpened,m_curSelectedIndex);
UpdateButtonDescript(m_bOpened,m_curSelectedIndex);
}
private void DebugError()
{
Debug.LogError("Errorrrrr : Class_DropDownBoxController");
}
}
转载于:https://www.cnblogs.com/HelloJimmyWu/p/3559275.html