Unity ECS and URP Experiments

Unity ECS and URP Experiments

I have been experimenting with The ECS (entity - component - system) for a while on different versions of Unity Releases to see how much I could utilize them.

It was a slow start as the documentations on the Topic is scarce compared to the regular documentation, you can find some though and it is documented on Unity's website, ECS is constantly changing so adapting to the updates is challenging at times, while I was testing on different versions.

ECS was documented to be great for performance and I wanted to see that what you could possibly do with ECS and get a better understanding of the Jobs System and the Brust Compiler. The new system makes it easier to write multi-threaded code, which is safer, comparing to writing multi-threaded code without utilizing the Jobs system.

The first thing people will run into is the different style of syntax compared to the Unity API, and the same is true when writing multi-threaded code, and you have to keep in mind things like:

  • Reading and Writing values order.
  • What you can write outside the main thread like instantiating a gameObject you cannot.
  • Using Native Array's and disposing of them. etc.
  • Making sure it's actually running on your worker threads and not on the main thread.

so doing some reading and experimenting with them would help while learning ECS.

I also wanted to use ECS with the URP (universal render pipeline) it isn't a big hassle if you know how the URP works.


Note......

If you want your Lit materials to work you will need to add ENABLE_HYBRID_RENDERER_V2  in Projects settings -> Player -> Other Settings -> Scripting Define Symbols, as mentioned in the documentation (https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.11/manual/index.html)

In the newer versions of Unity, you are not able to find the Entities and HybridRender in your package manager even if you enable preview mode you will need to download them manually with a link added to your gitUrl under the + button.

  • com.unity.entities
  • com.unity.rendering.hybrid
No alt text provided for this image

which you can find online but here are two links to save you time :





Version 2020.2.3f1 the compiled application would not run either on mobile or Windows builds when the scripting backend is set as IL2CPP even with Low Managed stripping Level option.

Update --------

(If you have the Hybrid Renderer package downloaded) then it will not run.

so if you want to work without the entities you can still use Job systems.

--------------------

I do recall it working with IL2CPP correctly on previous versions but that was a while ago and could be incorrect information so take it with a pinch of salt. I assume it's stripping the code of the burst compiler.

The Disabled option is only available with Mono scripting backend enabled, and that was tested with both standard.NET 2.0 and .NET 4.x options.

Now troubleshooting out the way ill list what I wanted to accomplish while using the URP:

  • ECS Compatibility with Mobile Development.
  • ECS Compatibility with VR Development.
  • ECS Development.
No alt text provided for this image

Creating an entity from scratch has problems as it will not render, even if a RenderBounds has been added it will look black at times.

It is great that you don't have to create your entity from scratch but I did it to learn the ECS a little bit better and how it works under the hood, to begin with.

1. Mobile

With the Mono scripting backend, you are able to run it on Mobile, but obviously, it did not work as efficiently as running on a PC build. this has been tested on one device. so results will vary on a high-end device, and on slightly better and newer devices, but for production, you need to keep the average consumer in mind. but it will be less efficient on mobile in general.

2. VR Standalone

The same Problems with the mobile occurred with the scripting backend, in addition, the hybrid render seemed not to work in VR and would not render the Entity it could be different if I did not use URP Materials but this will be tested without URP in the future.

Tested with OpenGLES3 graphics API. that could have been a factor that should have been tested thinking about it now, I shall try to see if other settings affect the outcome of this.

Update --------

At some point, I did come across The hybrid render not working on Adreno GPU's as the Oculus Quest2 runs on a Qualcomm® Adreno™ 540 GPU it did not run and would crash.

I will try to update this in the future.

And the reason for the crash seems to be the Burst compiler is you can see below. this is with IL2CPP backend

No alt text provided for this image

I downgraded the burst packages and the result was the same.

The Brust is a dependency for the jobs to work, so I could not test it without the burst compiler.

I have tried Using Mono scripting backend but you can only have ARMv4 target architecture, but if you are planning to release to the store, not that 32-bit is not supported on the Oculus store.

No alt text provided for this image

Here is a link about burst crashing from unity Link

Even when changing it to release it did not work.

--------------------


3. VR using PC

It works as no issue rendering Entities.

No alt text provided for this image

--------------------------------------

You are able to use Entities and MonoBehavior interactions without Unity.Physics (which you need to download manually as well) that is used with ECS by using Transforms position rather than Trigger or collisions and execute it within a job, by feeding the players position, etc, that is one application.

As you can get Inputs from the main thread and run computations on multithreaded jobs is another application, to do high computations with the jobs system. ECS is fantastic for Crowds and a Large number of Objects to move or any large number of computations.

Creating a system using ComponentSystem, JobComponentSystem, SystemBase each have their difference, their implementations vary, and their impact reflects in the profiler, as well as having lambda expressions, made a difference but that could have been an isolated occurrence, that was not tested further.


Some of the Classes and methods will be deprecated in the near further in reading the documentation.


In the videos to come as in the last video, each entity has a random rotation, rotation speed, and material color assigned to them, and the movement is handled as a job as well.

I did have post-processing options enabled and cranked up a bit, bloom and motion blur which are intensive, I did not disable them when building. Also while the application was running I had other applications running so the frame rate did suffer even further, I understand this is not the ideal conditions to conduct testings on but the condition will be better in future tests.

The number of entities created will be displayed on the top right as well as how many entities are selected and the FPS. the camera is a perspective camera and with simple movement and camera zooming script, on selection,

  • Colour is changed
  • Emissive is set to 1.

The frame rate could be improved as I have not done much if any optimization to the scene which is standard.

In the video above 40,000 cubes with random rotation and colour (color :P) in this one, I also create a mesh at runtime on selection.

(Above) 360,000 Cubes Generated

(Above) 490,000 Cube Generated

I'm sure with better optimization it's possible to get even better performance.

To view or add a comment, sign in

More articles by Mosab Barnawi

Others also viewed

Explore content categories