Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Adding and managing assets

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

This article will teach you Adding and managing assets in your Unity projects. We will walk you through optimizing the speed at which your Utility application is loaded using Unity 3D's outstanding game development functions. We will discover how to reduce the overall size of your build.

Before learning Adding and managing assets, we will first delve deeply into how Unity handles assets internally, covering topics such as:

  • What are the differences between Assets and Unity Objects?
  • How can I, as a developer, create my own Unity Objects?
  • How does Unity handle a resource lifecycle in memory during run-time?
  • What is the workflow of assets in Unity?

Creating our Own Unity Object

Unity provides two base Objects that allow us to create our own custom Objects that Unity will serialise using inheritance.

  • The first is called the MonoBehaviour. Within a Scene or Prefab, your MonoBehaviour Object can be serialised. They can be used as components on GameObjects.
     
  • Then there's the ScriptableObject. ScriptableObject, unlike MonoBehaviour, cannot be serialised within a Scene or Prefab asset. They are frequently employed as Data Storage types.

 

MonoScript refers to the script that your custom object is using. That is, your custom Objects (MonoBehaviour and ScriptableObjects) include a MonoScript reference. A MonoScript contains the information you need to find the script that defines the Object. The basic information required to locate a script includes the assembly name, namespace, and script name.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Serializing the Unity Objects

All of your Objects may contain references to one or more other Objects. This is referred to as Inter-Object References. Unity uses the file GUID and local ID to serialise your references, which is known as Reference Serialization.

It is a strong identification system that is designed to abstract the asset path location regardless of the platform on which our Project is built.

GUID is an abbreviation for Globally Unique Identifier. It's a 16-byte (128-bit) integer value that's guaranteed to be unique. The File GUID identifies a specific Asset file that you have saved on the disc. It is a reference to the Asset's precise location. This identifier is saved by Unity in the.meta file associated with the Asset.

Unity Assemblies

Assembly-CSharp.dll is created for each C# script in your project. With the exception of those in plugin folders that are compiled into Assembly-CSharp-firstpass.dll. The ability to define your own assemblies with assembly definition reference files was added in Unity 2017.3. During application startup, all of your assemblies are loaded.

This MonoScript Object is the reason why an AssetBundle (or a Scene or a prefab) does not contain executable code in any of the AssetBundle, Scene, or prefab's MonoBehaviour Components. This enables different MonoBehaviours to refer to specific shared classes, even if they are in different AssetBundles.

Importers of adding and managing assets

Asset types that are not native to Unity must be imported. This is accomplished through the use of an asset importer. While these importers are typically invoked automatically, the AssetImporter API exposes them to scripts. The TextureImporter API, for example, provides access to the settings used when importing individual texture Assets such as PNG files.

The import process produces one or more UnityEngine.Objects. Multiple sub-assets within the parent Asset, such as multiple sprites nested beneath a texture Asset that has been imported as a sprite atlas, are visible in the Unity Editor. Because their source data is stored in the same Asset file, each of these Objects will have the same File GUID. They will be identified by a symbol within the imported texture Asset by local ID.

Memory resource lifecycle

Understanding how Unity handles automatic Object loading and unloading is critical for managing an application's memory footprint at run-time.

When an Instance ID mapped to an Object is dereferenced and the referenced object is not already loaded, the Object is loaded automatically; of course, the Object data source must be located using the File GUID + Local ID pair.

Objects can be unloaded from memory in three different ways:

  • Asset cleanup is triggered automatically when a new Scene is loaded in a non-additive manner. Or when Resources are scarce. UnloadUnusedAssets is manually invoked within a C# script. Only unreferenced Unity Objects will be unloaded by this process.
     
  • If an Object was explicitly loaded from a Resources folder, it can be unloaded by invoking the Resources.UnloadAsset method.
     
  • Objects sourced from AssetBundles are unloaded automatically and immediately when the AssetBundle is invoked.

Workflow of Adding and managing assets

Any item we use in our Unity project to create our game or app is considered an asset. Assets in our project can represent visual or audio elements such as 3D models, textures, sprites, sound effects, or music. Assets can also mean more abstract items such as color gradients, animation masks, or arbitrary text or numeric data for use in any application.

An asset could be a file created outside of Unity, such as a 3D Model, audio file, or image. In the Unity Editor, we can also create assets such as a ProBuilder Mesh, an Animator Controller, a Render Texture, or an Audio Mixer.

Import

Importing means bringing source files into the Unity Editor to work with. When we save or copy a file into the Assets folder of our project, Unity imports it and allows us to work with it in the Editor.

It's critical to understand the fundamentals of asset importation into Unity, like where the files are stored in the project, how to adjust the Import Settings for each type of Asset, what metafiles are for, and how the Asset Database stores imported data. For more information on these topics, see Importing Assets.

When working with teams, we can use Unity Accelerator to accelerate asset processing.

Create

After we've imported some assets into our project, we can begin working on our game or app. This typically entails inserting assets as GameObjects into one or more Scenes and adding scripts to control how the user interacts with them.

We may need to divide our assets into separate groups, allowing our game to download selected extra content incrementally at runtime.

During the creation process, we can decide how to divide our assets into separate bundles and implement the code.

Build

Build refers to exporting our completed project to binary files, which we can then distribute and run on our preferred platform. For example, when we build for Windows, Unity generates an.EXE file and some data files that we can then distribute.

If we're using Addressable or Asset Bundles to organize our assets into downloadable bundles, we'll need to create those bundle files for distribution.

we can build our project on our computer or use Unity's Cloud Build service, which provides automated build generation and continuous integration for Unity projects.

Distribute

After we've created our game or app and its content bundles, our users will need a way to access them.

Unity's Cloud Content Delivery service is fully integrated into the Unity development platform, it allows us to host and deliver our game or app and its content to our users. This can save time and is especially useful for content-rich, live games or applications that require regular content updates.

Load

The loading process and experience of our game or app are defined by the rules and programming we set up and the way we grouped and bundled our assets.

we can provide fast initial downloads, ongoing updates, and extra content that we can roll out over the life of our project by combining the techniques and services described here.

Here is the visual representation of the same.

Asset workflow in Unity

Source

Frequently Asked Questions

What exactly is a Unity asset bundle?

AssetBundle is an archive file that contains non-code Assets (such as Prefabs, Textures, Models, and entire Scenes), which Unity can load at run time. Dependencies can be expressed using AssetBundles.

What are streaming assets in Unity?

The StreamingAssets folder allows users to easily save, load, and work with their own files in your Unity Project.

Can you add sound effects to the game in Unity?

Yes, Audio is an interesting topic to cover here. Two components are dealt with when adding audio in any project of Unity. These components are Audio Listener and Audio Source.

How do I use assets in the Unity store?

Select Window > Asset Store from the main menu to open the Asset Store window. On our first visit, we will be asked to create a free user account, which we will use to access the Store.

Conclusion

In this article, we have extensively discussed adding and managing assets. We started with creating our own unity engine object, serializing the unity object, unity assembly, asset importer, and memory resource lifecycle, and finally concluded with the workflow of assets.

We hope that this blog has helped us enhance our knowledge regarding adding and managing assets and if we would like to learn more, check out our article on Unity.

For peeps out there who want to learn more about Data Structures, Algorithms, Power programming, JavaScript, or any other upskilling, please refer to guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests and solve problems available and interview puzzles. Also, we can put our attention towards interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help other ninjas grow.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Topics covered
1.
Introduction
2.
Creating our Own Unity Object
3.
Serializing the Unity Objects
4.
Unity Assemblies
5.
Importers of adding and managing assets
6.
Memory resource lifecycle
7.
Workflow of Adding and managing assets
7.1.
Import
7.2.
Create
7.3.
Build
7.4.
Distribute
7.5.
Load
8.
Frequently Asked Questions
8.1.
What exactly is a Unity asset bundle?
8.2.
What are streaming assets in Unity?
8.3.
Can you add sound effects to the game in Unity?
8.4.
How do I use assets in the Unity store?
9.
Conclusion