1. 程式人生 > >unity通過改變相機視野實現UI元素放大縮小的效果

unity通過改變相機視野實現UI元素放大縮小的效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; public class MoveAndScroll : MonoBehaviour , IScrollHandler{
    public Camera mainCamera;
    private float view;     public void OnScroll(PointerEventData eventData)
    {
        view = view + eventData.scrollDelta.y * 3f;//當滑鼠中輪向上滾動時eventData.scrollDelta.y = 1.0 向下為-1;
        mainCamera.fieldOfView = Mathf.Clamp(view, 1f, 179f);//因為相機的視野只能為1-179;所以用Mathf.Clamp,將值限制一下
        t.text = eventData.scrollDelta.ToString();
    }
}
上面的程式碼綁在了一張UI圖片上,要點是將該照片設為FirstSelected,好提供一個事件的焦點