How to store some temporary variables, such as scores, stars, etc.?
PlayerPrefs.GetFloat/GetInt/GetString(SetXXXX)
GetXXX("key name", the default value returned when not found);
key = val form
How to modify the text component on the interface?
1, using UnityEngine.UI;
2. Set the public Text object, and set
3. obj.text = "123123123";
How to achieve the effect of pausing the game
Time.timeScale = 0 ; Pause the game, often used for menu calls
= 1 is recovery
How to reload the scene
1. using UnityEngine.SceneManagement
2. The scene should be added to the build setting. You can check the details on Baidu
3. SceneManager.LoadScene(id number); This function is a synchronous method, if the scene is too large, it will cause blocking
3. SceneManage.LoadSceneAsync(id number) This is an asynchronous method
Normally, the public attribute will be displayed in the game panel. After adding this attribute, it can not be displayed in the game panel
[HideInInspector]
public xxxxx;
How to operate the main camera?
Camera.main.xxxxxx
How to play sound?
public AudioClip xxx; game panel settings
AudioSource.PlayClipAtPoint(AudioClip object, location information);
trigger mouse action
void OnMouseDown()
void OnMouseUp()
if(Input.GetMouseButtonDown(0)) If the left button is pressed
How to set the velocity of the object?
Rigid body component.velocity = xxx;
How to set the position of an object?
xxxx.transform.position = originPos; Variable of Vector3
How to move smoothly?
Set xxx.position = Vector3.Lerp (current position, target position, time) in update
The general time will be set to a value *Time.deltaTime
How to limit a value to a range?
Mathf.Clamp(current value, minimum value, maximum value) can limit the range of the camera
How to draw lines?
1. Add a LineRender component to a GameObject
2. In a certain function, get the LineRender component [can be obtained by public, or by GetComponent]
3. Start drawing, just set the point of the line
right.SetPosition(0, rigthPos.position); the 0th point
right.SetPosition(1, transform.position); the 1st point
Other points can be added later.
How to delay the execution of a function, for example, execute a function after 0.5s
Invoke("function name", 0.5f);
How to dynamically create a new object? For example, after the pig is dead, the scores and explosion effects are displayed on it?
Instantiate(boom, transform.position, Quaternion.identity); //explosion effects
The first parameter is a GameObject, which can be set with the public panel property
The second parameter is the location where the object spawns
The third parameter is the rotation parameter, just keep the default Quaternion.identity
How to destroy an object?
Destroy(this.gameObject);
It is also possible to delay destroying objects
Destroy(gameObject,1.5f);
How to get the distance between two points? (Vector3)
Vector3.Distance(rigthPos.position, transform.position)
How to get the length of a vector?
Vec.magnitude (both 2d and 3d)
How to normalize a vector? (Generally used to solve the problem of rotation and limited size)
Vec3.normalized
How to enable, disable a component?
sp = GetComponent<SpringJoint2D>();
sp.enable = false/true;
Anything can be .enable = true/false;
How to enable, disable an object? [The effect is equivalent to clicking √ in the Inspector ]
GameObject.SetActive(true);
How to dynamically modify the picture of the sprite?
private SpriteRenderer render;
public Sprite hurt; //Injured resource picture, drag and set in the UNITY panel
First you need to get it in the void Awake function
render = GetComponent<SpriteRenderer>();
Then you can modify
render.sprite = hurt;
How to obtain the information at the moment of collision? & the relative velocity of the object?
void OnCollisionEnter2D(Collision2D collision)
{
print(collision.relativeVelocity.magnitude);
}
How to get the tag of the triggered object when Trigger?
void OnTriggerEnter2D(Collision2D collision) Start a collision
void OnTriggerExit2D(Collision2D collision) End the collision
collision.gameObject.tag is the tag name of the collision object.
How to modify the variables of the animation state machine? to achieve transitions between animation states?
1. Get the animation component first
2.anim.SetBool("isPause",true); Here, there are 4 functions according to the variable type of the state machine you added