unity常用API(四)

个人英语不好,所以看的是2018.1 的中文API 部分代码和解释都来源于此文档:原文链接

视频链接:点击链接

Physics

  1. Raycast : 向场景中的所有碰撞体投射一条射线,该射线起点为 /origin/,朝向 /direction/,长度为 /maxDistance/。
         // 接受一个射线
         Ray ray = new Ray(transform.position+transform.forward,transform.forward);
         bool isCollider = Physics.Raycast(ray);
         Debug.Log(isCollider);
         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);
             //Debug.Log(hit.point);  // 获取捕获的点
             //Debug.Log(hit.collider);   // 碰撞的游戏物体
             //
             // 碰撞 layer 为xx的层 
             // bool isCollider = Physics.Raycast(ray, Mathf.Infinity,LayerMask.GetMask("xx")); 
         }
         //2D
         Physics2D.Raycast();

UGUI 事件监听

```c#
    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);
    }
```

```c#
    // 实现接口 UI,EventSystem
     //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");
        }
    }

```

其他

  1. Camera
    ScreenPointToRay : 返回从摄像机通过屏幕点的光线。
     private Camera camera;
     // Use this for initialization
     void Start () {
         camera = Camera.main;
     }
     // Update is called once per frame
     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);
     }
  2. CharacterController : 可使您轻松进行受碰撞约束的移动,同时不必处理刚体。
         public float speed = 3;
         private CharacterController cc;
         // Use this for initialization
         void Start () {
             cc = GetComponent<CharacterController>(); 
         }
         // Update is called once per frame
         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);
         }
  3. Mesh : 材质
         public Mesh mesh;
         private Material mat;
         // Use this for initialization
         void Start () {
             //GetComponent<MeshFilter>().sharedMesh = mesh;
             //Debug.Log(GetComponent<MeshFilter>().mesh == mesh);
             mat = GetComponent<MeshRenderer>().material;      // 获取材质       
         }        
         // Update is called once per frame
         void Update () {
             mat.color = Color.Lerp(mat.color, Color.red, Time.deltaTime); // 更新颜色
         }