This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
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
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

播放背景音乐

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.让我们创建的节点Music Player组件从游戏开始,毛圈的音乐。

  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.创建一个新的c#组件和MusicPlayer调用它。打开你的IDE和复制下面的代码。保存在IDE中代码以确保它的自动切换回UnigineEditor编译。

    源代码 (C#)
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unigine;
    
    #if UNIGINE_DOUBLE
    using Vec3 = Unigine.dvec3;
    using Mat4 = Unigine.dmat4;
    using Scalar = System.Double;
    #else
    	using Vec3 = Unigine.vec3;
    	using Mat4 = Unigine.mat4;
    	using Scalar = System.Single;
    #endif 
    [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
    public class PlayerController : Component
    {
    //========================== NEW - BEGIN ===============================
    	bool isNextLeft = false;
    
    	// mouse fire button 
    	public Input.MOUSE_BUTTON mouseFireKey = Input.MOUSE_BUTTON.RIGHT;
    
    	// asset file that contains the bullet
    	public AssetLinkNode bullet;
    
    	public Node leftSpawn, rightSpawn;
    //========================== NEW - END ===============================
    	Player player;
    
    	BodyRigid rigid;
    
    	Vec3 pos;
    
    	//a WorldIntersection object to store the information about the intersection
    	WorldIntersection intersection = new WorldIntersection();
    
    	void Init()
    	{
    		player = Game.Player; 
    
    		rigid = node.ObjectBodyRigid;
    		rigid.AngularScale = new vec3(0.0f, 0.0f, 0.0f); // restricting the rotation
    		rigid.LinearScale = new vec3(1.0f, 1.0f, 0.0f); // restricting Z movement
    		rigid.MaxLinearVelocity = 8.0f; // clamping the max linear velocity
    	}
    //========================== NEW - BEGIN ===============================
    	void Update() 
    	{
    		if (Input.IsMouseButtonDown(mouseFireKey) && bullet.IsFileExist)
    		{
    			// load the bullet and set its position 
    			if (isNextLeft)
    				bullet.Load(rightSpawn.WorldTransform);
    			else
    				bullet.Load(leftSpawn.WorldTransform);
    
    			// alternate between the left and the right gun
    			isNextLeft = !isNextLeft;
    		}
    
    		// press ESC button to close the game
    		if (Input.IsKeyDown(Input.KEY.ESC))
    		{
    			Engine.Quit();
    		}
    	}
    //========================== NEW - END ===============================
    	void UpdatePhysics() 
    	{
    		// forward
    		if (Input.IsKeyPressed(Input.KEY.W))
    			Move(player.GetWorldDirection(MathLib.AXIS.Y)); 
    
    		// backward
    		if (Input.IsKeyPressed(Input.KEY.S))
    			Move(player.GetWorldDirection(MathLib.AXIS.NY)); 
    		// left
    		if (Input.IsKeyPressed(Input.KEY.A))
    			Move(player.GetWorldDirection(MathLib.AXIS.NX)); 
    
    		// right
    		if (Input.IsKeyPressed(Input.KEY.D))
    			Move(player.GetWorldDirection(MathLib.AXIS.X)); 
    
    		// finding the positions of the cursor and the point moved 100 units away in the camera forward direction 
    		ivec2 mouse = Input.MousePosition;
    		Vec3 p0 = player.WorldPosition;
    		Vec3 p1 = p0 + new Vec3(player.GetDirectionFromMainWindow(mouse.x, mouse.y)) * 100; 
    
    		// casting a ray from p0 to p1 to find the first intersected object
    		Unigine.Object obj = World.GetIntersection(p0, p1, 1, intersection); // the first bit of the intersection mask is set to 1, the rest are 0s
    
    		// finding the intersection position, creating a transformation matrix to face this position and setting the transofrm matrix for the body preserving angular and linear velocities
    		if (obj)
    		{
    			pos = intersection.Point;
    			pos.z = rigid.Transform.Translate.z; // project the position vector to the Body Rigid pivot plane
    			Mat4 transform = MathLib.SetTo(rigid.Transform.Translate, pos, vec3.UP, MathLib.AXIS.Y);
    			rigid.SetPreserveTransform(transform);
    		}
    	}
    
    	// moving the rigid body with linear impulse in the specified direction
    	void Move(vec3 direction) 
    	{
    		//directon is a normalized camera axis vector 
    		rigid.AddLinearImpulse(direction);
    	}
    }
  2. Create a new Dummy Node, rename it to "music_player", and place it somewhere in the world.创建一个新的Dummy Node,重命名它为"music_player",它在世界上的地位。
  3. Add the MusicPlayer component to the music_player node.MusicPlayer组件添加到music_player节点。
  4. Assign the imported music asset (programming_quick_start/music/ost.mp3) to the Background Music field of the MusicPlayer component.指定导入的音乐资源(programming_quick_start/music/ost.mp3)的Background Music字段MusicPlayer组件。

  5. Save changes to the world, go to File->Save World or press Ctrl+S hotkey.拯救世界的变化,去File->Save World或者按Ctrl+S热键。
  6. Run the project in the UnigineEditor to check out the background music.运行项目UnigineEditor检查背景音乐。

最新更新: 2024-04-19
Build: ()