Jednota singletonů

Příklady kódu

12
0

jednota singletonů

public class Example
{
public static Example Instance{get; private set;}

void awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}

}
5
0

jednota singleton vzor

#region Singleton

void Awake()
{
	if (instance == null)
	{
		instance = this;
	}
	else
	{
		Destroy(gameObject);
		return;
	}

	DontDestroyOnLoad(gameObject);
}

#endregion
3
0

jednota singletonová

public class Example
{
public static Example Instance{get; private set;}

void Awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}

}
2
0

jak vytvořit singleton v jednotě

 void Awake()
 {
   if (instance == null)
     instance = this;
   else if (instance != this)
     Destroy(gameObject);
 }
0
0

jednota singletonová

	
	private static GameObject _instance;
	// Create an accessible reference to the singleton instance
	public GameObject instance
	{
		get
		{
			// Obtain singleton instance, check if one exists first
			if(_instance = null)
			{
				_instance = new GameObject();
			}
			return _instance;
		}
		set
		{
			// If an instance is not null, one already exists
			if(_instance != null)
			{
				// Check if instance IDs differ, if they do then destroy duplicate
				if (_instance.GetInstanceID() != value.GetInstanceID())
					DestroyImmediate(value.gameObject);
				return;
			}
			// If the passed instance is new (different), assign it
			_instance = value;
		}
	}
    
	private void Awake()
	{
		instance = this;
	}

Související stránky

Související stránky s příklady

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................