General Code Outline: Minimum Code, Strongest Framework, Optimal Logic
1. The appearance of an extension method greatly simplifies the amount of code
Similar to GetComponent, our custom extension method also adds some necessary logic to it as needed.
//Extension Method
//Static Class->Static Method->Parameter List First is: this type parameter
public static class ExpandClass {
/// <summary>
/// Get Instances
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T GetInstance<T>(this T t,string s) where T : new()
{
if (t == null)
{
t = new T();
}
UnityEngine.Debug.Log(s);
return t;
}
}
test
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExpandTest : MonoBehaviour {
List<string> listA;
// Use this for initialization
void Start () {
List<string> listB = listA.GetInstance("listB");
List<string> listC = listA;
string msg1 = listB == null ? "listB is null,bad method!" : "listB isn't null,good method!";
string msg2 = listC == null ? "listC is null,please assignment!" : "listC isn't null,go!";
OnDebug(msg1);
OnDebug(msg2);
}
void OnDebug(string msg)
{
Debug.Log("Result: " + msg);
}
}
Result
2. Generic singletons inherited from MonoBehaviour
(Reproduced from: http://blog.csdn.net/honey199396/article/details/48827955 unity3D - single generic template)
Requirements met: a, ensure that script instances are directly available through code; b, the script instances are unique in the scene; c, the script can use methods built into UnityEngine (Start, Update, etc.)
- Generic singleton
using UnityEngine;
public class SingleTon<T>: MonoBehaviour where T :MonoBehaviour {
private static T instance;//Instance set to private for better encapsulation
private static object lockThread = new object();//Thread lock for data security
private static bool isQuit = false;//Type has no instances after being destroyed
public static T GetInstance//Getting instances from attributes
{
get
{
if (isQuit)
{
return null;
}
lock (lockThread)
{
if (instance == null)
{
instance = (T)Object.FindObjectOfType(typeof(T)); //Find first, no more new game objects, Add
if (Object.FindObjectsOfType(typeof(T)).Length > 1)
{
Debug.LogWarning("Too manay " + typeof(T).ToString());//If there is more than one instance, warn and return to the first
return instance;
}
if (instance == null)
{
GameObject singleTon = new GameObject("SingleTon",typeof(T));
instance = singleTon.GetComponent<T>();
DontDestroyOnLoad(singleTon); //Load does not destroy, depending on requirements, this sentence can be erased
}
}
return instance;
}
}
}
public static void OnDestroy()
{
isQuit = true;
}
}
-
Actual Needed Singleton Class
Hang up or hang up
using UnityEngine;
public class SingleTonTest : SingleTon<SingleTonTest> {
//Just test, if the method is called, output it
public void OnPrint()
{
Debug.Log(typeof(SingleTonTest).ToString());
}
}
-
Calls from other classes to singleton classes
Hang on Game Object
using UnityEngine;
public class SingleTonTest1 : MonoBehaviour{
public void Start()
{
SingleTonTest.GetInstance.OnPrint();
}
}
- test result