The pit of the dontdestroyonload of the Unity3d Institute

Source: Internet
Author: User

http://www.xuanyusong.com/archives/2938

One way in unity Dontdestroyonload can make certain game objects not cast when switching scenes, which sounds like a great way to do it, but it's probably a problem if it doesn't work out well.

As an example:

In the Start method of one of the scripts in Scene 1, Dontdestroyonload (a)

Then switch to Scenario 2, where a object is reserved.

If you go back to 1 scenes from 2 scenes, then execute again dontdestroyonload (a) However, before your a object has not been cast, the wireless loop continues.

Of course we can do the logical judgment whether dontdestroyonload, judge A is equal to NULL to do some judgment. But I think it's a really unwise thing to do. The Dontdestroyonload method is actually more to save a global game script, such as some third-party SDK you need to use this script to do middleware support.

My current practice is that I have made an initialization scenario. In the initialization scene I only do two things, the first in the initialization scene of a game object in the global script, the Start method I put all the game objects in this scene is all set to Dontdestroyonload, that is, switching scenes without destroying, Like the root makeup structure of Ngui.

1234 object[] initsobjects = gameobject. Findobjectsoftype (typeof ( gameobject) foreach (Object go in initsobjects) { dontdestroyonload(go); }

Second, then I'm entering my first game scene, which means my logic will never return to my initialization scenario. So I don't have to go back and forth to cut the scene dontdestroyonload without deleting the problem.

Another clever way to do this is to use the static initialization method, as shown in the following code, when the code calls the global class

First the program will go into the static global method, this method will always go through, so I create a gameobjcet here, and then bind the global script up, I am dontdestroyonload this object.

12345678910111213141516171819202122232425 using unityengine; using System. Collections; public class Global :monobehaviour {Public static Global instance; static Global() { gameobject go = new gameobject("Globa"); dontdestroyonload(go); instance = go. AddComponent<Global>(); }Public void dosomethings() { Debug. Log("dosomethings"); } void Start () { Debug. Log("Start"); }}

This script is like a static script, and the game object will never be destroyed by switching scenes. And it's very convenient to use. Just call it where it needs to be called.

1 Global. Instance. Dosomethings();

The pit of the dontdestroyonload of the Unity3d Institute

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.