Summary of getting started with Unity (1)

Important methods of the Unity script life cycle:

Awake

Start

FixedUpdate

Update

LateUpdate

OnGUI

OnDisable

OnDestroy

Get the local time and introduce the System namespace

DateTime NowTime = DateTime.Now.ToLocalTime();

DateTime NowTime = DateTime.Now;

Conversion between Unity quaternion and Euler angle

1. Convert quaternion to Euler angle

Vector3 v3=transform.rotation.eulerAngles;

2. Convert the quaternion into a direction vector

Vector3 vector3= (transform.rotation * Vector3.forward).normalized;

3. Convert Euler angles to quaternions

Quaternion rotation = Quaternion.Euler(vector3);

4. Convert Euler angles to direction vectors

Vector3 v3 = (Quaternion.Euler(vector3) * Vector3.forward).normalized;

5. Convert the direction vector to a quaternion

Quaternion rotation =Quaternion.LookRotation(vector3);

6. Convert direction vectors to Euler angles

Vector3 v3 =Quaternion.LookRotation(vector3).eulerAngles;

Conversion between Unity screen coordinates and world coordinates

1. World coordinates to screen coordinates:

Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);

2. Convert screen coordinates to world coordinates:

Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);

modify skybox

RenderSettings.skybox = Skymaterial;

Add Skybox component to MainCamera

Find the angle and normal vector of two vectors

1.Vector3.Angle(Vector3 v1,Vector3 v2)

2.Vector3.Dot(Vector3 v3,Vector3 v4)

Mathf.Acos(float) * Mathf.Rad2Deg

Vector3.Cross(Vector3 v5,Vector3 v6)

Get the child objects of an object

1.transform.GetComponentInChildren<component>(bool)

The bool value controls whether to select inactive sub-objects

2.transform.childCount

Get the number of child objects of an object

3.transform.GetSiblingIndex()

Get the index value of the current object

RayCast Target trigger principle

In Unity, UI events will be triggered in the EventSystem in the Update Process.

UGUI will traverse all the UIs in the screen whose RaycastTarget is true, then emit rays, and sort to find the UI that is first triggered by the player, and then throw an event to the logic layer to respond. Checking RaycastTarget will consume performance.

scene switch

Introduce the namespace in the script: using UnityEngine.SceneManagement;

Execution code: SceneManager.LoadScene

Parameters fill in the scene name or the corresponding index value in BuildSetting

SceneManagement.SceneManager.GetActiveScene().buildIndex

Returns the index of the current Scene

Loading a new scene does not destroy specific objects

When switching scenes in Unity, all game objects in the previous scene will be destroyed by default. When we want to keep the game object or script that controls the whole world, we can use the code to control not to be destroyed when switching scenes.

public static void DontDestroyOnLoad (Object target);

common error

1.NullReferenceException: null pointer exception

2.UnassignReferenceException: unassigned exception

3.MissingComponentException: component missing exception

4. IndexOutOfRange: array out of bounds exception

Judging the operating platform

1. Macro definition judgment:

#if UNITY_ANDROID

#endif

#if UNITY_IPHONE

#endif

#if UNITY_STANDALONE_WIN

#endif

2.Application.platform decision:

Application.platform == RuntimePlatform.Android

Application.platform == RuntimePlatform.IPhonePlayer

Application.platform == RuntimePlatform.WindowsEditor

singleton pattern

The Singleton Pattern ensures that there is only one instance of a class and provides a global access point to it.

The core of the singleton mode is that for a singleton class, there is only one instance in the system at the same time, and this instance is easily accessed by the outside world; because there is only one instance in memory, the memory overhead is reduced;

get object

1.public static GameObject Find(string name);

Traversing the entire current scene is inefficient.

Only active game objects can be obtained, and the return value type is GameObject type.

2.public Transform Find(string name);

It is more efficient to only find itself and its sub-objects.

The game object in the active/inactive state can be obtained, and the return value type is Transform type.

orientation control

1.LookAt

transform.LookAt(targetPos);

2. Direction vector

transform.forward = targetPos.position -transform.position;

Collision box detection

Physics.OverlapBox

Physics.OverlapCapsule

Physics.OverlapSphere

Wireframe drawing function:

private void OnDrawGizmos()

Gizmos.DrawWireSphere

The Exists method filters the List object

In the List collection operation of C#, sometimes it is necessary to judge whether there is an element object that meets the condition in the List collection according to the condition. At this time, you can use the Exists method of the extension method of the List collection. It is more direct to judge by Exists than to use the or loop or foreach to traverse the search.

take random number

Random.Range returns a random integer between min and max (read-only)

(1) float type overload random value including minimum and maximum [min,max]

(2) Int type overloaded random value only includes the minimum value, not including the maximum value [min,max)

Impact checking

The RigidBody rigid body is a sign to determine whether to achieve collision.

The OnCollisionEnter method requires that the initiator of the collision must have a rigid body.

The OnTriggerEnter method only needs to have a rigid body on both sides of the collision to be triggered.

Object class

gameobject can usually be omitted, such as this.gameobject.transform()

All C# objects are inherited from the object class, including function types such as int and float, and of course classes such as Monobehaviour and GameObject.

object manipulation

Obtain the object code under a certain root node:

xxxRoot.transform.GetChild(i).gameobject

Compare object tags Tag:

gameobject.compareTag("xxx")

Object hiding:

gameobject.setActive(true or false)

component hidden

component.enabled = true or false

Guess you like

Origin blog.csdn.net/weixin_51669718/article/details/127671145