private bool AddComponentDataOnEntityAdded <T>(ref Entity entity, out ComponentDataContainer componentDataContainer) where T : struct, IComponentData
    {
        componentDataContainer = null;
        if (EntityManager.HasComponent <T>(entity))
        {
            ComponentType     componentType     = ComponentType.Create <T>();
            int               numberOfMembers   = reflectionUtility.GetNumberOfMembers(componentType.GetManagedType());
            Entity            networkDataEntity = networkFactory.CreateNetworkComponentData <T>(entity, numberOfMembers);
            NativeArray <int> values            = networkFactory.NetworkEntityManager.GetFixedArray <int>(networkDataEntity);
            PostUpdateCommands.AddComponent(entity, new NetworkComponentState <T>());

            T component = EntityManager.GetComponentData <T>(entity);
            NetworkMemberInfo[]        networkMemberInfos   = reflectionUtility.GetNetworkMemberInfo(componentType);
            List <MemberDataContainer> memberDataContainers = new List <MemberDataContainer>();
            for (int i = 0; i < numberOfMembers; i++)
            {
                int value = (networkMemberInfos[i] as NetworkMemberInfo <T>).GetValue(component);
                memberDataContainers.Add(new MemberDataContainer()
                {
                    MemberId = i,
                    Data     = value
                });
                values[i] = value;
            }

            componentDataContainer = new ComponentDataContainer()
            {
                ComponentTypeId = reflectionUtility.GetComponentTypeID(componentType),
                MemberData      = memberDataContainers
            };
            return(true);
        }
        return(false);
    }
    void AddComponent <T>(Entity entity, List <MemberDataContainer> memberDataContainers) where T : struct, IComponentData
    {
        //Debug.Log(typeof(T));
        int numberOfMembers = reflectionUtility.GetNumberOfMembers(typeof(T));

        NetworkMemberInfo[] networkMemberInfos = reflectionUtility.GetNetworkMemberInfo(ComponentType.Create <T>());
        if (!EntityManager.HasComponent <T>(entity))
        {
            T component = new T();
            for (int i = 0; i < memberDataContainers.Count; i++)
            {
                int value = memberDataContainers[i].Data;
                (networkMemberInfos[i] as NetworkMemberInfo <T>).SetValue(ref component, value, value, Time.deltaTime, NetworkSendSystem.SendInterval, networkEntityMap);
            }
            PostUpdateCommands.AddComponent(entity, component);
        }

        if (!EntityManager.HasComponent <NetworkComponentState <T> >(entity))
        {
            Entity syncEntity          = networkFactory.CreateNetworkComponentData <T>(entity, numberOfMembers);
            DynamicBuffer <int> values = EntityManager.GetBuffer <NetworkValue>(syncEntity).Reinterpret <int>();
            for (int i = 0; i < memberDataContainers.Count; i++)
            {
                int index = i * 2;
                values[index]     = memberDataContainers[i].Data;
                values[index + 1] = memberDataContainers[i].Data;
            }
            PostUpdateCommands.AddComponent(entity, new NetworkComponentState <T>());
        }
    }
Esempio n. 3
0
    void AddComponent <T>(Entity entity, List <ComponentField> memberDataContainers) where T : struct, IComponentData
    {
        //Debug.Log(typeof(T));
        int numberOfMembers = reflectionUtility.GetFieldsCount(typeof(T));
        var infos           = reflectionUtility.GetFields(ComponentType.Create <T>());

        if (!EntityManager.HasComponent <T>(entity))
        {
            T component = new T();
            for (int i = 0; i < memberDataContainers.Count; i++)
            {
                int value = memberDataContainers[i].Value;
                (infos[i] as NetworkField <T>)
                .SetValue(ref component, value, value, Time.deltaTime, NetworkSendSystem.SendInterval);
            }
            PostUpdateCommands.AddComponent(entity, component);
        }
        if (EntityManager.HasComponent <NetworkComponentState <T> >(entity))
        {
            return;
        }

        var syncEntity = networkFactory.CreateNetworkComponentData <T>(entity, numberOfMembers);
        var values     = networkFactory.NetworkEntityManager.GetFixedArray <int>(syncEntity);

        for (int i = 0; i < memberDataContainers.Count; i++)
        {
            int index = i * 2;
            values[index]     = memberDataContainers[i].Value;
            values[index + 1] = memberDataContainers[i].Value;
        }
        PostUpdateCommands.AddComponent(entity, new NetworkComponentState <T>());
    }