Unity Interview Summary-Function Implementation

1. From receiving the task to completing the task, there are several processes in total. What should I pay attention to in each process?

  • Demand analysis: to ensure that the understanding and planning ideas are consistent.
  • Build ui or scene:
  • Write code:
  • Test function:
  • Fix the bug:

2. Design a backpack system with 500 grids. Pseudo-code can be added, deleted, modified and checked. struct sitem{ string uid, int index, int count, int itemcode}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PaiXu
{
    struct sitem {
       public string uid;     //唯一id
       public int index;      //位置
       public int itemCoad;   //物品id
       public int itemAmount; //物品数量
    }

    class Class2
    {
        public int bagCount = 500;

        //申请一个背包列表
        public List<sitem> bagList = new List<sitem>();


        //初始化单个背包格
        public void InitSimpBagItem(int index)
        {
            bagList[index] = new sitem();
        }
        //初始化整个背包
        public void InitBag() {
            for (int i = 0; i < bagCount; i++) {
                InitSimpBagItem(i);
            }
        }

        //检查格子是否为空
        public bool CheckNull(int index) {
            if (bagList[index].itemAmount == 0)
            {
                return true;
            }
            else {
                return false;
            }
        }
        //检查背包中是否含有这个物品(id),返回位置
        public int CheckItem(int id) {
            for (int i = 0; i < bagCount; i++) {
                if (bagList[i].itemCoad == id) {
                    return i;
                }
            }
            return -1;
        }
        //获取位置最小的空格子
        public int FindFull() {
            for (int i = 0; i < bagCount; i++) {
                if (bagList[i].itemAmount == 0) {
                    return i;
                }
            }
            return -1;  //满了
        }


        //增
        public void AddItem(sitem sitem) {
            //检查是否存在
            int bagIndex = CheckItem(sitem.itemCoad);
            if (bagIndex == -1)
            {
                int newBagIndex = FindFull();
                if (newBagIndex == -1)
                {
                    return;
                }
                else
                {
                    sitem.index = newBagIndex;
                    sitem.itemAmount = 1;
                    bagList[newBagIndex] = sitem;
                }
            }
            else {
                sitem.itemAmount = bagList[bagIndex].itemAmount + 1;
                bagList[bagIndex] = sitem;
            }
        }

        //删
        public void DelectItem(int id) {
            int bagIndex = CheckItem(id);
            if (bagIndex == -1)
            {
                return;
            }
            else {
                sitem item = bagList[bagIndex];
                item.itemAmount -= 1;
                if (item.itemAmount <= 0) {
                    InitSimpBagItem(bagIndex);
                }
                bagList[bagIndex] = item;
            }
        }
    }
}

3. Describe how the backpack is implemented?

Use the MVC design pattern:

modle and control layer

  • First define the size of the backpack
  • Then define the grid class, including the data of each grid, such as: grid seat, item id, quantity, item information
  • Create a backpack management class, apply for a list of grid classes, and initialize.
  • Contains some methods, such as initializing the grid, judging whether the grid is empty, obtaining the smallest space, adding, deleting, modifying, and so on.

view layer

  • Get the backpack information, generate the corresponding grid, and form the backpack.

4. How to display the model on the UI?

  • Use RenderTexture and rawIamge.
  • Create a camera that specifically illuminates the 3D model. In order to avoid affecting the main perspective, you can set the camera position farther.
  • Create a RenderTexture, assign it to the camera, and then assign it to Rawimag
  • Any objects under the camera will be recorded by the rendering texture and displayed in RawIamg.
  • Note: RenderTexture can set the resolution, preferably the same as the game. The Clear Flags of the camera is set to Solid Color.

5. How does Lua and C# realize communication?

Interactive process:

  • C# call Lua: Call the Lua parser underlying dll library from the C# file, and execute the corresponding Lua file by the dll file; C# -> dl l-> Lua
  • Lua call C#: First generate the Wrap file corresponding to the C# source file. The warp file registers the field method in the Lua interpreter, and then the C# file can be called through the Warp file. Lua ->Warp->C#

Interaction principle:

  • Mainly through the virtual stack.
  • C# call Lua: C# puts the data on the top of the stack, Lua goes to the top of the stack to get the data, performs processing in Lua, and then puts the result back to the top of the stack, and then C# takes the data from the top of the stack to complete the interaction.
  • Lua call C#: Barabara Barabara

6. What should I pay attention to when making ui? How to avoid

  • Canvas Canvas is the basic component of Unity UI. When one element or multiple elements change on the Canvas, the entire canvas will be polluted. Solution: separate dynamic and static elements.
  • Reduce unnecessary RayCast Target. The UGUI click event is also a loss of efficiency, so if it is not necessary, it is recommended to turn off the raycastTarget property of the control.
  • Close ricetext if not necessary.
  • Avoid using Camera.main

  • Hide the canvas and disable the Canvas component.

  • Only use Animator components on frequently dynamic elements. For elements that rarely change, you can use the DoTween plug-in to complete the animation.

7. How to achieve hurt floating words?

  • First, make a prefab of floating characters, with an appearance animation, and an automatic destruction function, which can transmit damage parameters.
  • Obtain the target position of the floating character to be displayed in world coordinates and convert it to screen coordinates. transfrom.position -> anchorPoint.
  • Create a parent container and instantiate the floating character prefab.

Guess you like

Origin blog.csdn.net/wang_lvril/article/details/109543567