Unity3d large interactive photo wall

1. This time, in response to customer needs, a large-scale photo wall interaction was made, with an output resolution of 9600*4320 (Note: the maximum resolution officially recommended by unity3d is 8192*3686.4). After a 24-hour on-site violent test, no problems occurred in the middle. Let's make a tentative judgment that it can reach the normal standard, let's not talk nonsense, let's focus on the effect first.

Unity3d large photo wall_哔哩哔哩_bilibili

Here are a few of the techniques used

Standby video scroll bar problem

A part of the masking bar is generated on the original photo layer, and the scrolling problem of the masking layer is realized by controlling the change time through DoTween

 The problem of duplication of adjacent photos above and below

Because the on-site photo wall is relatively huge, although the photos of each location are obtained from the photo library, it is inevitable that the upper and lower photos will be duplicated, so you need to write a specific algorithm to avoid the duplication of the upper and lower photos. The code is as follows:

//numeber数量,minNum最小范围,maxNum最大范围
    public int[] GetNum(int number, int minNum, int maxNum)
    {
        List<int> RandomNum = new List<int>();
        int temp;
        int[] b = new int[number];
        System.Random r = new System.Random();
        for (int i = 0; i < number; i++)
        {
            temp = r.Next(minNum, maxNum);
            if (!RandomNum.Contains(temp))
            {
                RandomNum.Add(temp);
                b[i] = temp;
            }
            else
            {
                i--;
            }
        }
        for (int j = 0; j < b.Length; j++)
        {
            NewRandomNum[j] = b[j];
        }
        return b;
    }

Adjacent photos zoom in and zoom out

Because each time a photo is enlarged, the corresponding surrounding photos must be reduced. In order to make the range of photos affected by each interaction as small as possible, the number of people who can be played can be increased, and the middle position of the enlargement is used to reduce the corresponding adjacent two rows on the left and right, one for five rows. Unit avoids affecting other rows

Photos slide up and down infinitely

The Scroll View that comes with unity3d cannot achieve unlimited sliding on the line. To achieve this function, we need to rewrite the Scroll View component. The code is as follows, and the space is limited to show some code. Blogs and github have been rewritten by many masters. Everyone has needs You can go to find:

 Squeeze and move photos left and right

Import the dotween plug-in to realize photo zoom in and zoom out, as well as the easing effect of moving left and right

 Play the video after zooming in on the photo

Due to the wide range of on-site interactions, the number of videos to be played is uncertain, and it may be possible to play 20+ videos at the same time. The videoplayer that comes with unity3d is difficult to play. The video player plug-in AVPro is introduced. You can use it on the unity store. Search, import and use, and AVPro plays video, which is also convenient for you to replace externally

Like the video

In order to simplify the operation, the number of likes is stored in excel (it is convenient for Party A to set the number of likes for a certain leader, so it is recommended to install it externally and can be modified at any time), mainly to realize the reading and writing of excel, first read the person's obtained The number of likes, and then displayed, if there is a user likes, a red heart is displayed, and the number of likes +1

Read excel and display the code as follows:

public void ReadExcel(int ID)
        {
        //1.打开文件,创建一个文件流操作对象
        FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/DZ/" + "/star.xlsx", FileMode.Open, FileAccess.Read);
        //2.创建一个excel读取类
        IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);

        //方法2:读取
        DataSet result = reader.AsDataSet();

        star = int.Parse(result.Tables[0].Rows[ID][1].ToString());
        mtext.text = star.ToString();
        reader.Close();
    }

 Write excel and display the code as follows

public void WriteExce(int wID)
    {
        //string filePath = Application.dataPath + "/StreamingAssets" + "/" + "star.xlsx";
        string filePath = Application.streamingAssetsPath + "/DZ/" + "/star.xlsx";
        //string filePath = Application.dataPath + "/Excel/" + "star.xlsx";
        FileInfo file = new FileInfo(filePath);
        if (!file.Exists)
        {
            Debug.LogError("not excel file!!!");
        }
        using (ExcelPackage package = new ExcelPackage(file))
        {
            star += 1;
            ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
            worksheet.Cells[wID + 1, 2].Value = star;
            package.Save();
        }
    }

 Note: Like the character video, my implementation method read-"write-"read, if there is any simplified method, please leave a comment below to discuss

Close the video after playing

Because the video component is generated in real time to play the video through the AVPro official case prompt, so I didn’t think of a way to detect whether the video is played and then close it. Finally, under the prompt of my colleague, I added a script to the generated MediaPlayer to detect whether the video has been played, which is a perfect solution. Automatically close the video after playing

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RenderHeads.Media.AVProVideo;

public class ControlPlayVideo : MonoBehaviour
{
    private MediaPlayer mediaPlayer;
    // Start is called before the first frame update
    void Start()
    {
        mediaPlayer = GetComponent<MediaPlayer>();
        mediaPlayer.Events.AddListener(OnVideoEvent);
    }

    public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
    {
        switch (et)
        {
            case MediaPlayerEvent.EventType.ReadyToPlay:
                break;
            case MediaPlayerEvent.EventType.Started:
                break;
            case MediaPlayerEvent.EventType.FirstFrameReady:
                break;
            case MediaPlayerEvent.EventType.FinishedPlaying:
                //Debug.Log(gameObject.name + "播放完毕");
                mediaPlayer.Control.Stop();
                if (gameObject.transform.parent.parent!=null)
                {
                    Destroy(gameObject.transform.parent.parent.gameObject, 0.1f);
                    ControlVolume.Instance.RemoveList();
                }  
                break;
        }
    }

This is the summary of this vertical photo. If you have any comments or suggestions, you can leave a message below, and we will learn and communicate together.

Guess you like

Origin blog.csdn.net/m0_38116456/article/details/122667148