十六、射线检测(Physics)
Raycast:对场景中所有的碰撞体,从点开始,向长最大距离的方向发射射线。
RaycastAll:在场景中投射光线并返回所有命中。
Physics.Raycast(Ray,out RaycastHit);射线检测——第一个参数为要检测的射线,如果射线与其他物体相撞了,相撞的信息存储在第二参数里,返回类型bool,如果射线与场景中的物体碰撞了,返回值true,否则返回false
测试代码:
void Update () {
Ray ray = new Ray(transform.position+transform.forward, transform.forward);
//bool isCollider = Physics.Raycast(ray);
//bool isCollider = Physics.Raycast(ray, 1);
RaycastHit hit;
//bool isCollider = Physics.Raycast(ray, out hit);
bool isCollider = Physics.Raycast(ray, Mathf.Infinity, LayerMask.GetMask("Enemy1", "Enemy2", "UI"));
Debug.Log(isCollider);
//Debug.Log(hit.collider);
//Debug.Log(hit.point);
}
十七、监听UGUI事件
1、拖拽
Button:按钮
Slider:滑动条
Dropdown:下拉列表
Toggle:开关
2、代码添加
- using UnityEngine.UI;
测试代码:
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
public class UIEventManager : MonoBehaviour {
public GameObject btnGameObject;
public GameObject sliderGameObject;
public GameObject dropDownGameObject;
public GameObject toggleGameObject;
void Start () {
btnGameObject.GetComponent<Button>().onClick.AddListener(this.ButtonOnClick);
sliderGameObject.