Beispiel #1
0
 private void OnEnable()
 {
     networkableSettings = (NetworkableSettings)target;
     serializedObj       = new SerializedObject(networkableSettings);
 }
Beispiel #2
0
    public static void Initialize(NetworkableSettings networkableSettings, RegisterSerializers registerSerializers)
    {
        Assembly currAssembly = Assembly.GetExecutingAssembly();

        Dictionary <Type, Type> typeToRoot = new Dictionary <Type, Type>();

        List <Type> networkable = new List <Type>();
        List <Type> roots       = new List <Type>();
        List <Type> children    = new List <Type>();
        List <Type> needsDefaultObjectSerializers = new List <Type>();
        List <Type> needsReferenceSerializers     = new List <Type>();
        List <Type> needsValueSerializers         = new List <Type>();

        List <Type> loadResourcesAndInitializeIds = new List <Type>();

        foreach (Type type in currAssembly.GetTypes())
        {
            object[] attributesWithInheritance = type.GetCustomAttributes(true);
            bool     isNetworkableById         = Array.Exists <object>(attributesWithInheritance, attribute => attribute is NetworkableById);
            bool     isNetworkableByValue      = Array.Exists <object>(attributesWithInheritance, attribute => attribute is NetworkableByValue);
            bool     isNetworkable             = (isNetworkableById || isNetworkableByValue);

            Assert.IsFalse(isNetworkableById && isNetworkableByValue, "Class " + type.FullName + " is tagged both as NetworkableByValue and NetworkableById. This is not supported.");

            object[] baseAttributesWithInheritance = (type.BaseType != null ? type.BaseType.GetCustomAttributes(true) : new object[0]);
            bool     isBaseNetworkableById         = Array.Exists <object>(baseAttributesWithInheritance, attribute => attribute is NetworkableById);
            bool     isBaseNetworkableByValue      = Array.Exists <object>(baseAttributesWithInheritance, attribute => attribute is NetworkableByValue);
            bool     isBaseNetworkable             = (isBaseNetworkableById || isBaseNetworkableByValue);

            bool isNetworkableRoot = (isNetworkable && !isBaseNetworkable);

            if (isNetworkable)
            {
                networkable.Add(type);

                if (isNetworkableRoot)
                {
                    roots.Add(type);
                }
                else
                {
                    children.Add(type);
                }

                if (isNetworkableByValue && !type.IsAbstract)
                {
                    needsValueSerializers.Add(type);
                }
                else if (isNetworkableById)
                {
                    needsReferenceSerializers.Add(type);
                }
                else
                {
                    needsDefaultObjectSerializers.Add(type);
                }

                if (isNetworkableById && isNetworkableRoot)
                {
                    loadResourcesAndInitializeIds.Add(type);
                }
            }
        }

        foreach (Type type in networkable)
        {
            Type root = type;
            while (!roots.Exists(rootEntry => rootEntry == root))
            {
                root = root.BaseType;
            }

            typeToRoot[type] = root;
        }

        NetworkableId <Type> .Create(null);

        Debug.Log("========= register networkable classes =====");

        RegisterNetworkableTypes(networkable, networkableSettings.PersistentTypeIds);

        Debug.Log("========= setup NetworkableId registries for root classes =====");

        foreach (Type root in roots)
        {
            RegisterNetworkableIdHierarchy(root, null);
        }

        Debug.Log("========= set Networkableid registries for child classes =====");

        foreach (Type child in children)
        {
            RegisterNetworkableIdHierarchy(child, typeToRoot[child]);
        }

        Debug.Log("========= register byValue serializers =====");

        foreach (Type type in needsValueSerializers)
        {
            registerSerializers.RegisterSerializersForValueType(type);
        }

        Debug.Log("========= register byId serializers =====");

        foreach (Type type in needsReferenceSerializers)
        {
            registerSerializers.RegisterSerializersForIdType(type);
        }

        Debug.Log("========= register default-object serializers =====");

        foreach (Type type in needsDefaultObjectSerializers)
        {
            registerSerializers.RegisterSerializersForDefaultObjectType(type);
        }

        Debug.Log("========= register assets that are by-id and present in asset database =====");

        RegisterNetworkableAssets(networkableSettings.PersistentAssetIds);
    }