5unity-3d Day06

Source: Internet
Author: User

Continue to make a single example mode in the actual combat to experience the single case mode

Ah still do not understand although know how to write, but where the use is still very vague and so on later write more good

In retrospect, the singleton pattern is defined as guaranteeing that a class has only one instance and provides a global access point to access it. The class of the Singleton object must guarantee that only one instance exists. In our scenario, the global script object is created only once, guaranteeing the singleton, and then it (the global Singleton Class) provides a way for each script to access the Singleton object, and all other script common data is stored in the global singleton script.

The singleton mode provides easy access to data, objects, and only one instance at a time, guaranteeing the uniqueness of each program module. You might think of global variables, yes, you can do it with global variables. However, in order to provide maintainability of the code, the overall situation is very limited. You also want to be able to share information with static variables often in unity, but you also find that he is declared everywhere. Right, not easy to find, not good maintenance.

As for the use of it, I'm in the process of controlling the game, background music, switching scenes and what, using a singleton mode.

The following is a singleton pattern for a model: a singleton of a model cannot have a constructor! Also pay attention to the order of execution of awake and start.

usingUnityengine;usingSystem.Collections;//Model Singleton Mode Public classgamemanager:monobehaviour{Private StaticGamemanager M_manager;  Public StaticGamemanager M_manager {Get        {            if(M_manager = =NULL) {M_manager=NewGameobject ("Gamemanager"). Addcomponent<gamemanager>(); }            returnM_manager; }    }    //Note the order of execution    voidAwake () {//Adding audio ComponentsGameobject.addcomponent<audiosource>(); }    //Use this for initialization    voidStart () {}//Add Audio     Public voidplayaudio (audioclip clip) {Audio.    Playoneshot (clip); }    //Update is called once per frame    voidUpdate () {}}

Just a simple call in the camera script.

void Start ()    {        = Gamemanager.m_manager;        Manager. Playaudio (clip);         = Modelmanager.m_mamager;    }


There is a singleton for the object, and the usage is no different from C #.

usingUnityengine;usingSystem.Collections;//Object Single Example Public classmodelmanager{Private StaticModelmanager M_mamager;  Public StaticModelmanager M_mamager {Get {            if(M_mamager = =NULL) {M_mamager=NewModelmanager (); }            returnModelmanager.m_mamager; }    }     Publicmouse[] Mouses =Newmouse[Ten];  PublicModelmanager () { for(inti =0; I <Ten; i++) {Mouses[i]=NewMouse (); }    }    }


Here are some of the application classes that are used to feel or are the same formula:

usingUnityengine;usingSystem.Collections; Public classgamecontroller:monobehaviour{ PublicAudioClip clip; //Use this for initialization    voidStart () {Gamemanager Manager=Gamemanager.m_manager; Manager.        Playaudio (clip); Modelmanager Modelmanager=Modelmanager.m_mamager; }    //Update is called once per frame    voidUpdate () {}voidOngui () {Guilayout.label (Application.platform.ToString ());             Guilayout.label (Application.runInBackground.ToString ());        Guilayout.label (Application.dataPath.ToString ());        Guilayout.label (Application.persistentDataPath.ToString ());        Guilayout.label (Application.temporaryCachePath.ToString ());        Guilayout.label (Application.loadedLevel.ToString ()); //Switching scenes        if(Guilayout.button (" Level")) {            //before destroying the sceneApplication.loadlevel (1); //do not destroy previous scenesApplication.loadleveladditive (1); }        //Exit        if(Guilayout.button ("quit") ) {application.quit (); }        //link Open Web page        if(Guilayout.button ("Baidu") {Application.openurl ("http://www.baidu.com"); }            }}

So much research today good night buddies!

5unity-3d Day06

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.