title: Unity的两种单例模式
继承MonoBehaviour的单例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public static Player _instance;
private void Awake()
{
_instance = this;
}
}
不继承MonoBehaviour的单例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstanceManager
{
private static InstanceManager _instance;
public static InstanceManager Instance
{
get
{
if (_instance == null)
{
_instance = new InstanceManager();
}
return _instance;
}
}
}