十六、射線檢測(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.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);
}
}
3、通過實現接口
- Help->Unity Manual->Scripting->Event System->Supported Events
Paycast Target 勾選
測試代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
//interface
public class UIEventManager2 : MonoBehaviour//, IPointerDownHandler,IPointerClickHandler,IPointerUpHandler,IPointerEnterHandler,IPointerExitHandler
,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下載圖片)
測試代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WWWTest : MonoBehaviour {
public string url = "abc.jpg";
IEnumerator Start()
{
WWW www = new WWW(url);
yield return www;
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = www.texture;
}
}
十九、Camera組件
1、allCameras:返回場景中所有啓用的攝像機。
2、allCamerasCount:當前場景中的攝像機數量。
3、current:我們目前使用的相機渲染,只用於底層渲染控制(只讀)。
4、main:第一個啓用的標記爲“主攝像頭”的攝像頭(只讀)。
5、ScreenPointToRay:返回從攝像機通過屏幕點的光線。
測試代碼:
public class CameraTest : MonoBehaviour {
private Camera camera;
void Start () {
camera = Camera.main;
}
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
//Debug.DrawRay(ray.origin, ray.direction);
//Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
Debug.Log(hit.collider);
}
}
二十、角色控制器CharacterController
1、center:角色膠囊的中心相對於變換的位置。
2、height:人物膠囊的高度。
3、isGrounded:在最後的動作中,角色控制器是否觸地。
4、Move:一個更復雜的移動函數,採用絕對移動增量。
5、SimpleMove:以速度移動角色。(有重力影響)
6、OnControllerColliderHit:(事件)控制器執行移動時碰到碰撞器時調用的。
測試代碼:
public class PlayerCC : MonoBehaviour {
public float speed = 3;
private CharacterController cc;
void Start () {
cc = GetComponent<CharacterController>();
}
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
cc.SimpleMove(new Vector3(h, 0, v) * speed);
//cc.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);
Debug.Log(cc.isGrounded);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit.collider);
}
}