基於Unity3D實現3D照片墻效果

一、前言

Unity3D不僅僅可以開發遊戲,還有非常多的開發方向,秉承著興趣為先,將可以使用Unity制作的各種應用案例,分享如何進行開發,如何實現,希望大傢可以在感興趣的地方,學習到自己想要學習的東西。

今天就來實現一個3D照片墻的效果。

我對於這個項目的構思就是照片是3D的,可以滑動,然後使用DoTween插件去做動畫平滑移動。

OK,那就正式開始。

先看一下效果圖:

二、正式開發

新建項目,我使用的版本是Unity3D 2019.4.7f1,模板選用3D:

(1)導入DoTween插件。

(2)新建Canvas,設置參數:

設置Canvas的坐標到0,0,0:

(3)在Canvas新建N個Image:

(4)設置Main Camera的屬性:

設置完的樣子:

反正就是讓Image在屏幕中間就可以瞭。

(5)新建腳本UIRotate.cs,雙擊打開腳本,編輯代碼:

using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIRotate : MonoBehaviour
{
    private int halfSize;
    private GameObject[] gameObjects;
    /// <summary>
    /// 圓半徑
    /// </summary>
    private int r = 300;
    /// <summary>
    /// 相間角度
    /// </summary>
    private int angle;

    private void Start()
    {
        //初始化數組
        var childCount = transform.childCount;
        //計算出中點
        halfSize = (childCount - 1) / 2;
        //求出圓內角度
        angle = 360 / childCount;
        //初始
        gameObjects = new GameObject[childCount];
        for (var i = 0; i < childCount; i++)
        {
            gameObjects[i] = transform.GetChild(i).gameObject;
            SetPosition(i);
            SetDeepin(i);
        }
    }

    /// <summary>
    /// 設置物體位置
    /// </summary>
    private void SetPosition(int index)
    {
        float x = 0;
        float z = 0;
        if (index < halfSize)
        {
            int id = halfSize - index;
            x = r * Mathf.Sin(angle * id);
            z = -r * Mathf.Cos(angle * id);
        }
        else if (index > halfSize)
        {
            int id = index - halfSize;
            x = -r * Mathf.Sin(angle * id);
            z = -r * Mathf.Cos(angle * id);
        }
        else
        {
            x = 0;
            z = -r;
        }
        Tweener tweener = gameObjects[index].GetComponent<RectTransform>().DOLocalMove(new Vector3(x, 0, z), 1);
    }

    private void SetDeepin(int index)
    {
        //計算圖片深度也就是z軸的距離,離攝像機的遠近
        int deepin = 0;
        if (index < halfSize)
        {
            deepin = index;
        }
        else if (index > halfSize)
        {
            deepin = gameObjects.Length - (1 + index);
        }
        else
        {
            deepin = halfSize;
        }
        gameObjects[index].GetComponent<RectTransform>().SetSiblingIndex(deepin);
    }
}

(6)將UIRotate.cs腳本附到Canvas對象上,運行程序:

(7)添加左右拖動代碼,繼續修改UIRotate.cs代碼:

using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIRotate : MonoBehaviour
{
    private int halfSize;
    private GameObject[] gameObjects;
    /// <summary>
    /// 圓半徑
    /// </summary>
    private int r = 300;
    /// <summary>
    /// 相間角度
    /// </summary>
    private int angle;

    private void Start()
    {
        //初始化數組
        var childCount = transform.childCount;
        //計算出中點
        halfSize = (childCount - 1) / 2;
        //求出圓內角度
        angle = 360 / childCount;
        //初始
        gameObjects = new GameObject[childCount];
        for (var i = 0; i < childCount; i++)
        {
            gameObjects[i] = transform.GetChild(i).gameObject;
            SetPosition(i);
            SetDeepin(i);
        }
    }

    /// <summary>
    /// 設置物體位置
    /// </summary>
    private void SetPosition(int index)
    {
        float x = 0;
        float z = 0;
        if (index < halfSize)
        {
            int id = halfSize - index;
            x = r * Mathf.Sin(angle * id);
            z = -r * Mathf.Cos(angle * id);
        }
        else if (index > halfSize)
        {
            int id = index - halfSize;
            x = -r * Mathf.Sin(angle * id);
            z = -r * Mathf.Cos(angle * id);
        }
        else
        {
            x = 0;
            z = -r;
        }
        Tweener tweener = gameObjects[index].GetComponent<RectTransform>().DOLocalMove(new Vector3(x, 0, z), 1);
    }

    private void SetDeepin(int index)
    {
        //計算圖片深度也就是z軸的距離,離攝像機的遠近
        int deepin = 0;
        if (index < halfSize)
        {
            deepin = index;
        }
        else if (index > halfSize)
        {
            deepin = gameObjects.Length - (1 + index);
        }
        else
        {
            deepin = halfSize;
        }
        gameObjects[index].GetComponent<RectTransform>().SetSiblingIndex(deepin);
    }

    /// <summary>
    /// 向左滑動
    /// </summary>
    public void OnLeftDrag()
    {
        var length = gameObjects.Length;
        for (var i = 0; i < length; i++)
        {
            var temp = gameObjects[i];
            gameObjects[i] = gameObjects[length - 1];
            gameObjects[length - 1] = temp;
        }
        for (var i = 0; i < length; i++)
        {
            SetPosition(i);
            SetDeepin(i);
        }
    }

    /// <summary>
    /// 向右滑動
    /// </summary>
    public void OnRightDrag()
    {
        var length = gameObjects.Length;
        for (var i = 0; i < length-1; i++)
        {
            var temp = gameObjects[i];
            gameObjects[i] = gameObjects[i+1];
            gameObjects[i+1] = temp;
        }
        for (var i = 0; i < length; i++)
        {
            SetPosition(i);
            SetDeepin(i);
        }
    }

    private Vector2 touchFirst = Vector2.zero;//手指開始按下的位置
    private Vector2 touchSecond = Vector2.zero;//手指拖動的位置

    void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            touchFirst = Event.current.mousePosition;//記錄開始按下的位置
        }
        if (Event.current.type == EventType.MouseUp)
        {
            touchSecond = Event.current.mousePosition;//記錄拖動的位置
            if (touchSecond.x < touchFirst.x)
            {
                OnLeftDrag();//向左滑動
            }

            if (touchSecond.x > touchFirst.x)
            {
                OnRightDrag();//向右滑動
            }
            touchFirst = touchSecond;
        }
    }
}

運行程序:

(8)導入照片最後試一下:

三、總結

在Unity3D中實現瞭3D照片墻,雖然效果出來瞭,但是想要實現更加美觀的效果還需要一些努力。

到此這篇關於基於Unity3D實現3D照片墻效果的文章就介紹到這瞭,更多相關Unity3D照片墻內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: