一.射线检测
【1】Raycast
(1)基本方法
检测物体player前方是否有物体
void Update ()
{
Ray ray = new Ray(transform.position+transform.forward, transform.forward);
bool isCollider = Physics.Raycast(ray);
Debug.Log(isCollider);
}
(2)重载方法
<1>
void Update ()
{
Ray ray = new Ray(transform.position+transform.forward, transform.forward);
bool isCollider = Physics.Raycast(ray, 1); //player在1米范围内才能检测到敌人
Debug.Log(isCollider);
}
<2>
Ray ray = new Ray(transform.position+transform.forward, transform.forward);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
Debug.Log(isCollider);
Debug.Log(hit.collider);
Debug.Log(hit.point);

<3>
Ray ray = new Ray(transform.position+transform.forward, transform.forward);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, Mathf.Infinity, LayerMask.GetMask("Enemy1", "Enemy2", "UI"));//射线无限长,指定layer层发生碰撞。
Debug.Log(isCollider);
(3)RaycastAll
在当前情况下,Raycast默认输出碰撞到的第一个物体的信息;RaycastAll则输出碰撞到的两个的信息。
二. UGUI事件监听
【1】拖拽
在各自的inspector面板中找到类似这样的面板。
拖拽物体到红圈位置
【2】代码添加
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI; //创造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<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);
}
void Update () {
}
}
【3】通过实现接口
(1)
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 OnPonintClick(PointerEventData eventData)
{
Debug.Log("OnPonintClick");
}
public void OnPonintDown(PointerEventData eventData)
{
Debug.Log("OnPonintDown");
}
public void OnPonintEnter(PointerEventData eventData)
{
Debug.Log("OnPonintEnter");
}
public void OnPointExit(PointerEventData eventData)
{
Debug.Log("OnPointExit");
}
public void OnPointUp(PointerEventData eventData)
{
Debug.Log("OnPointUp");
}
}
一定要勾选Raycast Target
(2)跟拖拽相关的事件接口的实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class UIEventManager2 : MonoBehaviour,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");
}
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");
}
}
三. WWW
通过WWW下载图片
public string url = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2622066562,1466948874&fm=27&gp=0.jpg";//图片地址
IEnumerator Start()
{
WWW www = new WWW(url);
yield return www;
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = www.texture;
}
四. Touches触摸事件
if (Input.touches.Length > 0)
{
Touch touch1 = Input.touches[0];
TouchPhase phase = touch1.phase;
}
来源:CSDN
作者:???????wfjj
链接:https://blog.csdn.net/ZXY_ruchu/article/details/104578044