【发布时间】:2016-05-05 07:03:02
【问题描述】:
我正在尝试使用单例模式来监控 Unity 项目的统计信息。我正在使用下面的课程。我遇到的问题是,我的代码似乎不是只有一个单例,而是为我调用单例函数的每个实例创建一个单例。例如,每次玩家开枪时,我都会拨打ShootingStats.Instance.incrementTotalShotsFired();。如果我总共有三个玩家,我的代码似乎会生成三个 ShootingStats 单曲 - 每个玩家一个。我想要实现的是只有一个类来监控所有玩家的这些统计数据,而不是每个玩家一个。
我找到了单例代码:http://wiki.unity3d.com/index.php/Singleton。我是 Unity 新手,但在编程方面很有经验。我最初尝试使用一个非常简单的 Singleton 模式版本,就像有人在 C# 中使用的那样,但它没有按预期工作。相反,每次我访问其中一个单例方法时,它都会创建一个新对象。非常感谢您提前提供的帮助。
单例类
using UnityEngine;
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
///
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
namespace Porject.Statistics
{
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
if (applicationIsQuitting)
{
//Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
// "' already destroyed on application quit." +
// " Won't create again - returning null.");
return null;
}
lock (_lock)
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (FindObjectsOfType(typeof(T)).Length > 1)
{
//Debug.LogError("[Singleton] Something went really wrong " +
// " - there should never be more than 1 singleton!" +
// " Reopening the scene might fix it.");
return _instance;
}
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton) " + typeof(T).ToString();
DontDestroyOnLoad(singleton);
//Debug.Log("[Singleton] An instance of " + typeof(T) +
// " is needed in the scene, so '" + singleton +
// "' was created with DontDestroyOnLoad.");
}
else
{
//Debug.Log("[Singleton] Using instance already created: " +
// _instance.gameObject.name);
}
}
return _instance;
}
}
}
private static bool applicationIsQuitting = false;
/// <summary>
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
public void OnDestroy()
{
applicationIsQuitting = true;
}
}
}
ShootingStats 类
using UnityEngine;
using System.Collections;
using Project.Statistics;
namespace Project.Statistics
{
public class ShootingStats : Singleton<ShootingStats>
{
private int _onTarget = 0;
private int _totalShotsFired = 0;
protected ShootingStats() { }
public void incrementOnTarget()
{
_onTarget++;
}
public void incrementTotalShotsFired()
{
_totalShotsFired++;
}
public void printStats()
{
string lines = "Total shots fired: " + _totalShotsFired + "\n" + "Shots on target: " + _onTarget + "\n.";
Debug.Log(lines);
}
}
}
MethodExtensionForMonoBehaviourTransform 类 - 不确定这是什么
using UnityEngine;
static public class MethodExtensionForMonoBehaviourTransform
{
/// <summary>
/// Gets or add a component. Usage example:
/// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
/// </summary>
static public T GetOrAddComponent<T>(this Component child) where T : Component
{
T result = child.GetComponent<T>();
if (result == null)
{
result = child.gameObject.AddComponent<T>();
}
return result;
}
}
【问题讨论】:
-
很抱歉,您的评论没有任何帮助。什么应该是静态的,为什么?我调用获取实例的方法是静态的。更多解释将不胜感激
标签: c# design-patterns unity3d singleton unity5