0
点赞
收藏
分享

微信扫一扫

框架学习:单例属性

若如初梘 2022-01-31 阅读 50
单例模式
#region 单例
    public interface ISingleton
    {

    }
    public abstract class Singleton<T> where T : class, ISingleton
    {
        private static T mInstance;
        public static T Instance
        {
            get
            {
                if (mInstance == null)
                {
                    //MyExtentsion.GetMonoOrPrivateClass支持实例化Mono的挂载类以及私有的普通类
                    mInstance = MyExtension.GetMonoOrPrivateClass<T>();
                }
                return mInstance;
            }
        }
    } 
    #endregion
public static T GetMonoOrPrivateClass<T>() where T : class
        {
            T obj = default(T);
            var type = typeof(T);
            var monoType = typeof(MonoBehaviour);
            //判断是否为Mono挂载类
            if (monoType.IsAssignableFrom(type))
            {
                obj = GetMonoClass<T>();
            }
            else
            {
                obj = GetPriClass<T>();
            }
            return obj;
        }

以下为GetMonoClass分支:

//Mono类的创建,通过属性[后面用到]
[AttributeUsage(AttributeTargets.Class)]
        public class PathInHierary : Attribute
        {
            private string mPath;
            public PathInHierary(string path)
            {
                mPath = path;
            }
            public string Path { get { return mPath; } }
        }
        //例:
        [MyExtension.PathInHierary("GameFramework/Examples/Test11")]
    public class Test1
    {
        
    }
 private static T GetMonoClass<T>() where T : class
        {
            GameObject game = FindGameobject<T>();
            if(game == null)
            {
                game = new GameObject(typeof(T).Name);
            }
            return game.AddComponent(typeof(T)) as T;
        }
        
private static GameObject FindGameobject<T>()
        {
            string path = string.Empty;
            var type = typeof(T);
            //获取前面设定的属性,true表示子类也会被获取
            var cus = type.GetCustomAttributes(true);
            foreach (var c in cus)
            {
                var monoPath = c as PathInHierary;
                if(monoPath != null)
                {
                    path = monoPath.Path;
                }
            }
            //根据设定的路径去获取游戏对象
            GameObject obj = FindGameobject(path);

            return obj;
        }
        
         private static GameObject FindGameobject(string path, bool isNew = true)
        {
            string[] subPaths = path.Split('/');
            GameObject obj = FindGameobject(null,subPaths,0,isNew);
            return obj;
        }

        private static GameObject FindGameobject(GameObject root,string[] subPaths, int index, bool isNew)
        {
            GameObject tmep = null;
            if(root == null)
            {
                tmep = GameObject.Find(subPaths[index]);
            }
            else
            {
                var trans = root.transform.Find(subPaths[index]);
                if(trans != null)
                {
                    tmep = trans.gameObject;
                }
            }
            if (tmep == null)
            {
                if (isNew)
                {
                    tmep = new GameObject(subPaths[index]);
                    if (root != null)
                    {
                        tmep.transform.SetParent(root.transform);
                    }
                }
                else
                {
                    throw new Exception("没有该游戏对象");
                }
            }
            root = tmep;
            return ++index < subPaths.Length ? FindGameobject(root, subPaths, index, isNew) : root;
        }

以下为私有的普通类

private static T GetPriClass<T>() where T : class
        {
            var type = typeof(T);
            var cons = type.GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            var employCon = Array.Find(cons, c => c.GetParameters().Length == 0);
            return employCon.Invoke(null) as T;
        }

使用方法:

public class Test2 : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            Test11 test11 = Test11.Instance;
            Test12 test12 = Test12.Instance;
            test12.Print();
        }

        // Update is called once per frame
        void Update()
        {

        }
    }
    public class Test12 : ISingleton
    {
        public static Test12 Instance => Singleton<Test12>.Instance;
        private Test12()
        {

        }
        public void Print()
        {
            Debug.Log("私有类的输出");
        }
    }
    [MyExtension.PathInHierary("GameFramework/Examples/Test11")]
    public class Test11 : MonoBehaviour, ISingleton
    {
        public static Test11 Instance => Singleton<Test11>.Instance;

        private void Start()
        {
            Debug.Log("成功被创建");
        }
    }
举报

相关推荐

0 条评论