Ejemplo n.º 1
0
    public CComponent AddCComponent(Type type)
    {
        Type       baseType     = GetBaseType(type);
        CComponent oldComponent = null;

        if (m_ComponentMap.TryGetValue(baseType, out oldComponent))
        {
            //判断新的组件是否由旧的组件派生
            Type newType = type;
            if (oldComponent.GetType().IsAssignableFrom(newType) && oldComponent.GetType() != type)
            {
                //删除旧的组件并添加新的组件
                RemoveCComponent(oldComponent);
            }
            else
            {
                //不用添加新的组件,因为新的组件的派生组件已在添加到实体里
                return(GetCComponent(type));
            }
        }

        CComponent component = CComponent.CreateComponent(type, this);

        m_ComponentMap.Add(baseType, component);
        if (entityState == EntityState.Alive)
        {
            component.InitializeComplete();
        }
        return(component);
    }
Ejemplo n.º 2
0
    public void AddCom(CComponent com)
    {
        if (com == null)
        {
            Debug.LogError("CObject AddComponent error! com == null");
            return;
        }
        if (mGameObj == null)
        {
            mGameObj = com.gameObject.transform.parent.gameObject;
        }
        else
        {
            if (mGameObj != com.gameObject.transform.parent.gameObject)
            {
                Debug.LogError("CObject AddComponent error! com.gameObject name:" + com.gameObject.transform.parent.gameObject.name);
                return;
            }
        }
        com.cobj = this;
        mComList.Add(com);
        // really add to game object
//        mGameObj.AddComponent(com.GetType());
        mComDict[com.GetType().Name] = com;

        com.OnAttach();
    }
Ejemplo n.º 3
0
 public void RemoveCComponent(CComponent component)
 {
     if (component != null)
     {
         Type       baseType = GetBaseType(component.GetType());
         CComponent c        = null;
         if (m_ComponentMap.TryGetValue(baseType, out c))
         {
             c.BeginDestroy();
             m_ComponentMap.Remove(baseType);
         }
     }
 }