Generator This thing is very common in the game, we need to use this thing if we want to randomly produce the enemy. Plainly, I just need something to produce a lot of objects under my request. In the Unity3d directly to provide the interface can be easily done, I started to think that the generator is using unity3d particle system particle systems do, in fact, there is a preset and the instantiation of instantiate help you to complete together easily. Of course, the generation must abide by the Basic Law of Unity3d, ah, can not say that the generation is generated, need to be timed generation. Timed generation, should not be in the update () function in the form of an assembly language polling hangs in a way that is always judged, Unity3d provides the concept of the process to help you complete the scheduled task. Here's a simple example to illustrate all this.
As shown above, as long as I easily click the right mouse button, there will be a lot of Cube cube fell down, if this is gold, toys or chocolate more cool ah. And then I just click the right mouse button to stop falling, just like closing the valve. At the same time, the cube generated here is not violent, every 0.2s one, or the video card to explode, especially I this slag i3 set display.
So what does the above procedure do?
first, Scene layout
1, 2D part nothing to say, please refer to the "Unity3d" bulletin board and the layout of the Start interface (click to open the link) and "Unity3d" Ugui adaptive screen and Anchor point (click Open link) layout good one text at the same time make an adaptive screen.
2. The 3D section is shown in the following figure. Cube and plane please give yourself black solid color material, material use do not understand can refer to "Unity3d" object, material settings, object displacement and rotation "(Click to open the link).
3. We will attach the following script Cube.cs to Cube cube, which is very simple. Actually did not write also line, just for those flying out of view of the cube or consciously destroy it, or the game has been calculated it crazy fall, the flight of 3,000 feet, suspected Galaxy fell nine days off cube, in the near Y negative infinity position is CPU computing coordinates, simply CPU to fry. No script can be added to refer to "Unity3d" Helloworld (click to open link).
Using Unityengine;
Using System.Collections;
public class Cube:monobehaviour
{
//initialization
void Start ()
{
}
//Update is C Alled once per frame
void Update ()
{
if (Gameobject.transform.position.y <-2)//Once out plane
{
Gameobject.destroy (Gameobject);//Destroy Object
}}
}
4, set the preset, this is a key step in the layout of the scene. As shown below, set a prefab in assets as you would set the material.
Then drag the cube directly into the prefab, which is a preset, meaning: This thing will be copied several times, and prefab is the template.
This step must be done before we fully set up the cube, including giving the good script.
second, script writing
We will then assign the following Create.cs script to the empty object gameobject.
Using Unityengine;
Using System.Collections;
public class Create:monobehaviour {private IEnumerator coroutine;
private bool Isstart = false;//is used to control whether the generator is making a flag public gameobject createobject;//Set the object form parameter void Start () { Coroutine = Waitandprint (0.2f);//waitandprint function executes 0.2s once} void Update () {if (Input.getmousebutt Ondown (1))//If the right mouse button is pressed {if (!isstart)//does not start {startcoroutine (coroutine);//
Open this generator chant Isstart = true;
} else {stopcoroutine (coroutine);//Destroy Isstart = false; Coroutine = Waitandprint (0.2f);//Of course, after the destruction to seed, for the next time to open to prepare}} private IE
Numerator waitandprint (float waitTime)//co-coroutine exists, with the following yield return callback, this code will be executed by 0.2s once {while (true) {Gameobject.instantiate (createobject, GameObject.transform.position, Gameobject.transform.rotation);//In fact, this piece of code//createobject is the generated object, the second third parameter refers to the position and direction of the generation, here is the current position and direction of the passing position Yie
LD return new Waitforseconds (waitTime);
}
}
}
And then in the editor setting this script CreateObject is referring to that prefab, as shown in figure:
At this point, release, the whole project done. The following emphasis is put on the meaning of the above script, which is the focus of this article.
1, gameobject in the form of parameters CreateObject refers to the default prefab, that is, prefab is passed in the script Create.cs with the existence of variable CreateObject. This pass value, you can see "Unity3d" with the scene object value and vector (click Open link), here no longer repeat.
2, the above script, copy object, come and go is a line gameobject.instantiate (CreateObject, GameObject.transform.position, GameObject.transform.rotation);.
The Gameobject.instantiate () method can replicate a preset prefab once, which is called a preset instantiation instantiate Unity3d.
So these proper nouns are used to bluff people, but it's not difficult at all.
3, the above script, the use of the Unity3d, that is, Coroutine completed a similar timer timer function java. "Java" timer, thread and anonymous inner class (click to open link).
Not my blind writing, is directly from the Unity3d API copied down with: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
Compared to the API and the implementation of the script, it is estimated that everyone has already understood the same.
The
uses a C # a seemingly IEnumerator, but complex concept. In fact, no need to control him, copy is. The whole segment function and JavaScript setinterval, see "JavaScript" a synchronization in local time Dynamic Time "(click Open link) very similar, or more should be said to be settimeout, as long as we specify the function name, and time interval, It will be executed automatically and honestly.