简单单例模式
void Awake() { m_instance = this; } void Start () {}// Update is called once per framevoid Update () {}}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Army : MonoBehaviour { public static Army m_instance; // Use this for initialization
using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; public class Program: MonoBehaviour { void Start() { IAnimalFactory<Animal> factory = FactoryBuilder.Build("Dog"); Animal a = factory.Create(); } } public class Animal { public Animal() { Debug.Log("a"); } } public class Dog : Animal { public Dog() { Debug.Log("Dog"); } public void show() { Debug.Log("汪汪.."); } } public class Cat : Animal { public Cat() { Debug.Log("Cat"); } } public interface IAnimalFactory<TAnimal> { TAnimal Create(); } public class AnimalIFactory<TAnimalBase, TAnimal> : IAnimalFactory<TAnimalBase> where TAnimal : TAnimalBase, new() { public TAnimalBase Create() { return new TAnimal(); } } public class FactoryBuilder { public static IAnimalFactory<Animal> Build(string type) { if (type == "Dog") { return new AnimalIFactory<Animal, Dog>(); } else if (type == "Cat") { return new AnimalIFactory<Animal, Cat>(); } return null; } }
void Awake() { m_instance = this; } void Start () {}// Update is called once per framevoid Update () {}}
用于场景脚本的管理,可以通过public 把要管理的物品挂在在这里,然后通过 static m_intstance.xxx 进行访问。这个是最简单的单例, static 不好控制生命周期,用多容易出bug
https://www.cnblogs.com/yerongsc/p/4199666.html :在最近unity3d的项目中,为了图方便使用了很多的静态变量,通常是单例类 (public static 类名 xxx )的使用,但是很少有在脚本使用结束后去将静态变量的值置为空,如果该脚本的成员变量中申请过内存空间 比如new byte[]或者存放了一些图片 Resources.load()等,那么即使该脚本对应的gameobject被删除了,这些成员变量中申请过的内存也不会被释放,另为Resource.load的图的内存还需要在调用Resources.UnloadUnusedAssets()后,才能释放。如果成员变量也为静态类型,仅仅将static的类名置空是无用的,还需要将对应的静态变量也置为空。
Unity3d中内存通常出现的ManagedStaticReferences就更静态变量的不规范使用有关系,以后得多注意了。
2 简单工厂
using UnityEngine; using System; using UnityEditor; public class Food<T> { public T type; } /// <summary> /// 没有简单工厂之前 /// </summary> public class Customer: MonoBehaviour { /// <summary> /// 烧菜方法 /// </summary> /// <param name="type"></param> /// <returns></returns> public static Food Cook(string type) { Food food = null; // 吃鸡蛋 自己烧 if (type.Equals("炒鸡蛋")) { food = new ScrambledEggs(); } //又想吃土豆, 还是自己烧 else if(type.Equals("炒土豆")) { food = new ScrambledTomato(); } return food; //switch(type) //{ // case "炒鸡蛋": // food = new ScrambledEggs(); // return food; // break; //} } void Awake() { //做鸡蛋 Food food1 = Cook("炒鸡蛋"); food1.Print(); //做土豆 Food food2 = Cook("炒土豆"); food2.Print(); } void Start() { } } /// <summary> /// 菜抽象类 /// </summary> public abstract class Food { //输出点了什么菜 public abstract void Print(); } /// <summary> /// 炒鸡蛋菜 /// </summary> public class ScrambledEggs : Food { public override void Print() { UnityEngine.Debug.Log("香喷喷的鸡蛋"); } } /// <summary> /// 炒土豆菜 /// </summary> public class ScrambledTomato : Food { public override void Print() { UnityEngine.Debug.Log("香喷喷的土豆"); } }
factory:
using UnityEngine; using System; using UnityEditor; //public class Food<T> //{ // public T type; //} /// <summary> /// 没有简单工厂之前 /// </summary> #region 烧菜 //自己烧 //public class customer : MonoBehaviour //{ // /// <summary> // /// 烧菜方法 自己烧 // /// </summary> // /// <param name="type"></param> // /// <returns></returns> // public static food cook(string type) // { // food food = null; // // 吃鸡蛋 自己烧 // if (type.Equals("炒鸡蛋")) // { // food = new scrambledeggs(); // } // //又想吃土豆, 还是自己烧 // else if (type.Equals("炒土豆")) // { // food = new scrambledtomato(); // } // return food; // //switch(type) // //{ // // case "炒鸡蛋": // // food = new scrambledeggs(); // // return food; // // break; // //} // } // void awake() // { // //做鸡蛋 // food food1 = cook("炒鸡蛋"); // food1.print(); // //做土豆 // food food2 = cook("炒土豆"); // food2.print(); // } // void start() // { // } //} /// <summary> /// 顾客类 向工厂点菜 /// </summary> public class Custom : MonoBehaviour { void Start() { food egg = FoodSimpleFactory.CreateFood("炒鸡蛋"); egg.print(); food tomoto = FoodSimpleFactory.CreateFood("炒土豆"); tomoto.print(); } } /// <summary> /// 简单方法 负责烧菜,工厂烧 /// </summary> public class FoodSimpleFactory { /// <summary> /// 烧菜 /// </summary> /// <param name="type"></param> /// <returns></returns> public static food CreateFood(string type) { food food = null; if (type.Equals("炒鸡蛋")) { food = new scrambledeggs(); } //又想吃土豆, 还是自己烧 else if (type.Equals("炒土豆")) { food = new scrambledtomato(); } return food; } } /// <summary> /// 菜抽象类 /// </summary> public abstract class food { //输出点了什么菜 public abstract void print(); } /// <summary> /// 炒鸡蛋菜 /// </summary> public class scrambledeggs : food { public override void print() { UnityEngine.Debug.Log("香喷喷的鸡蛋"); } } /// <summary> /// 炒土豆菜 /// </summary> public class scrambledtomato : food { public override void print() { UnityEngine.Debug.Log("香喷喷的土豆"); } } /// <summary> /// 简单工厂模式,顾客当客户端,负责点菜, 厨子(工厂) 负责烧菜 /// </summary> #endregion //class Customer : MonoBehaviour //{ //}
泛型 工厂模式