Unity组件式思想框架 非常好用

Posted sanyejun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity组件式思想框架 非常好用相关的知识,希望对你有一定的参考价值。

改良了几代版本

组件式设计思想

 

using System;
using System.Collections.Generic;
using UnityEngine;

public class Entity
{
    public static Dictionary<Type, object> compDic = new Dictionary<Type, object>();
    
    /// <summary>
    /// 添加组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T AddComp<T>() where T : new()
    {
        Type type = typeof (T);
        T t = (T)Activator.CreateInstance(type);
        
        if (!compDic.ContainsKey(type))
        {
            compDic.Add(type, t);
        }
        else
        {
            Debug.LogError("不能重复添加组件");
        }
        
        return t;
    }
    
    /// <summary>
    /// 添加挂在场景中的MonoBehaviour组件脚本
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T AddMonoComp<T>() where T : MonoBehaviour
    {
        Type type = typeof (T);
        T t = UnityEngine.Object.FindObjectOfType<T>();
        if (t == null)
        {
            GameObject go = new GameObject(typeof(T).ToString());
            t = go.AddComponent<T>();
        }
        if (!compDic.ContainsKey(type))
        {
            compDic.Add(type, t);
        }
        else
        {
            Debug.LogError("不能重复添加组件");
        }

        return t;
    }
    
    /// <summary>
    /// 获取组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T GetComp<T>()
    {
        Type type = typeof (T);
        try
        {
            return (T)compDic[type];
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            throw;
        }
    }
}

 

using System;
using System.Collections.Generic;
using UnityEngine;

public class EntityMono : MonoBehaviour
{
    public Dictionary<Type, object> compDic = new Dictionary<Type, object>();
    
    /// <summary>
    /// 添加组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T AddComp<T>() where T : new()
    {
        Type type = typeof (T);
        T t = (T)Activator.CreateInstance(type);
        
        if (!compDic.ContainsKey(type))
        {
            compDic.Add(type, t);
        }
        else
        {
            Debug.LogError("不能重复添加组件");
        }
        
        return t;
    }
    
    /// <summary>
    /// 添加挂在场景中的MonoBehaviour组件脚本
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T AddMonoComp<T>() where T : MonoBehaviour
    {
        Type type = typeof (T);
        T t = UnityEngine.Object.FindObjectOfType<T>();
        if (t == null)
        {
            GameObject go = new GameObject(typeof(T).ToString());
            t = go.AddComponent<T>();
        }
        if (!compDic.ContainsKey(type))
        {
            compDic.Add(type, t);
        }
        else
        {
            Debug.LogError("不能重复添加组件");
        }

        return t;
    }
    
    /// <summary>
    /// 获取组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetComp<T>()
    {
        Type type = typeof (T);
        try
        {
            return (T)compDic[type];
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            throw;
        }
    }
}

 

实例

技术图片

 

以上是关于Unity组件式思想框架 非常好用的主要内容,如果未能解决你的问题,请参考以下文章

力软框架怎么样

敏捷开发框架 初识组件式开发

敏捷开发框架 初识组件式开发

Unity - Animator组件

Unity - Animator组件

初探响应式编程框架Combine构建的分工协作体系