This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Playing Background Music

The game must output some audio besides the bullet hit sound effect. To play the background music we will use the component system once again.

Let's create the node with a Music Player component that plays the looped music from the game start.

  1. Create a new C# component and call it MusicPlayer. Open your IDE and copy the code below. Save your code in the IDE to ensure it's automatic compilation on switching back to UnigineEditor.

    MusicPlayer.cs
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unigine;
    
    [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
    public class MusicPlayer : Component
    {
    	public AssetLink backgroundMusic;
    	
    	AmbientSource music;
     
    	void Init()
    	{
    		// check if the backgroundMusic is set and the file asset exists
    		if (backgroundMusic.IsFileExist)
    		{
    			music = new AmbientSource(backgroundMusic.Path);
    			music.Loop = 1;
    			music.Gain = 0.5f;
    			// start playing the music on initialization
    			music.Play();
    		}
    
    	}
    
    	void Shutdown()
    	{
    		if (music.IsValidPtr)
    			music.DeleteLater(); 
    	}
    }
  2. Create a new Dummy Node, rename it to "music_player", and place it somewhere in the world.
  3. Add the MusicPlayer component to the music_player node.
  4. Assign the imported music asset (programming_quick_start/music/ost.mp3) to the Background Music field of the MusicPlayer component.

  5. Save changes to the world, go to File->Save World or press Ctrl+S hotkey.
  6. Run the project in the UnigineEditor to check out the background music.

Last update: 2022-12-14
Build: ()