泰斗破坏神(一)登陆界面的制作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36215025/article/details/67634135

一、导入UI素材

二、UI界面的制作

  • 云飘动的效果:利用NGUI里面的添加动画Tween-position,选择pingpong。通过调节云的Depth来控制显示遮挡的远近。

三、按钮事件的监听

这里写图片描述
这里写图片描述

  • 服务器选择界面(注册和账号登陆的实现与服务器选择大同小异)
    • 更新已选择服务器
      其中困扰了N天的bug是点击已选择服务器后,该已选择服务器的label文字为null,登陆start面板上的服务器label文字也为null。原因是已选择服务器上挂载了ServerProperty脚本,相冲突了。
using UnityEngine;
using System.Collections;

public class StartmenuController : MonoBehaviour {

public TweenScale startpanelTween;
public TweenScale serverPanelTween;  

public GameObject serverRed;  //火爆服务器
public GameObject serverGreen;  //正常服务器
public GameObject serverSelectGo;    //已选择服务器

public UIGrid serverGird;  //有序排列
public string serverNameSelected;  //已选择服务器的名字  ?需不需要用static?
public static ServerProperty sp;   //挂载在每个服务器上面的脚本SP  清先看第二个代码块
public UILabel serverLableStart;  //登陆界面选择服务器的label
private bool haveInitServer =false;

void Start() {
        InitServerlist();//初始化服务器列表
}

public void InitServerlist() {
        if (haveInitServer) return;

        //1.连接服务器 取得游戏服务器列表信息
        //TODO
        //2.根据上面的信息  添加服务器列表
        //TODO
        for (int i = 0; i < 20; i++) {
            string ip = "127.0.0.1:9080";
            string name = (i + 1) + "区 马达加斯加";
            int count = Random.Range(0, 100);
            GameObject go = null;
            if (count > 50)
            {
                //火爆
      go=NGUITools.AddChild(serverGird.gameObject,serverRed);  //UI界面添加对象
            }
            else { 
                go =NGUITools.AddChild(serverGird.gameObject,serverGreen);
            }
            ServerProperty sp =go.GetComponent<ServerProperty>();
            sp.ip = ip;
            sp.name = name;
            sp.count = count;
            serverGird.AddChild(go.transform);   //将对象放入Gird管理排序
        }
        haveInitServer = true;
    }
}
  //更新已选择服务器
    public void OnServerSelect(GameObject serverGo) { 
        sp =serverGo.GetComponent<ServerProperty>();
        serverSelectGo.GetComponent<UISprite>().spriteName = sp.GetComponent<UISprite>().spriteName;
        serverSelectGo.transform.Find("label").GetComponent<UILabel>().text = sp.name;
        serverSelectGo.GetComponent<UIButton>().normalSprite = serverGo.GetComponent<UIButton>().normalSprite;
        serverSelectGo.transform.Find("label").GetComponent<UILabel>().color = serverGo.transform.Find("label").GetComponent<UILabel>().color;
        serverNameSelected = sp.name;

    }

    //按下已选择服务器按钮
    public void OnServerPanelClose() {
        serverPanelTween.PlayReverse();  //动画反播
        StartCoroutine(HidePanel(serverPanelTween.gameObject));
        startpanelTween.gameObject.SetActive(true);
        startpanelTween.PlayReverse();
        serverLableStart.text = sp.name;
        }
        //隐藏面板
    IEnumerator HidePanel(GameObject go) {
        yield return new WaitForSeconds(0.4f);  //根据动画播放长短来定时间
        go.SetActive(false);
    }
using UnityEngine;
using System.Collections;

public class ServerProperty : MonoBehaviour { //这是挂载在每个服务器上面的脚本,注意已选择服务器上面不能挂载,否则登陆界面无法取到赋值后的服务器名字!!!

    public string ip="127.0.0.1:9080";
    private  string _name;
    public  string name {
        set {
            transform.Find("label").GetComponent<UILabel>().text = value;
            _name = value;
        }
        get {
            return _name;
        }
    }

    public int count=100;

    public void OnPress(bool isPress) {
        if (isPress == false) {
            //选择了当前的服务器
            transform.root.SendMessage("OnServerselect",this.gameObject);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_36215025/article/details/67634135
今日推荐