16NGUI之商店(shop)购物系统

制作商品的UI显示###

效果展示

一、商品铺的搭建##

商品铺的搭建

二、点击一个具体的商品,显示具体信息的搭建##

并且设置为不可见,但满足我的逻辑条件的时候才可显示出来
显示框的搭建

三、商品铺满Shop里面##

通过加载预制物的方法,制作预制物.
预制物的制作
加载脚本的显示:
public class ShopTest : MonoBehaviour {

    public GameObject two;//获取预制物
    public GameObject[] cells;//获取格子

    //public string[] equpimentsName; 
    void Start () {
        int num = 0;
       
        for (int i = 0; i < cells.Length; i++)
        {
            num++;
            GameObject clone = Instantiate(two, Vector3.zero, Quaternion.identity) as GameObject;//加载预支物
            clone.transform.parent = cells[i].transform;
            clone.transform.localPosition = Vector3.zero;
            clone.transform.localScale = new Vector3(1, 1, 1);
            clone.transform.GetComponent<UISprite>().spriteName =num.ToString();
            //这是通过改变预制物的名字,让系统自动去找到我们所要在商店展示的物品信息
            clone.transform.GetComponent<UISprite>().height = 40;
            clone.transform.GetComponent<UISprite>().width = 40;
           
        }
       
    }
    
    
    void Update () {
    
    }

}

四、给商品添加配置文件,不同的商品不同信息##

给每个商店里面的物品一个自己的信息
脚本展示:获取text文件信息
public class EveryThing : MonoBehaviour {

    public string price;//价格
    public string thingName;//商品名字
    public string shouMing;//效果作用
    public string spriteNum;//在NGUI里面的物品名字

     GameObject buy;
    
    void Start () {

        buy = GameObject.FindGameObjectWithTag("Buybtn");//找到Buybtn这个按钮物体

        spriteNum = this.GetComponent<UISprite>().spriteName;
        TextAsset thingtext = (TextAsset)Resources.Load("ui");//文本文件的加载
        string str = thingtext.text;
        string[] thingname= str.Split(',');//分离不同物品的不同信息
       // print("123");
        for (int i = 0; i < thingname.Length; i++)
        {
            //print(thingname[i]);
            //print(spriteNum);
            if (thingname[i]==spriteNum)
            {
                price = thingname[i+1];
                thingName = thingname[i+2];
                shouMing = thingname[i+3];
                print(price + "      " + thingName + "     " + shouMing);
                break;
            }
        }
        
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    public void ClickTwo()
    {
        buy.GetComponent<Buybtn>().two = this.gameObject;
    }



}

五、各种点击事件方法,逻辑##

各种点击事件的先对应,逻辑。tag的合理使用,如何在不同的脚本获取其他脚本的方法..
脚本展示
public class Buybtn : MonoBehaviour {
    //挂载在Buybtn的脚本
    //各种点击事件的方法
    public GameObject sureBuyWindow;
    UIButton btn;
    public  GameObject two;//通过tag值获取

    int num = 1;


    void Start () {
        btn = GetComponent<UIButton>();
    }
    
    
    void Update () {
    
    }
    public void BuyWindow()
    {
        //只有选中格子里面的物品,才可以进行点击购买显示框的弹出
        if (two!=null) {
            if (sureBuyWindow.activeSelf == false)
            {
                sureBuyWindow.SetActive(true);
            }
            else
            {
                //btn.enabled = true;
                sureBuyWindow.SetActive(false);
            }
            btn.enabled = false;
            GameObject.FindGameObjectWithTag("money").GetComponent<UILabel>().text = two.GetComponent<EveryThing>().price;
        }
       
    }

    public void AgainClick()//从新选物品点击展示效果
    {
        if (two != null)
        {
            num = 1;
            GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text = num.ToString();
            GameObject.FindGameObjectWithTag("money").GetComponent<UILabel>().text = (num * int.Parse(two.GetComponent<EveryThing>().price)).ToString();
        }
        else
        {
            sureBuyWindow.SetActive(false);
        }
        //btn.enabled = false;

    }


    public void OnSubmitLabel()//在输入文本框里面进行数据的输入提交
    {
        //GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text = num.ToString();
        GameObject.FindGameObjectWithTag("money").GetComponent<UILabel>().text = (int.Parse(GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text) * int.Parse(two.GetComponent<EveryThing>().price)).ToString();
        num = int.Parse(GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text);
    }



    public void AddGameObject()//点击添加按钮的事件,把相应的数据改变
    {

        num = int.Parse(GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text);
       //num=PlayerPrefs.GetInt("+",0);
        num++;
        // PlayerPrefs.SetInt("+", num);

        //GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text = PlayerPrefs.GetInt("+").ToString();
        GameObject.FindGameObjectWithTag("number").GetComponent<UILabel>().text = num.ToString();
       // GameObject.FindGameObjectWithTag("money").GetComponent<UILabel>().text = (PlayerPrefs.GetInt("+") * int.Parse(two.GetComponent<EveryThing>().price)).ToString();
        GameObject.FindGameObjectWithTag("money").GetComponent<UILabel>().text = (num * int.Parse(two.GetComponent<EveryThing>().price)).ToString();
    }


}

六、重难点的理解##

1.预制物名字的改变系统之自动帮我们寻找改变后的物体.
2.理清各种逻辑,符合我们的需求.
3.在不同的脚本之间各种变量的获取.
4.在UI制作过程中,不同物体Widgt的Depth(深度)不同,显示不同。数值越小,在底层。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,637评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 在印度洋里的一点眼泪中滚圈 七.海边漫步 清晨,享用了房东的早餐后,我们直奔海边。今日目的地是Mirissa美蕊莎...
    难得一游阅读 430评论 0 0
  • Mac 安装redis 下载 去官网下载最新的稳定版本,这里是3.2.11 安装 下载完成后,打开命令行工具,...
    天堂的码头阅读 351评论 0 3
  • 去除数据中的重复,并将剩余部分转换成标准、可接受格式的处理过程。 1、数据问题 数据缺失 通过手动或规则填充数据;...
    小马哥志峰阅读 403评论 0 0