DetailPageControl.cs 2.4 KB
Newer Older
haililiu's avatar
create  
haililiu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;

public class DetailPageControl : ObjectBase
{
    public RectTransform Box;
    public Button BtnClose;
    public GameObject BoxContent;
    private Transform ContentChild;
    private System.Action<string> OnClosed;
    public bool IsShow = false;

    private void Start()
    {
        EventListener.Get(BtnClose.gameObject).onClick = OnBtnCloseClicked;
    }

    void OnBtnCloseClicked(GameObject sender)
    {
        SoundManager.Instance.PlaySound("音效/按钮");
        Hide();
    }

    public GameObject Show(string contentName, Vector2 size, System.Action<string> onClosed, object data = null)
    {
        IsShow = true;
        base.ExcuteInit();
        OnClosed = onClosed;
        this.gameObject.SetActive(true);
        if (!string.IsNullOrEmpty(contentName))
        {
            Transform item = SignalObjectManager.Instance.Spawn(contentName);
            if (item == null)
            {
                Debug.Log("未找到" + contentName);
                return null;
            }
            item.parent = BoxContent.transform;
            item.GetComponent<RectTransform>().offsetMin = Vector2.zero;
            item.GetComponent<RectTransform>().offsetMax = Vector2.zero;
            item.GetComponent<DetailPage>().SetPar(0, data);
            item.GetComponent<DetailPage>().ExcuteInit();
            ContentChild = item;
        }
        Box.GetComponent<RectTransform>().sizeDelta = size;
        EventListener.Get(this.gameObject).onClick = OnBackGroundClicked;
        Box.transform.localScale = Vector3.zero;
        Box.transform.DOScale(1, 0.06f);
        return ContentChild.gameObject;
    }

    void OnBackGroundClicked(GameObject g)
    {
        //Hide();
    }

    public void Hide()
    {
        IsShow = false;
        base.ExcuteFree();
        if (ContentChild != null)
        {
            ContentChild.GetComponent<DetailPage>().ExcuteFree();
            SignalObjectManager.Instance.Despawn(ContentChild);
            if (OnClosed != null)
                OnClosed(ContentChild.GetComponent<DetailPage>().SelectedData);
        }
        Tweener tweener = Box.transform.DOScale(0, 0.06f);
        tweener.OnComplete(() =>
        {
            Box.transform.localScale = new Vector3(1, 1, 1);
            this.gameObject.SetActive(false);
        });
    }
}