射线检测
- Raycast:2D检测和3D检测方法差不多,但是要注意2D检测使用Physics2D,并且被检测物体上需要添加2D的Collider,默认只检测碰撞到的第一个物体(物体重叠或者处于同一个直线上)
- RaycastAll:检测碰撞到的所有物体,返回数组
测试代码
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);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, Mathf.Infinity,LayerMask.GetMask("enemy1"));
Debug.Log(isCollider);
}
}
UGUI事件监听
- 直接拖拽:对象不受限制
- 代码添加:对象不受限制
测试代码
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.GetComponent<