问题

一.小魔法师的探险

1.Unity中UI的穿透问题

黑暗之光中当点击老爷爷出现UI界面时,鼠标上还有主角的一个点击效果的触发,这就是鼠标“穿透”了。解决办法是:

(1.)使用 NGUI 的话 有一个判断可以使用,利用 UICamera.hoveredObject 来判断,如果鼠标在 ui 上返回 true,否则返回 false。

(2.)UGUI利用EventSystem.current.IsPointerOverGameObject()

如果我们使用的 UGUI 的话,并没有这个方法。那我们也可以使用其他方法,UGUI 系统提供了更为简便的方法。

先新建一个 Plane,然后给他添加一个 tag 为 plane。然后给摄像机添加一个叫做 MouseClick 的脚本:

using UnityEngine;
using System.Collections;
public class MouseClick : MonoBehaviour {

   void Update () {
        //如果左键按下
        if (Input.GetMouseButtonDown(0))
        {
            Click();
        }
    }
    void Click ()
    {
        //从主这相机到鼠标点发射一条射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //接受射线返回的碰撞信息
        RaycastHit hitInfo;
        //如果射线碰撞到东西
        if (Physics.Raycast(ray, out hitInfo))
        {
            if (hitInfo.collider.tag == "plane")
            {
                Debug.Log("点击了Plane...");
            }
        }
    }
}

现在我们添加一个 Button 也给它添加一个 脚本 BtnClick:

using UnityEngine;
using System.Collections;
public class BtnClick : MonoBehaviour {
    public void Click ()
    {
        Debug.Log("点击了按钮...");
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class MouseClick : MonoBehaviour {

   void Update () {
        //如果左键按下
        if (Input.GetMouseButtonDown(0))
        {
            Click();
        }
    }
    void Click ()
    {
        //从主这相机到鼠标点发射一条射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //接受射线返回的碰撞信息
        RaycastHit hitInfo;
        //如果射线碰撞到东西
        //EventSystem.current.IsPointerOverGameObject()  如果当前鼠标在 ui 上返回true 否则返回false
        if (Physics.Raycast(ray, out hitInfo) && !EventSystem.current.IsPointerOverGameObject())
        {
            if (hitInfo.collider.tag == "plane")
            {
                Debug.Log("点击了Plane...");
            }
        }
    }
}

 2.

二.决战丛林

1.粘包问题

在客户端给服务器发送消息的时候,由于发送消息比较频繁,就出现了粘包问题

解决:

 

posted @ 2018-04-11 23:27  薛林瑶Risible  阅读(111)  评论(0编辑  收藏  举报