コード例 #1
0
ファイル: CFGUtil.cs プロジェクト: ChrisAdderley/B9PartSwitch
        public static void AssignConfigObject(ConfigFieldInfo field, ConfigNode value, ref IConfigNode result)
        {
            if (!field.ElementType.DerivesFrom(typeof(IConfigNode)))
            {
                throw new ArgumentException("Element type of field " + field.Name + " does not derive from IConfigNode");
            }

            if (result == null)
            {
                if (field.ElementType.DerivesFrom(typeof(Component)) && field.Parent is Component)
                {
                    result = (field.Parent as Component).gameObject.AddComponent(field.ElementType) as IConfigNode;
                }
                else if (field.ElementType.DerivesFrom(typeof(ScriptableObject)))
                {
                    result = ScriptableObject.CreateInstance(field.ElementType) as IConfigNode;
                }
                else
                {
                    try
                    {
                        result = (IConfigNode)Activator.CreateInstance(field.ElementType);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("Error: Could not load field '" + field.Name + "' because an instance of " + field.ElementType.FullName + "could not be created: " + e.Message);
                        return;
                    }
                }
            }

            result.Load(value);
        }
コード例 #2
0
ファイル: CFGUtil.cs プロジェクト: ChrisAdderley/B9PartSwitch
        public static void AssignConfigObject(ConfigFieldInfo field, string value, ref object result)
        {
            object parseResult = CFGUtil.ParseConfigValue(field.ElementType, value);

            if (parseResult == null)
            {
                return;
            }

            if (result.IsNotNull() && (result is UnityEngine.Object) && field.Attribute.destroy)
            {
                UnityEngine.Object.Destroy((UnityEngine.Object)result);
            }

            result = parseResult;
            return;
        }