based on unity and C-sharp, there is a code from conception to implementation as follows. I don"t understand why this code is singleton mode.
public class BoidSpawner : MonoBehaviour{
static public BoidSpawner S;
.....
public GameObject boidPrefab;
....
void start( ){
//SBoidSpawner
S = this;
}
void LateUpate(){
.....
}
}
< hr >
for the above code, there is no instance of BoidSpawner created. And I didn"t see it in the follow-up code.
for Java, usually the singleton pattern is simple and can be written as (the code in the rookie tutorial):
public class SingleObject {
// SingleObject
private static SingleObject instance = new SingleObject();
// private
private SingleObject(){}
//
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
< hr >
the above code is easy to understand. An object of this class is created within the class, and the static method SingleObject is called to get a unique instance.
but I don"t understand the C-sharp paragraph. There is no instance of this class created in the BoidSpawner class, and for the BoidSpawner class that can create such objects frequently, new BoidSpawner (), does not quite understand the direction of the this in the code, because there is no new BoidSpawner () in the whole code. If you mount the script to a node, is this node an instance of this class?
does not mean that scripts in unity are like separate OOP classes, and scripts attached to scene objects are instances of objects. In fact, I don"t quite understand why the script becomes an instance of the object, and the object is not a class.