Главная | Обратная связь | Поможем написать вашу работу!
МегаЛекции

Scene Reference. Best Practices. Destroy. Animator. Scriptable Objects




Scene Reference

Source:

[Unity3D] A Reliable, user-friendly way to reference SceneAssets by script.

Reload Current Scene:

Legacy Input system: When Pressing the “R” Key:

if (Input. GetKeyDown(KeyCode. R)) {

   SceneManager. LoadScene(SceneManager. GetActiveScene(). name);

}

 

New Input System: When Pressing the “R” Key:

Using the new Unity Input System:
using UnityEngine. InputSystem;

Declare a “Keyboard” member field:
private Keyboard kb;

Get Device in Start:
private void Start() {

kb = InputSystem. GetDevice< Keyboard> ();

 

}

Check if the key was pressed (probably in Update):
if (kb. rKey. wasPressedThisFrame) {
SceneManager. LoadScene(SceneManager. GetActiveScene(). name);
}

■ “rKey” can be substituted for any other key

Load the next consecutive Scene:

buildIndex + 1: Simply get the current build index and add 1 to it:
SceneManager. LoadScene(SceneManager. GetActiveScene(). buildIndex + 1);

Load Arbitrary Scenes:

Supply the Scene to load either as an integer (build Index) or as a String

● As an int:
int levelToLoad = 1;
SceneManager. LoadScene(levelToLoad);

● As a String Reference:
SceneManager. LoadScene(“SceneName”);

Use an Enum with Scene Names: To reference levels by “name” without using a string reference, you can declare an enum with the names of each level and then call “ToString” on the Enum when passing it as the parameter for LoadScene:
public enum Scenes { MainMenu, Level1, Level2 }
SceneManager. LoadScene(Scenes. MainMenu. ToString());

Play from a specific Scene:

● Hit the Play button but load into a specific scene of your choice. Handy if you have the Level 3 Scene open, but want the game to start from the Title screen

○ Create “Editor” Folder under “Assets”

○ Create a C# Script named “SceneAutoLoader. cs” in “Editor”

○ Get the. cs Script from here:
http: //wiki. unity3d. com/index. php/SceneAutoLoader

 

Best Practices

Audio

Reference:

○ Introduction to AUDIO in Unity - YouTube

○ Simple Sound Manager (Unity Tutorial) - YouTube

○ Unity - Scripting API: AudioSource

Audio System in Unity:

○ An audio system needs at least one “Audio Listener” to function

Audio Listener Component usually exists on the Main Camera (since it follows the player)

○ Each source of Sound in the game can be played from an Audio Source Component

Audio Source Properties:

Play On Awake: The sound plays as soon as the game starts

Loop: Whether or not the audio will continue to play after it concludes

Scripting:

AudioClip: A class which holds reference to the actual audio file to play, like a Sprite for visual assets

PlayClipAtPoint: Instantiate an object which plays a sound at a position in World Space, then disposes of itself after the audio has finished playing; https: //docs. unity3d. com/ScriptReference/AudioSource. PlayClipAtPoint. html

PlayOneShot: Plays an instance of the audio clip which is unique, and therefore won’t stop another instance of itself (prevents audio from being cut off) but cannot be “Stopped” after initiated
https: //gamedev. stackexchange. com/questions/95274/play-vs-playoneshot

AudioPool: A system which handles playback of sounds in the game through the use of an Object pool

AudioManager:

■ Holds Volume levels which are applied to each instance of the sound

AudioPlayer:

■ Instantiated by AudioManager at runtime

■ Holds reference to a Unity AudioSource to actually play the sounds

■ Has reference to the SoundData it is playing

SoundData:

■ Scriptable Object which holds the audio clip and audio property data

Further Development;

Cull Limit; Over this number, inactive Audio sources will be culled

■ Audio Object Prefab;

● Create a prefab with all the settings you want, supply this on SoundData and it will use those parameters

■ Cross-Fade

■ Play with Random Pitch

■ Play one of x alternate sounds

● Add Alternate sounds onto the SoundData object and it will pull from them randomly

● With at least 3 sounds, guaranteed not to play the same sound twice in a row

■ Loop Random: Audio sequences set to loop with alternate sound clips

■ Play Sequence: Play all audio clips on a SoundData object sequentially

Destroy

● Supply a float to Destroy to destroy after a period of time:

Destroy on Create: Destroy(Instantiate(prefabName), 0. 05f);

Animator

● Animations: Don’t forget to set animations to Loop

Animator:

○ Don’t forget to disable “Has Exit Time” on things you want to transition between using Trigger Conditions

■ Also, set “Transition Duration” to 0

Blend Tree:

■ Basics of setting up a 2D Blend-Tree:
https: //youtu. be/whzomFgjT50? t=782

Scriptable Objects

Code Snippet:
[CreateAssetMenu(fileName = " DefaultFileName", menuName = " MenuListLabel/SubDirectory", order = 1)]
public class ClassNameData: ScriptableObject
{

}

DefaultFileName is the name new assets of this type will be given as they are created.

MenuListLabel is the label created on the right-click menu when creating a new asset.

SubDirectory allows you to store multiple Scriptable Objects in a single “folder” within the hierarchy of the Create Menu.

ClassNameData is whatever name the class has been given.

Data convention: To differentiate it from non-scriptable objects, you can include the term “Data” at the end of the name

Inherits from ScriptableObject: All ScriptableObjects must inherit from “ScriptableObject” in the class definition.

Holds data: A ScriptableObject

Hold other ScriptableObjects: ScriptableObjects can even hold references to other ScriptableObjects

Modify at runtime: ScriptableObjects can be modified while the game is running and WILL RETAIN ANY CHANGES MADE unlike other changes made while the game is running which revert when you exit play mode.

Поделиться:





Воспользуйтесь поиском по сайту:



©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...