Use Unity3d to be aware of the details

Source: Internet
Author: User
Tags translate function


Welcome to Unity Learning,Unity training ,UnityEnterprise TrainingEducation Area, there are manyu3d Resources,u3d Training Video,u3d Tutorials,U3d Frequently Asked questions,u3d Project Source code, the "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry unity3d Training , learning the first brand.



Please be careful when operating transform.localposition.

Moving Gameobject is a very common thing, and the code looks simple:

Transform.localposition + = new Vector3 (10.0f * time.deltatime, 0.0f, 0.0f);

But be careful, assuming that the above Gameobject has a parent, and that the parent Gameobject Localscale is (2.0f,2.0f,2.0f). Your gameobject will move 20 units per second. Because the Gameobject world position equals:

Vector3 offset = new Vector3 (my.localposition.x * parent.lossyscale.x,
MY.LOCALPOSITION.Y * Parent.lossyscale.y,
MY.LOCALPOSITION.Z * parent.lossyscale.z);
Vector3 worldposition = parent.position + parent.rotation * OFFSET;

In other words, the Localposition method of direct operation above is done without considering scale calculation, in order to solve this problem, Unity3d provides translate function, so the correct practice should be:

Transform. Translate (10.0f * time.deltatime, 0.0f, 0.0f);

Exposure to variables in inspector can also be used by animation View Editor

Sometimes we want to use Unity3d's animation View editor to do some simple animation operations. Animation Editor can operate not only the component of Unity3d itself, but also the various property in our custom Monobehavior. So if you have a float value that needs to be manipulated with a curve, you can simply expose it to a type that can be serialize, such as:

public float foobar = 1.0f;
In this way, this variable will not only appear in inspector, but can also be manipulated in animation view, generating animationclip for us to call through Animationcomponent.

Example:
public class Testcurve:monobehaviour {
public float foobar = 0.0f;
IEnumerator Start () {
Yield return new waitforseconds (2.0f);
Animation. Play ("Foobar_op");
Invokerepeating ("Logfoobar", 0.0f, 0.2f);
Yield return new Waitforseconds (animation["Foobar_op"].length);
Cancelinvoke ("Logfoobar");
}
void Logfoobar () {
Debug.Log ("Foobar =" + Foobar);
}
}
  

Getcomopnentt can take the parent class type
Unity3d allows us to derive from Monobehavior, so you might have the following code:
public class Foo:monobehaviour {
...
}
public class Bar:foo {
...
}
  
Suppose we now have a A, a, B, two gameobject, a containing Foo and a bar, and when we write
Foo COMP1 = A.getcomponentfoo ();
Bar COMP2 = B.getcomponentbar ();

Can see COMP1, comp2 all got the component deserved. So if we change the operation of B to:

Foo COMP2 = B.getcomponentfoo ();
The answer is whether COMP2 will return bar component and convert to Foo type. You can also use the down conversion to get valid variables:

Bar Comp2_bar = comp2 as bar;
The rational use of Getcomponentbase_type () allows us to design component with less coupling.

Invoke, yield and other functions are affected by Time.timescale

Unity3d provides a very convenient function time.timescale to adjust the time. For those who use Unity3d for the first time, it is misleading to assume that Time.timescale can also be used for in-game pauses (pause) and start (Resume). So many people have the habit of writing:

Time.timescale = 0.0f

For the game to pause/start, is part of the game system design, and Time.timescale is not used for this part of the operation. The right thing to do is to collect scripts or gameobject that need to be paused, to stop their scripting activities by setting their Enabled = False, or to set them off when they are paused by a specific function.

Time.timescale more is used in the game slow-motion playback and other operations, in the server-side-dominated game should avoid such operations. It is worth mentioning that many of the time-related functions of unity3d are linked to timescale, while timescale = 0.0f will leave these functions or animations in a completely stopped state, which is the main reason why it is not suitable for a pause operation.

Some of the main functions and component affected by timescale are listed here:

Monobehaviour.invoke ()
Monobehaviour.invokerepeating ()
Yield Waitforseconds ()
Gameobject.destroy ()
Animation Component
Time.time, Time.deltatime
The relationship between Coroutine and IEnumerator

When we first write Unity3d C # scripts, we often make the mistake of calling the Coroutine function to forget how to use Startcoroutine. Such as:

TestCoroutine.cs
IEnumerator Colog () {
Yield return new waitforseconds (2.0f);
Debug.Log ("Hello Foobar");
}

When we use the following code to call the above function:

Testcoroutine Testco = Getcomponenttestcoroutine ();
Testco.colog ();
Testco.startcoroutine ("Colog");
Then the Testco.colog () call will have no effect.

Startcoroutine, Invokerepeating and its caller association
Usually we only call startcoroutine or invokerepeating in a gameobject, we write:


Startcoroutine (Foobar ());
Invokerepeating ("Foobar", 0.0f, 0.1f);
So if this gameobject is disable or destroy, these coroutine and invokes will be canceled. As we call it manually:

Stopallcoroutines ();
Cancelinvoke ();
  

This looks wonderful, for AI, it's like telling an NPC you're dead, you of their ownJust listen to the little gestures.
  

But note that if this code is used in a manager-type control AI, he might be able to control other AI, or invoke, coroutine to do some micro-threading operations, The design of the caller of Startcoroutine or invokerepeating should be clarified at this time. Before we go into the discussion, we need to understand that the call of Startcoroutine or invokerepeating will open a thread state in that monobehavior, and will need to manipulate the function, Variables and timers are placed in this stack and are processed uniformly before the renderer renders at the end of the update for each frame of the engine. So if this monobehavior is destroy, then the thread state disappears, and all of his stored calls fail.

If there are two copies of Gameobject A and B, they know each other, if a in the Startcoroutine or invokerepeating to invoke the function of B to control B, this time the thread state is stored in a, When a is disable or destroy, these may take a period of time the control function also fails, this time B is not dead, and will not move. A better approach is to have the thread state stored in B by B.startcoroutine () in the function of a.
  
Class Testcortouine
public class Testcoroutine:monobehaviour {
Public IEnumerator Colog (string _name) {
Debug.Log (_name + "Hello foobar 01");
Yield return new waitforseconds (2.0f);
Debug.Log (_name + "Hello foobar 02");
}
}
Component attached on Gameobject A
public class A:monobehaviour {
Public Gameobject B;
void Start () {
Testcoroutine COMPB = B.getcomponenttestcoroutine ();
Good, thread state in B
Same As:compB.StartCoroutine ("Colog", "B");
Compb.startcoroutine (Compb.colog ("B"));
Bad, thread state in A
Startcoroutine (Compb.colog ("A"));
Debug.Log ("Bye Bye A, we'll miss You");
Destroy (Gameobject); t_t I don ' T want ...
}
}
  
The above code, the result will be:
B Hello Foobar 01
A Hello Foobar 01
Bye Bye A, we ' ll miss you
B Hello Foobar 02
  
If you do not need start, Update, lateupdate function, please remove them
When there are no start, Update, lateupdate functions in your script, Unity3d will not add them to his Update list, which facilitates the overall efficiency of the script.
We can see the difference between the two scripts:


Update_01.cs
public class Update_01:monobehaviour {
void Start () {}
void Update () {}
}
Update_02.cs
public class Update_02:monobehaviour {
}

For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy Http://edu.gopedu.com


Statement: This document comes from the "Dog Planing Learning Network" Community-unity Extreme College, is a self-published Unity3d study articles, if anything violates your relevant interests, please communicate with the official, we will deal with the real-time.



  

Use Unity3d to be aware of the details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.