Grid下面的 子对象按钮如何回调

Posted 彭然心跳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grid下面的 子对象按钮如何回调相关的知识,希望对你有一定的参考价值。

我们游戏里们经常会遇到装备,或者其他的东西需要用一个链表装起来。那个这时Unity3D就要用到Grid,成为它的子物体的对象有些需求不至于成为一张图片静静的呆在那儿,老总还需要有点击的功能那么下面我就一步步介绍是如何让它们做的的;

1:对话框的基类

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class BWDialog : BWWnd {
  6     private bool m_bInitOK = false;
  7     //private BWBlur m_mask = null;
  8     //private BWBlur m_oldMask = null;
  9     // Use this for initialization
 10     //private BWDarkMask m_mask = null;
 11     private int m_setDepth =  0;
 12     private int m_nTimes = 0;
 13     private TweenScale m_scale;
 14     public bool m_bAni = false;
 15     public bool m_bDark = true;
 16     public UICall OnTweenOK = null;
 17     //private TweenScale m_scale2;
 18     protected override void Start()
 19     {
 20         base.Start ();
 21         Init ();
 22         //20170824 版号申请,先不用新手引导 
 23         if (MainUI.COPYRIGHT_VERSION == false) 
 24             AutoHelp.GetInst().DoHelp(this);
 25     }
 26 
 27     public int setDepth
 28     {
 29         get{
 30             return m_setDepth;
 31         }
 32     }
 33 
 34     void Update()
 35     {
 36         if (!m_bInitOK) {
 37             if (transform.parent != null)
 38                 Init ();
 39         }
 40 
 41         //
 42     }
 43 
 44     void ReShow()
 45     {
 46         //m_tween.RemoveOnFinished (base.DoRemove);
 47         gameObject.SetActive(true);
 48         m_nTimes = 0;
 49         GameObject parent = transform.parent.gameObject;
 50         FadeIn();
 51         int nDepth = GetMaxDepth(parent, true, gameObject);
 52         m_setDepth = nDepth + 5;
 53         if (m_bDark)
 54             BWDarkMask.Show(this, m_setDepth);
 55 
 56         DisableDown();
 57 
 58         if (m_bAni)
 59         {
 60             TweenScale s = gameObject.GetComponent<TweenScale>();
 61             if (s != null)
 62                 GameObject.DestroyImmediate(s);
 63             TweenScale scale1 = gameObject.AddComponent<TweenScale>();
 64             scale1.from = Vector3.zero;
 65             scale1.to = new Vector3(0.2f, 1.1f, 1f);
 66             scale1.duration = 0.12f;
 67             scale1.PlayForward();
 68             scale1.AddOnFinished(OnAniOver);
 69             m_scale = scale1;
 70             /*TweenScale scale2 = gameObject.AddComponent<TweenScale> ();
 71             scale2.from = Vector3.zero;
 72             scale2.to = Vector3.one;
 73             scale2.duration = 0.2f;
 74             scale2.PlayForward ();
 75             scale2.AddOnFinished (OnAniOver);
 76             m_scale1 = scale1;
 77             m_scale2 = scale2;*/
 78         } //else if (OnAniOver != null)
 79         else if (OnTweenOK != null)
 80             OnTweenOK();
 81     }
 82 
 83     void Init()
 84     {
 85         if (m_bInitOK)
 86             return;
 87         if (transform.parent == null)
 88             return;
 89         
 90         BWDialogSet set = AddComponent<BWDialogSet> (gameObject);
 91         set.m_dialog = this;
 92         ReShow ();
 93         m_bInitOK = true;
 94     }
 95 
 96     void OnAniOver()
 97     {
 98         m_nTimes++;
 99         if (m_nTimes == 1) {
100             m_scale.from = m_scale.to;
101             m_scale.to =Vector3.one;
102             m_scale.duration = 0.12f;
103             m_scale.ResetToBeginning ();
104             m_scale.PlayForward ();
105             //m_nTimes++;
106             return;
107         }/* else if (m_nTimes == 2) {
108             m_scale.from = m_scale.to;
109             m_scale.to = Vector3.one;
110             m_scale.duration = 0.1f;
111             m_scale.ResetToBeginning ();
112             m_scale.PlayForward ();
113             return;
114         }*/
115 
116         GameObject.DestroyImmediate (m_scale);
117         if (OnTweenOK != null)
118             OnTweenOK ();
119     //    GameObject.DestroyImmediate (m_scale2);
120         //TweenAlpha alpha = gameObject.GetComponent<TweenAlpha> ();
121         //if (alpha != null)
122         //    GameObject.DestroyImmediate (alpha);
123     }
124 
125     public int GetDepth ()
126     {
127         UIPanel pan = gameObject.GetComponent<UIPanel> ();
128         if (pan != null)
129             return pan.depth;
130         UIWidget wid = gameObject.GetComponent<UIWidget> ();
131         if (wid != null)
132             return wid.depth;
133         return 0;
134     }
135 
136     public override bool Remove()
137     {
138         gameObject.GetComponent<BWDialogSet>().m_bRemoving = true;
139         GameObject parent = transform.parent.gameObject;
140         base.Remove();
141 
142         //        Debug.LogError ("Error");
143         //int depth = m_mask.depth;
144         //m_mask.Remove ();
145         //m_mask = null;
146         //if (m_oldMask)
147         //    m_oldMask.Show ();
148 
149 
150         if (m_bDark)
151         {
152             if (BWDarkMask.Hide(m_setDepth))
153                 EnableDown();
154         }
155         else
156         {
157             EnableDown();
158         }
159 
160 
161         //BWBlur blur = FindBlurMask (parent,m_mask.depth);
162         //BWDarkMask blur = FindDarkMask(parent,depth);
163         //if (blur != null)
164         //    blur.gameObject.SetActive (true);
165         //Debug.LogError (blur.depth);
166         /*if (blur != null && blur.IsHided ()) {
167             blur.gameObject.SetActive (true);
168             GameObject.DestroyImmediate (m_mask.gameObject);
169             //blur.Show ();
170         }*/
171         return true;
172     }
173 
174 
175     /*bool DestroyNoneTop()
176     {
177         bool bOK = false;
178         Transform trans = gameObject.transform.parent;
179         int i, iCount = trans.childCount;
180         for (i = 0; i < iCount; i++) {
181             
182         }
183         return bOK;
184     }*/
185 
186 }

上面只是做的一个准备工作,下面就要开始了,在相应按钮下面添加这个脚本,相应的就会添加预制体,但是功能都已经实现好了。我们还得继续往里面追,是如何实现这个功能;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BwSocket;
public class PetDlg : BWDialog {
    public const string _strRes = "Final/UI/Pet/PetDlg";
    // Use this for initialization
    private PetInfo m_pet;
    private ArrayPage m_array;
    private CollecterPage m_colleter;
    private TechPage m_tech;
    protected override void Start () {
        OnTweenOK += DoInit;

        name = "Genius";
        m_pet = AddChildComponent<PetInfo> (gameObject, "Pet/Page");
        m_pet.Init();
        m_array = AddChildComponent<ArrayPage> (gameObject, "Formation/Page");
        //m_array.Init ();
        m_colleter = AddChildComponent<CollecterPage>(gameObject,"Collect/Page");
        //m_colleter.Init ();
        m_tech = AddChildComponent<TechPage> (gameObject, "Science/Page");
        //m_tech.Init ();
        AddComponent<MProtoGameToyFreeRep> (gameObject).Register (OnFreeOK);
        base.Start ();
        //XChyzClient.Ins.DoToysGet (0);
    }

    bool DoInit()
    {
        m_pet.Init ();
        m_array.Init ();
        m_colleter.Init ();
        m_tech.Init ();
        ActiveChildren (gameObject);
        XChyzClient.Ins.DoToysGet (0);
        return true;
    }

    void OnFreeOK(XProtoGameToyFreeRep rep)
    {
        DropOnly drop = ShowLazy<DropOnly> (DropOnly._strRes);

        GameObject src = Resources.Load ("Final/UI/Pet/PetRefine") as GameObject;

        drop.SetDrops (rep.dropItems,"提炼吃货",false,src);
    }
        
}

现在我们继续往里面追

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BwSocket;

public class FightPetView : PetView {

    // Use this for initialization
    protected override void Start () {
        
        base.Start ();
    }

    public override bool Init ()
    {
        m_petType = PetType.Fight;
        if(!base.Init()) return false;
        OnItem += OnPetDetail;//这里

        return true;
    }


    bool OnPetDetail(PetItem item)
    {
        PetDetail dlg = AddChild<PetDetail> (PetDetail._strRes);
        dlg.m_nPetID = item.GetID();
        dlg.Init ();
        return true;
    }

    protected override void OnFreeOK(XProtoGameToyFreeRep rep)
    {
        base.OnFreeOK (rep);
        SetLabelString ("Count/number", "战斗吃货:" + m_toys.Count);
    }

    public override void UpdateToys (List<TOYINFO> toys)
    {
        List<TOYINFO> newToys = new List<TOYINFO> ();
        int i, iCount = toys.Count;
        for (i = 0; i < iCount; i++) {
            TOYINFO info = toys [i];
            if (info.nType == PetView.GENIUSTYPE_BATTLE)
                newToys.Add (info);
        }
        SetLabelString ("Count/number", "战斗吃货:" + newToys.Count);
        base.UpdateToys (newToys);
    }

}

明天继续....

以上是关于Grid下面的 子对象按钮如何回调的主要内容,如果未能解决你的问题,请参考以下文章

WPF - 如何使用绑定创建菜单和子菜单

来自另一个片段的 Snackbar 回调

使用导航控制器按下后退按钮后如何防止前一个片段显示?

如何在嵌套片段内的两个子片段之间进行通信

使用导航控制器按下后退按钮后,如何防止先前的片段出现?

如何在地图片段 API v2 布局顶部添加按钮