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

Render Textures. Prototyping. Optimization. Scripting. UI Best Practices and Optimization. File IO. PlayerPrefs. ConstrainedPrefs




Render Textures

Reference: Unity Mini Map with Render Texture - YouTube

Create the Render Texture: Under Assets, Right-Click -> Create -> Render Texture

Create a Camera:

○ Aim the Camera at whatever it should see

Culling Mask: Untick the “UI” Layer (and any other Layers this camera should not see)

Target Texture: Supply the created Render Texture

Apply the Render Texture to a surface (like a Quad in view of the Camera)

Set Layer: To “UI”

Remove RigidBody

Mesh Renderer -> Cast Shadows: Off

○ Drag the Render Texture onto the Material of the Object

Prototyping

Unite LA

- Renaud Forestie

Mixamo

Google Poly

TurboSquid

SketchFab

iTween Plugin

Optimization

Videos:

○ Unite Europe 2017 - Squeezing Unity: Tips for raising performance

○ Optimization tips for maximum performance – Part 1 | Unite Now 2020

○ Optimization tips for maximum performance - Part 2 | Unite Now 2020

Scripting

Data Structures: Iterating through a List is cheap, Adding or Removing members is expensive, and vice-versa for a Dictionary

Scriptable Objects for Static Data: For static parameters, like an Enemy’s “VisionRange, MoveSpeed” etc. Use a Scriptable Object which the Instance references; otherwise every Instance of the Enemy will allocate memory for these static parameters

Remove Empty Unity Messages: Get rid of those automatically added “Start” and “Update” methods added to each script

Hash Animator Strings: Don’t use string references; create Integer hashes of them and use those instead

Reduce Hierarchy Complexity: When possible; try to make hierarchies as shallow as possible

Disable Accelerometer Frequency: If you aren’t using the device’s accelerometer, set the Polling frequency to “Disabled” under the Player Settings for Mobile

Move Rigidbodies the right way:

○ Use FixedUpdate

○ Use rb. MovePosition(); Don’t modify the transform position of a Rigidbody

Always Cache GetComponent: If possible, and if the Component will be referenced for more than one call

AVOID:

Putting Assets in the Resources folder: The more assets there are in here, the larger a “header” look-up table will be generated that will slow down your game’s initial load time

Heavy Logic in Start / Awake: Lots of logic executed in the first frame can result in quite a lag spike; better to try and make these occur one frame later if possible

AddComponent Method: The more Components you add in a row, the worse it gets

GameObject. Find: It needs to iterate through EVERY gameObject in the Scene

GetComponent InChildren / Parent: These are very heavy calls

UI Best Practices and Optimization

Videos:

○ Unite '17 Seoul - Tips and Tricks for Optimising Unity UI

● Create sub-canvases on the primary canvas to reduce the number of re-builds

○ Keep elements that will not change on a single canvas

○ Separate other elements that change more often onto their own canvas (or canvases)

○ Remember to add a graphic ray caster to each sub-canvas if it is supposed to accept input

● Load all UI elements in on Start, then Enable / Disable objects rather than create / destroy

● Pooling; For scrolling lists, reuse elements that have scrolled off screen

● Have all Sprite Elements on a single atlas to improve batching

● Have RectTransforms all on the same Z depth to avoid batch breaking

● Have all child elements cropped by the same Mask

● Only mask dynamic elements; pre-cut small elements to avoid batch breaking

● Shaders; For elements which don’t need masking, use an optimized shader

● Text; Use TextMeshPro for all Text assets

● Create your own Layout scripts when possible; rather than rely on Unity’s Layout groups

● Pixel Perfect; Don’t use it, or at the very least, only use it on elements that don’t move

○ Create sub-canvases that don’t inherit it with Canvas and Graphic Raycaster Components

● Turn Off Graphic Raycast Target wherever possible

Full-Screen UI: If a menu takes up the whole screen, disable the Camera that is viewing the Scene behind it

Disable Off-Screen Elements: If a UI Element shifts off-screen, disable it (it will still be rendered by the camera! )

File IO

PlayerPrefs

Scripting API: PlayerPrefs

ConstrainedPrefs

● This class ensures that values are clamped within an expected range when loading from PlayerPrefs, as it is possible to tamper with these files in a text editor, and this could result in unexpected or undesirable behaviour when the game runs

[Serializable]

public class ConstrainedFloatPref

{

  public string key;

  public float defaultValue;

  public float minValue;

  public float maxValue;

  [HideInInspector] public float value;

 

  public float Read() {

  //Load in values or their defaults, clamp each value to prevent external tampering from causing problems

value = Mathf. Clamp(PlayerPrefs. GetFloat(key, defaultValue), minValue, maxValue);

return value;

  }

 

  public void Write(float _value) {

//Save a value, but Clamp it before even writing to the file

value = Mathf. Clamp(_value, minValue, maxValue);

PlayerPrefs. SetFloat(key, value);

  }

}

 

[Serializable]

public class ConstrainedIntPref

{

  public string key;

  public int defaultValue;

  public int minValue;

  public int maxValue;

  [HideInInspector] public int value;

 

  public int Read() {

  //Load in values or their defaults, clamp each value to prevent external tampering from causing problems

value = Mathf. Clamp(PlayerPrefs. GetInt(key, defaultValue), minValue, maxValue);

return value;

  }

 

  public void Write(int _value) {

//Save a value, but Clamp it before even writing to the file

value = Mathf. Clamp(_value, minValue, maxValue);

PlayerPrefs. SetInt(key, value);

  }

}

Поделиться:





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



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