签到面板要用到的资源文件,注意名字和路径: [

](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021401-300x243.png)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021401.png)签到界面图,item预制体: [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021402-300x205.jpg)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021402.jpg)最终实现的效果(gif图): [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021403-300x297.gif)
](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021401-300x243.png)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021401.png)签到界面图,item预制体: [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021402-300x205.jpg)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021402.jpg)最终实现的效果(gif图): [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021403-300x297.gif)
JSON压缩后的数据:

[
{“ItemName”:”item_1”,”ItemRank”:”0_white”,”ItemNum”:5},
{“ItemName”:”item_2”,”ItemRank”:”0_white”,”ItemNum”:10},
{“ItemName”:”item_6”,”ItemRank”:”0_white”,”ItemNum”:100},
{“ItemName”:”item_3”,”ItemRank”:”1_green”,”ItemNum”:100},
{“ItemName”:”item_7”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_9”,”ItemRank”:”3_purple”,”ItemNum”:60},
{“ItemName”:”item_8”,”ItemRank”:”2_blue”,”ItemNum”:80},
{“ItemName”:”item_5”,”ItemRank”:”1_green”,”ItemNum”:10},
{“ItemName”:”item_3”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_10”,”ItemRank”:”3_purple”,”ItemNum”:1},
{“ItemName”:”item_3”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_2”,”ItemRank”:”0_white”,”ItemNum”:10},
{“ItemName”:”item_6”,”ItemRank”:”0_white”,”ItemNum”:100},
{“ItemName”:”item_3”,”ItemRank”:”1_green”,”ItemNum”:100},
{“ItemName”:”item_9”,”ItemRank”:”3_purple”,”ItemNum”:60},
{“ItemName”:”item_3”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_4”,”ItemRank”:”2_blue”,”ItemNum”:20},
{“ItemName”:”item_9”,”ItemRank”:”2_blue”,”ItemNum”:70},
{“ItemName”:”item_6”,”ItemRank”:”0_white”,”ItemNum”:100},
{“ItemName”:”item_3”,”ItemRank”:”1_green”,”ItemNum”:100},
{“ItemName”:”item_7”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_3”,”ItemRank”:”2_blue”,”ItemNum”:50},
{“ItemName”:”item_9”,”ItemRank”:”3_purple”,”ItemNum”:50},
{“ItemName”:”item_10”,”ItemRank”:”2_blue”,”ItemNum”:1},
{“ItemName”:”item_8”,”ItemRank”:”2_blue”,”ItemNum”:1},
{“ItemName”:”item_8”,”ItemRank”:”3_purple”,”ItemNum”:5},
{“ItemName”:”item_8”,”ItemRank”:”4_orange”,”ItemNum”:10},
{“ItemName”:”item_10”,”ItemRank”:”5_gold”,”ItemNum”:10}
]

Item元素数据实体类:

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

///


/// 签到面板Item元素数据实体类.
///

public class SignInItem {

private string itemName;    //名称.
private string itemRank;    //等级.
private int itemNum;        //数量.

public string ItemName {  get{ return itemName;}   set{ itemName= value; }  }

public string ItemRank {  get{ return itemRank;}   set{ itemRank = value;}  }

public int ItemNum     {  get{ return itemNum; }   set{ itemNum = value; }  }

public override string ToString()
{
    return string.Format("名称:{0},等级:{1},数量:{2}", this.itemName, this.itemRank, this.itemNum);
}

}

签到面板管理器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

///


/// 签到面板管理器脚本.
///

public class SignInPanelManager : MonoBehaviour {

private Transform m\_Transform;

private List<SignInItem> itemList = new List<SignInItem>();

public GameObject prefab\_Item;

private Transform grid\_Transform;

//等级图片字典.
private Dictionary<string, Sprite> rankDic = new Dictionary<string, Sprite>();

//月签到数.
private int signInNum = 12;

void Start () {
    m\_Transform = gameObject.GetComponent<Transform>();
    string jsonStr = Resources.Load<TextAsset>("signIn").text;
    prefab\_Item = Resources.Load<GameObject>("Item");
    grid\_Transform = m\_Transform.Find("SignInBG/Grid").GetComponent<Transform>();
    //Debug.Log(jsonStr);

    //等级图片加载.
    Sprite\[\] rankSprite = Resources.LoadAll<Sprite>("SignIn/rank");
    for (int i = 0; i < rankSprite.Length; i++){
        //Debug.Log(rankSprite\[i\].name);
        rankDic.Add(rankSprite\[i\].name, rankSprite\[i\]);
    }

    JsonData jsonData = JsonMapper.ToObject(jsonStr);
    //Debug.Log(jsonData.Count);


    for (int i = 0; i < jsonData.Count; i++){
        SignInItem temp = JsonMapper.ToObject<SignInItem>(jsonData\[i\].ToJson());
        itemList.Add(temp);
    }

    jsonStr = null;
    rankSprite = null;

    //循环实例化Item.
    for (int i = 0; i < itemList.Count; i++){
        //Debug.Log(itemList\[i\].ToString());
        GameObject go = GameObject.Instantiate<GameObject>(prefab\_Item, grid\_Transform);
        //给子物体传值.
        Sprite tempRank = null;
        rankDic.TryGetValue(itemList\[i\].ItemRank, out tempRank);

        SignInItemManager sm = go.GetComponent<SignInItemManager>();

        if(i < signInNum){
            sm.SetItemValue(itemList\[i\].ItemName, tempRank, itemList\[i\].ItemNum, true);
        }
        else{
            sm.SetItemValue(itemList\[i\].ItemName, tempRank, itemList\[i\].ItemNum, false);
        }

        if (i == signInNum){
            sm.AddButton();
        }
    }

}

}

签到管理器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SignInItemManager : MonoBehaviour {

private Transform m\_Transform;

private Image rank\_Image;       //物品等级图标.
private Image item\_Image;       //物品图标.
private Text num\_Text;          //物品数量.
private GameObject mask;        //签到后的遮罩.

void Awake () {
    m\_Transform = gameObject.GetComponent<Transform>();

    rank\_Image = gameObject.GetComponent<Image>();
    item\_Image = m\_Transform.Find("Img").GetComponent<Image>();
    num\_Text = m\_Transform.Find("Num").GetComponent<Text>();

    mask = m\_Transform.Find("Mask").gameObject;
    mask.SetActive(false);
}

/// <summary>
/// 给Item赋值.
/// </summary>
public void SetItemValue(string name, Sprite rank, int num, bool isSignIn){
    //rank\_Image.sprite = Resources.Load<Sprite>("SignIn/rank/" + rank);
    rank\_Image.sprite = rank;
    item\_Image.sprite = Resources.Load<Sprite>("SignIn/item/" + name);
    num\_Text.text = num.ToString();
    if(isSignIn){
        mask.SetActive(true);
    }
}

/// <summary>
/// 给自身添加Button.
/// </summary>
public void AddButton(){
    gameObject.AddComponent<Button>();
}

}