02 component-oriented development

Applications
Unity newly created project, its directory folder contains four folders
Assets: Resource
Library: library
ProjectSetting: Project Settings file
Temp: temporary files

Relations projects, scenarios, game objects and components:
the Unity development: a project which has multiple scenes, a scene which has more game objects, each game object there are multiple components

Unity component development thinking
Unity engine is a component-oriented development, what kind of game objects you want to achieve what function component is added

Transform component
any game object in the scene will have a position, rotation, scaling and the basic information, so that the assembly must exist.
Transform: transform the current game script object mounted
transform may be directly linked refers to the current script load game objects
tranform components to maintain parent-child relationship

Script Component
script must be consistent with the script name when creating the class name, and automatically inherit MonoBehaviour class,

Unity Script Component
Unity script class (two classification):
(the Classes Runtime) run-time class: class execution of the game is running
(Editor Classes) Editor categories: mainly used to implement plug-ins

MonoBehaviour categories:
1.Unity script component must inherit MonoBehaviour class
2. MonoBehaviour inherited from the script component can automatically call it a good package of ruin method.

Unity脚本生命周期
Awake——>Start——>Update——>FixedUpdate——>LateUpdate——>OnGUI——>OnDisable——>OnDestroy

// represents the current script is invoked wake
void the Awake () {
Debug.Log ( "the Awake");
}
// represents the current script is invoked when available
void OnEnable () {
Debug.Log ( "OnEnable");
}
/ / before the first execution of the update function is called
void the Start () {
Debug.Log ( "the Start");
the Destroy (gameObject,. 3);
}
// performed for each frame time during game play
void the update () {
Debug.Log ( "the Update");
//Time.deltaTime represents the current execution time consumed by a
}
// callback fixing frame, the fixed time call
// generally precedes the Update
void FixedUpdate () {
Debug.Log ( "FixedUpdate" );
}
// Update each executing the function executes the
void LateUpdate () {
// always performed after Update
Debug.Log ( "LateUpdate");
}
// the GUI rendering and processing GUI events
OnGUI void () {
Debug.Log ( "OnGUI");
}
// Called when the script is not available
void OnDisable () {
Debug.Log ( "OnDisable");
}
// Called when the script is destroyed
void OnDestroy () {
Debug.Log ( "the OnDestroy");
}

Unity Script component commonly used classes

       GameObject categories:

     Get the game object tag value: GameObject.tag
    get the name of the game object; GameObject.name
find the corresponding game objects based on the object name
GameObject.Find ( "Sphere");
find the corresponding game objects based on the value of Tag game objects
GameObject.FindGameObjectWithTag ( "Player");
by this method to find a class has the same label plurality of game objects, in the order in the returned array is an array with the order of creation of the object opposite the game.
to change by components on Cube Cube color: GetComponent <MeshRenderer> () .material.color = Color.red;
default script is mounted to the object, if the object is to change the current game script mounted, can be omitted gameObject: playerObj.GetComponent <MeshRenderer > () .material.color = Color.green;
set of game objects unavailable: enemyObj.SetActive (false);
remove game object after 3 seconds: Destroy (enemyObj, 3f);

Transform categories:

   World coordinate position: Debug.Log ( "position =" + transform.position);
   relative to the distance between the parent object: Debug.Log ( "Localpotion =" + transform.localPosition);
   object rotation: Debug.Log ( " roation = "+ transform.rotation);
  with respect to the world origin of Euler angles: Debug.Log (" eulerAngle = "+ transform.eulerAngles);
  relative to the parent object Euler angles: Debug.Log (" localeulerAngle = "+ transform.localEulerAngles);
  zoom: Debug.Log ( "scale =" + transform.localScale);
  find games parent object: Debug.Log (transform.parent);
  // toward GameObject
public target the Transform;
void the Update ( ) {
transform.LookAt (target);
// this is a game object script mounted toward target, target object toward the game
Translate: let the object is moved toward a certain direction
transform.Translate (new Vector3 (0.01f, 0 , 0));
If left blank or set to relativeTo Space.Self, movement is applied with respect to its axis transformation. (When the selected object scene view, x, y and z axes shown) if Space.World relative movement is applied with respect to the world coordinate system.
Another moves
transform.position + = new Vector3 (0.01f, 0, 0);
the object is rotated about an axis
transform.Rotate (new Vector3 (0, 10 , 0));
pivoting
transform.Rotate (new Vector3 (5, 0, 0) , 10);
about a point of rotation, the second rotatable shaft parameter, the first parameter is the point about the third rotation angle parameter
transform.RotateAround (new Vector3 (0 , 0, 0), new new Vector3 (0,. 1, 0), Speed);
transform.forward, transform.right, transform.up

Vector3 (three-dimensional vector)
Vector3 vector is encapsulated structure related variables and methods
Debug.Log ( "position vector V3:" + transform.position);
Normalized (property): indicates the normalized vector, the direction of change of size. 1
the Debug .Log ( "normalized vector V3:" + transform.position.normalized);
Magnitude: length V3 vector
Debug.Log ( "V3 vector length" + transform.position.magnitude);
sqrMagnitude: square of the length of the vector V3
Debug .Log ( "the square of the length of the vector V3" + transform.position.sqrMagnitude);

Debug.Log (Vector3.up);//(0,1,0)
Debug.Log (Vector3.down);//(0,-1,0)
Debug.Log (Vector3.right);//(1,0,0)
Debug.Log (Vector3.left);//(-1,0,0)
Debug.Log (Vector3.forward);//(0,0,1)
Debug.Log (Vector3.back);//(0,0,-1)

Moving object control
transform.Translate (Vector3.left * Time.deltaTime);

VEC = new new Vector3 Vector3 (. 6,. 8, 10);
Debug.Log ( "before normalization:" + VEC);
Normalize can get a standardized and normalized vector, and the caller same direction, are of size 1,
but using Normalize It will direct the caller to change itself, but using normalized does not change the caller
vec.Normalize ();
Debug.Log ( "after standardization:" + vec);

// find the distance between the pellets and the Cube
(first parameter is the initial position, the second position is the end)
a float DIS = Vector3.Distance (transform.position, cubeTransform.position);

Determining the distance between the beads 0.1 and less than Cube
confirm the ball reaches the Cube
IF (DIS> = 0.1f) {
// Lerp uses interpolation, the interpolation interpolation coefficients a (<1), the first movement two * the distance between the object interpolation coefficients, the second movement of the remaining distance * interpolation coefficients, only infinitely close to another object, never stop

The first parameter is the position of the moving object to the second object is a moving target, the third parameter is the interpolation coefficient
transform.position = Vector3.Lerp (transform.position, cubeTransform.position, 0.02f);
}
Cube orientation angle of rotation obtained pellets of
a float angle = Vector3.Angle (cubeTransform.forward, (transform.position - cubeTransform.position));
Debug.Log ( "Cube orientation angle of rotation of pellets:" + angle);

Vector3.Angle (angle):
Returns to and from the two vectors is no greater than a 180 ° angle of


Class Time:
From now to start the game all the time
Debug.Log ( "time =" + Time.time );

Current time executing a
Get Last Update () time to the execution time of the execution of this Update
injection; deltaTime every value is not the same, is an unstable value
Debug.Log ( "deltaTime:" + Time.deltaTime);

A fixed number of frames, the execution time of each frame
Debug.Log ( "fixedDeltaTime:" + Time.fixedDeltaTime );

It represents a time scaling, time scaling is a positive, if transferred into a 1.5 playback speed becomes faster, tune to 0, the time will still
be done pauses the game
Debug.Log (Time.timeScale);

Quaternion class:
the Identity: no rotation quaternion representation, commonly used in creating an object
LookRotation: returns a pointer from its own position to the target position vector
when // get the perspective right in front of the Cube to see the target destination point you want to rotate angle quaternion
Rote = Quaternion.LookRotation (targetTransform.position - transform.position);
// rotate to be assigned to the quaternion ,, Cube Cube toward a target point to achieve
transform.rotation = Quaternion.Lerp (transform.rotation, rote , Time.deltaTime);

Mathf categories:
Lerp: interpolating between two float
a float TEMP = Mathf.Lerp (1F, 1OF, 0.5f);
// corresponds to: (. 1 + 10) * 0.5
Debug.Log ( "TEMP =" TEMP + );
Clamp: returns a limit value
//Mathf.Clamp: returns the number indicates the maximum value and the minimum value between,
// if the return value is less than the minimum value, then returns the minimum value
// If greater than the maximum value, then returns the maximum value
//Debug.Log (Mathf.Clamp (time.time, 1.0f, 3.0f));
Sqr: returns the square root
pI: pi π: 3.1415926 ...

Preset body:
Preset body is a collection of game objects, use the default body can make game objects or resources reuse
after using the default body can use scripts to control the generation or destruction of game objects, rather than manually machinery in the hierarchical view created on.
use instantiate code to achieve the creation of the game object is equivalent to one example of the process in the scene.

Guess you like

Origin www.cnblogs.com/zpy1993-09/p/11730551.html