Unity Tips

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangjiangrong/article/details/97016565

1.Component of copy paste

It is equivalent to Copy under Inspector window Component, Paste Component Values, Paste Component As New

#if UNITY_EDITOR

//首先复制需要复制的组件
UnityEditorInternal.ComponentUtility.CopyComponent(needCopyComponent);

//粘贴组件的值到另一个组件上
UnityEditorInternal.ComponentUtility.PasteComponentValues(newComponent);

//为GameObject添加复制的组件
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObject);

#endif

 

2. Component Removal

//Running模式下
Destroy(component);

//Editor模式下
DestroyImmediate(component);

 

Update method or OnGUI script execution mode under 3.Editor

Sometimes in order to help plan and do some editing tools needed in Editor mode, which automatically help to do the operation, to be tested under Update. Add ExecuteInEditMode label can before the class definition.

[ExecuteInEditMode]
public class TestComponent : MonoBehaviour
{
}

 

4. radian angle conversion

Mathf.Sin (float), the method Mathf.Cos (float) These sine cosine, the unit parameter is in radians rather than angle .

Radians: arc length equal to the radius of the arc, the central angle for which it was 1 radian unit abbreviation rad. (A circle with a radius r, when an angle α arc length r, then the angle α is the arc length a)

By definition we can know, 2nr is the circumference of a circle, corresponding to 360 °, i.e. 2nr = [deg.] 360, 180 [pi] r = [deg.] . 1rad corresponding to the arc length r, can be obtained, 1rad ÷ π≈57.295 = 180 [[deg.] [Deg.] Turn, . 1 [deg.] ≈0.01745rad

There are two variables for converting the Mathf

Mathf.Deg2Rad = 0.0174532924F; // degrees to radians

Mathf.Rad2Deg = 57.29578F; // angle of rotation in radians

So if we ask sin30 °, the wording should be

Mathf.Sin(30 * Mathf.Deg2Rad);

 

5.Vector3.ProjectOnPlane

Vector3.ProjectOnPlane (Vector3 vector, Vector3 planeNormal); the normal vector to vector is the vector projected on the plane planeNormal.

For example, you have a vector Vector3 v = new Vector3 (x, y, z); you want to ask him axis vector in the xy plane (z-axis normal vector), this method can be used

Vector3.ProjectOnPlane(v, Vector3.forward);

 

6. The vector dot product, the cross product

Dot product

Vector3.Dot(Vector3 lhs, Vector3 rhs);

Calculated as: a · b = | a | · | b | cos <a, b> where | a | and | b | magnitude of a vector represented, <a, b> represents the angle between two vectors. Also in the dot product, <a, b> and <b, a> angle regardless of the order.

Under the premise of a, b non-zero, if the dot product is negative, then a, b form an angle greater than 90 degrees; if zero, a, b vertical; if positive, then a, b form an acute angle . Since the mold is a unit vector, the dot product of two unit vectors obtained cos of the angle between two vectors , dot product so we can calculate the angle between two vectors.

Vector3 a = new Vector3(7, 0, 0);
Vector3 b = new Vector3(0, 8, 0);

//cos<a,b> = x
float x = Vector3.Dot(a.normalized, b.normalized);

//反余弦 Cos(r) = x, r = Acos(x);
float r = Mathf.Acos(x);

//前面得到的是弧度,转角度
float angle = r * Mathf.Rad2Deg;

Cross product

Vector3.Cross(Vector3 lhs, Vector3 rhs);

Calculated as: | c | = | axb | = | a | · | b | sin <a, b>

Vector3.Cross (a, b) value obtained is still a vector c, a plane perpendicular to c, b is formed, and the direction of right-hand rule compliance (i.e. b to a bending direction refers to a steering to the right-hand four large direction is the direction of the c's thumb) thus axb ≠ bxa, but = AXB - BXA . Therefore, we can use this property to determine the relative positions of a, b, and also can obtain the angle a, b of the mold cross-product of two unit vectors as sin of the angle between two vectors

Vector3 a = new Vector3(1, 0, 1);
Vector3 b = new Vector3(0, 0, 1);

Vector3 c1 = Vector3.Cross(a, b);
Debug.Log("c1:" + c1);//c1:(0.0, -1.0, 0.0)
//xz平面上,b到a为顺时针

Vector3 c2 = Vector3.Cross(b, a);
Debug.Log("c2:" + c2);//c2:(0.0, 1.0, 0.0)
//xz平面上,a到b为顺时针

//|c| = sin<a, b>
Vector3 c = Vector3.Cross(a.normalized, b.normalized);
//x = |c|
float x = Vector3.Distance(Vector3.zero, c);
//反余弦 Sin(r) = x, r = Asin(x);
float r = Mathf.Asin(x);

//前面得到的是弧度,转角度
float angle = r * Mathf.Rad2Deg;
Debug.Log("angle:" + angle);//angle:45

 

7. Objects uniform motion, constant acceleration

Uniform motion, by the equation: Distance = Speed ​​Time *

float speed = 10;

void Update()
{
    transform.position += transform.forward * speed * Time.deltaTime;
}

Uniformly accelerated motion, by the formula:

float speed = 0;
float endspeed;
int acceleration = 10;//加速度

void Update()
{
    endspeed = speed + acceleration * Time.deltaTime;
    //transform.position += transform.forward * (speed * Time.deltaTime + acceleration * Time.deltaTime* Time.deltaTime*0.5f);
    transform.position += transform.forward * (speed + endspeed) * 0.5f * Time.deltaTime;
    speed = endspeed;
}

 

8.Unity project to open

Some items require multiplayer online, when you need to open the test to simulate. But only with a unity project open at a time, we can refer to a new project by the coupling of the same way to achieve more open. As shown, the need to open up the original project was MainProject, and MainProject_copy is our mirror project

We create a .bat file in the root directory of the same level required to open the project, as follows:

%cd%
 
rem 需要创建的目录
set dir = CopeProjectFolderName
 
rem 如果没有则创建
if not exist %dir% ( md %dir%) 
 
rem 创建链接
mklink /J %dir%\Assets YourProjectFolderName\Assets
mklink /J %dir%\ProjectSettings YourProjectFolderName\ProjectSettings
 
pause

CopeProjectFolderName: Mirror project file name

YourProjectFolderName: the original project file name

Principle is to create a directory and join Assets ProjectSettings directory of original works project in the mirror, so that the original directory modification, it mirrors a directory will also be changed.

After you run the .bat file, and set up a new project, and finally with unity can open the new project.

Guess you like

Origin blog.csdn.net/wangjiangrong/article/details/97016565