예제 #1
0
        /// <summary>
        /// Helper function that uses serialized data from asset bundle to setup prefab
        /// </summary>
        /// <param name="prefab">prefab object to setup</param>
        /// <param name="recordDictionary">serialized data</param>
        /// <returns></returns>
        public GameObject SetupPrefabHelper(GameObject prefab, Dictionary <string, List <SerializedRecord> > recordDictionary)
        {
            if (prefab == null || recordDictionary == null)
            {
                Debug.LogError("Failed to setup prefab - either the prefab or the deserialized dictionary was null - stopping");
                return(null);
            }

            List <Transform> transforms = new List <Transform>();

            ModManager.GetAllChildren(prefab.transform, ref transforms);

            for (int i = 0; i < transforms.Count; i++)
            {
                GameObject go = transforms[i].gameObject;

                if (recordDictionary.ContainsKey(go.name))
                {
                    List <SerializedRecord> records = recordDictionary[go.name];

                    for (int j = 0; j < records.Count; j++)
                    {
                        SerializedRecord    sr = records[j];
                        Component           co = go.AddComponent(sr.componentType);
                        Idfmod_Serializable isCustomSerializable = co as Idfmod_Serializable;

                        if (isCustomSerializable == null)
                        {
                            continue;
                        }
                        if (sr.serializedObjects == null || sr.serializedObjects.Length < 1)
                        {
                            continue;
                        }

                        isCustomSerializable.Deseralized(sr.serializedObjects);
                    }
                }
            }
            return(prefab);
        }
예제 #2
0
        /// <summary>
        /// Serializes components on gameobject & children w/ Idfmod_Serializable interface
        /// WARNING: This will destroy any serialized component from prefab
        /// </summary>
        /// <param name="prefab">base prefab</param>
        /// <param name="serialized">serialized string</param>
        /// <returns></returns>
        private static bool SerializePrefabHelper(GameObject prefab, out string serialized)
        {
            serialized = "";

            Dictionary <string, List <SerializedRecord> > recordDictionary = new Dictionary <string, List <SerializedRecord> >();
            List <Transform> transforms = new List <Transform>();

            ModManager.GetAllChildren(prefab.transform, ref transforms);

            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i] == null)
                {
                    continue;
                }

                GameObject go = transforms[i].gameObject;
                List <SerializedRecord> serializedRecords = new List <SerializedRecord>();
                Component[]             components        = go.GetComponents <Component>();

                for (int j = 0; j < components.Length; j++)
                {
                    if (components[j] == null)
                    {
                        continue;
                    }

                    Component           co            = components[j];
                    Idfmod_Serializable sModInterface = co as Idfmod_Serializable;

                    if (sModInterface == null)
                    {
                        continue;
                    }
                    else if (sModInterface.Ignore)
                    {
                        continue;
                    }

                    object[] toSerialize = sModInterface.ToSerialize();
                    if (toSerialize != null)
                    {
                        for (int k = 0; k < toSerialize.Length; k++)
                        {
                            if (toSerialize[k].GetType().IsSubclassOf(typeof(Component)))
                            {
                                Debug.LogError("Can't serialize monobehaviours: " + toSerialize[k].ToString());
                                return(false);
                            }
                        }
                    }

                    SerializedRecord sr = new SerializedRecord(go.name, co, toSerialize);
                    serializedRecords.Add(sr);
                    UnityEngine.Object.DestroyImmediate(co, true);
                }

                if (serializedRecords.Count > 0)
                {
                    if (recordDictionary.ContainsKey(go.name))
                    {
                        Debug.LogWarning("Please make sure all game objects have unique names, can't serialize objects for: " + go.name);
                        continue;
                    }
                    else
                    {
                        recordDictionary.Add(go.name, serializedRecords);
                    }
                }
            }

            FullSerializer.fsData   sData;
            FullSerializer.fsResult result = ModManager._serializer.TrySerialize(typeof(Dictionary <string, List <SerializedRecord> >), recordDictionary, out sData).AssertSuccessWithoutWarnings();

            serialized = FullSerializer.fsJsonPrinter.PrettyJson(sData);
            return(result.Succeeded);
        }