jednota singletonů
public class Example
{
public static Example Instance{get; private set;}
void awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}
}
jednota singleton vzor
#region Singleton
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
}
#endregion
jednota singletonová
public class Example
{
public static Example Instance{get; private set;}
void Awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}
}
jak vytvořit singleton v jednotě
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
}
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;
}