2018-09-28 19:18:50 luoyikun 阅读数 749
    private bool bInTouch = false;
    private float ClickAfter = 0.0f;
    GameObject m_curClickBox;
    private Vector3 mousePosLast = Vector3.zero;
    private bool Dragged = false;

    private bool bTemporarySelect = false;
 public void Update()
    {
        //if (Input.GetMouseButton(0))
        //{

        //    if (EventSystem.current.IsPointerOverGameObject())
        //    {
        //        //Debug.Log("left-click over a GUI element!");
        //        return;
        //    }

        //    if (!bInTouch)
        //    {
        //        bInTouch = true;

        //    }
        //}
        //else
        //{
        //    if (bInTouch)
        //    {
        //        bInTouch = false;

        //        EventManager.Instance.DispatchEvent(Common.EventStr.DeleteBtHomeHeadUi);
        //    }
        //}

        if (Input.GetMouseButton(0))
        {

            if (Application.isMobilePlatform && Input.touchCount > 0)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                    return;
            }
            else if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            if (bInTouch == false)
            {

                bInTouch = true;
                ClickAfter = 0.0f;
                mousePosLast = Input.mousePosition;
                bTemporarySelect = false;
                Dragged = false;

                //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //RaycastHit hit;
                //if (Physics.Raycast(ray, out hit) && (hit.collider.gameObject.tag == "HtBox"))
                //{
                //    m_curClickBox = hit.collider.gameObject;
                //}
                //else
                //{
                //    m_curClickBox = null;
                //}
            }
            else
            {
                if (Vector3.Distance(Input.mousePosition, mousePosLast) > 0.01f)
                {
                    if (!Dragged)
                    {
                        Dragged = true;
                    }
                }
                else
                {
                    if (!Dragged)
                    {
                        ClickAfter += Time.deltaTime;
                        if (!bTemporarySelect && (ClickAfter > 0.5f))
                        {
                            bTemporarySelect = true;
                            //Debug.Log ("Update2 buildingSelected:"+((buildingSelected != null) ? buildingSelected.name : "none"));
                            PickOneBox();
                        }
                    }
                }
            }
        }
        else
        {
            if (bInTouch)
            {
                bInTouch = false;

                if (Dragged)
                {

                }
                else {
                    if (bTemporarySelect)
                    {
                    }
                    else {
                        PickOneBox();
                    }
                }
            }
        }
    }
    void PickOneBox()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.tag == "HtBox")
            {
                hit.collider.gameObject.GetComponent<BoxCtrl>().OnPick();
              
            }   
        }
    }
2012-03-29 16:14:28 xgx198831 阅读数 10026

功能:

1.显示视窗及关闭视窗功能

2.单击物体弹出GUI窗口 

[javascript] view plaincopy
  1. static var WindowSwitch : boolean = false;  
  2. var mySkin : GUISkin;  
  3. var windowRect = Rect (200, 80, 240, 100);  
  4. function OnGUI ()  
  5. {  
  6.    if(WindowSwitch ==  true)  
  7.    {  
  8.       GUI.skin = mySkin;  
  9.         windowRect = GUI.Window (0, windowRect, WindowContain, "测试视窗");  
  10.    }  
  11. }  
  12. function WindowContain (windowID : int)  
  13. {  
  14.     if (GUI.Button (Rect (70,40,100,20), "关闭视窗"))  
  15.    {  
  16.       WindowSwitch = false;  
  17.    }  
  18. }  
 

[javascript] view plaincopy
  1. function OnMouseEnter ()  
  2. {  
  3.     renderer.material.color = Color.red;  
  4. }  
  5. function OnMouseDown ()  
  6. {  
  7.    Func_GUIWindow.WindowSwitch = true;  
  8. }  
  9. function OnMouseExit ()  
  10. {  
  11.    renderer.material.color = Color.white;  
  12. }  
2017-10-10 10:33:04 qq_30087879 阅读数 2239

在update中检测

首先要在摄像机中添加组件Physics Raycaster

public void Update(){
if(Input.getMouseButtonUp(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rayhit;
if(Physics.Raycast(ray,out rayhit)){
debug.log(rayhit.collider.gamaObject.name);

}
}


 

2017-08-10 11:22:00 R873112 阅读数 187

1.将下面该段代码赋给摄像机

void Update ()
{
    RaycastHit hit = new RaycastHit();
    for (int i = 0; i < Input.touchCount; ++i)
    {
        if (Input.GetTouch(i).phase.Equals(TouchPhase.Began))
        {
            // Construct a ray from the current touch coordinates
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
            if (Physics.Raycast(ray, out hit))
            {
                hit.transform.gameObject.SendMessage("OnMouseDown");
            }
         }
    }
}
  2.为要被点击的3D物体创建一个C#脚本,并   添加OnMouseDown函数,以使上段代码进行触发执行;   

void OnMouseDown()
{
    DoSomething();
}
This script can be attached to any GameObjects that you want to use as buttons.   

转载于:https://www.cnblogs.com/feiyanstudio/articles/7338286.html

没有更多推荐了,返回首页