API自学记录(三)

API自学记录(三)

射线检测之基本使用

RayCast从开始点发射一条射线,检测是否与物体发生碰撞

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

public class Player : MonoBehaviour {
	void Start () {
	}
	
	void Update () {
        Ray ray = new Ray(transform.position+transform.forward,transform.forward);
        bool isCollider=Physics.Raycast(ray);
        Debug.Log(isCollider);
	}
}

当cube处于Playe正前方时,发生碰撞检测,返回值为true

射线检测之重载方法

	void Update () {
        Ray ray = new Ray(transform.position+transform.forward,transform.forward);

        //bool isCollider=Physics.Raycast(ray);
        //bool isCollider = Physics.Raycast(ray, 1);//射线距离为1
        RaycastHit hit;
        //bool isCollider = Physics.Raycast(ray,out hit);
        bool isCollider = Physics.Raycast(ray,Mathf.Infinity,LayerMask.GetMask("Enemy1"));//只与Enemy1发生碰撞
        Debug.Log(isCollider);
        //Debug.Log(hit.collider);
        //Debug.Log(hit.point);//获取位置点
}

在这里插入图片描述

关于2D射线检测和检测碰撞到所有物体

   Physics.RaycastAll();//返回一个数据,检测到所有物体
    Physics2D.Raycast();//只能使用2D碰撞器

通过拖拽的方式监听UGUI的事件

添加UI项目
Button按键
Slider滑动条
DropDown下拉条
Toggle选择
在这里插入图片描述

通过代码添加对UGUI控件的事件监听

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

public class UIEventManager : MonoBehaviour {
    public GameObject btnGameObject;

    // Use this for initialization
    void Start () {
        btnGameObject.GetComponent<Button>().onClick.AddListener(this.ButtonOnClick);
    }
    void ButtonOnClick()
    {
        Debug.Log("ButtonOnClick");
    }
    // Update is called once per frame
    void Update () {
		
	}
}

在这里插入图片描述
全部添加代码如下:

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

public class UIEventManager : MonoBehaviour {
    public GameObject btnGameObject;
    public GameObject sliderGameObject;
    public GameObject dropDownGameObject;
    public GameObject toggleGameObject;

    // Use this for initialization
    void Start () {
        btnGameObject.GetComponent<Button>().onClick.AddListener(this.ButtonOnClick);
        sliderGameObject.GetComponent<Slider>().onValueChanged.AddListener(this.OnSliderChanged);
        dropDownGameObject.GetComponent<Dropdown>().onValueChanged.AddListener(this.OnDropDownChanged);
        //toggleGameObject.GetComponent<Toggle>().onValueChanged.AddListener(this.OnToggleChanged);
    }
    void ButtonOnClick()
    {
        Debug.Log("ButtonOnClick");
    }
    void OnSliderChanged(float value)
    {
        Debug.Log("SliderChanged:" + value);
    }
    void OnDropDownChanged(Int32 value)
    {
        Debug.Log("DropDownChanged:" + value);
    }
    void OnToggleChanged(bool value)
    {
        Debug.Log("ToggleChanged:" + value);
    }
    // Update is called once per frame
    void Update () {
	}
}

跟鼠标相关的事件接口的实现

前俩种方法对对象没有限制

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

public class UIEventManager2 : MonoBehaviour, IPointerDownHandler
{
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("OnPointerDown");
    }
}

在这里插入图片描述
全部代码:

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

public class UIEventManager2 : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
    IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler

{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("OnPointerClick");
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("OnPointerDown");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("OnPointerEnter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("OnPointerExit");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("OnPointerUp");
    }
}

在这里插入图片描述

跟拖拽相关的事件接口的实现

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class UIEventManager2 : 
IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");
    }

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("OnDrop");//在内部释放
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("OnEndDrag");
    }

版权声明:本文为weixin_43821563原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。