Grid下面的 子物件按鈕如何回撥

彭然心跳發表於2017-09-13

我們遊戲裡們經常會遇到裝備,或者其他的東西需要用一個連結串列裝起來。那個這時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);
    }

}

明天繼續....

相關文章