robertDinca2003 / UnityTool2.0

UnityTool2.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UnityTool2.0

UnityTool2.0 is a console application that can receive 2 parameters, a unity project folder, and an output folder. The tool will return the hierarchy for every scene and all the unused scripts in the output folder.

I recommend reading at least the part about the problems with the task and backward compatibility.

I have completed the main task and the second bonus task(about the parallelizing).

Resources I have read and helped in the development:

The algorithm is described as simple and efficient as possible:

  • Step 1: Verify the input

    • The application will not start if the input has more or less than 2 parameters.
    • The application will not start if the unity project folder does not contain an assets folder as a direct subfolder.
    • The application will not start if the directories do not exist.
    • The user will be notified about any of the above
  • Step 2: Scraping the unity project and collect all scripts and scenes

  • Step 3: Parse all the scenes and create a dictionary with the keys being the class id and the values of the actual properties I need.

    • GameObject
    • Transform
    • MonoBehaviour
    • SceneRoots
  • Step 4: Creating the hierarchy

    • Step 4.1: For each scene, I have to access its scene root which consists of IDs of the Transform class.
    • Step 4.2: With the help of the property m_gameobject, is possible to access by id a certain GameObject and get its name.
    • Step 4.3: For every child of the current Transform, step 4 will be applied, creating in that way a DFS.
  • Step 5: Finding the unused scripts:

    • Step 5.1: Generate a hash set with all guid of the used scripts which can be found in the MonoBehaviour objects generated in step 3
    • Step 5.2: For every script collected in step 2, I will extract its guid from the metafile, and verify if it is in the set
    • Step 5.3: If the script is not found in the set, that means it is unused

    Problems with the task and tool

    I observed that in the version of Unity 2021 and below the SceneRoots attribute from the scene yalm file does not exist. That means there is a compatibility issue. I discovered that by applying the tool to an old project. The unity documentation contains such example with a scene yalm example which has been carried for years and not updated: https://docs.unity3d.com/2021.3/Documentation/Manual/YAMLSceneExample.html

    Also in old unity projects like the following one, the SceneRoot attribute is missing:

    https://github.com/nvjob/Infinity-Square-Space/blob/master/Assets/OSG%20Infinity%20Square%20Space/Scenes/main.unity

    The optimization added by parallelizing

    The time shown I know is not the real-time that the application spends on the CPU, but I believe is an interesting statistic.

    I have also built a version without any parallelized process which is around 2 times slower than the current version. I used a stopwatch to count the differences in both scripts.

    The optimized version scoring 10 milliseconds Screenshot_20231207_175601

    The unoptimized version scoring between 19-20 milliseconds Screenshot_20231207_180220

About

UnityTool2.0


Languages

Language:C# 100.0%