Beispiel #1
0
    private static void PopulateObject(IList <FieldInfo> cache, SRMonoBehaviourEx instance, bool justSet)
    {
        for (var i = 0; i < cache.Count; i++)
        {
            var f = cache[i];

            if (!EqualityComparer <Object> .Default.Equals(f.Field.GetValue(instance), null))
            {
                continue;
            }

            // If import is enabled, use SRServiceManager to import the reference
            if (f.Import)
            {
                var t = f.ImportType ?? f.Field.FieldType;

                var service = SRServiceManager.GetService(t);

                if (service == null)
                {
                    Debug.LogWarning("Field {0} import failed (Type {1})".Fmt(f.Field.Name, t));
                    continue;
                }

                f.Field.SetValue(instance, service);

                continue;
            }

            // If autoset is enabled on field, try and find the component on the GameObject

            if (f.AutoSet)
            {
                var newValue = instance.GetComponent(f.Field.FieldType);

                if (!EqualityComparer <Object> .Default.Equals(newValue, null))
                {
                    f.Field.SetValue(instance, newValue);
                    continue;
                }
            }

            if (justSet)
            {
                continue;
            }

            if (f.AutoCreate)
            {
                var newValue = instance.CachedGameObject.AddComponent(f.Field.FieldType);
                f.Field.SetValue(instance, newValue);
            }

            throw new UnassignedReferenceException(
                      "Field {0} is unassigned, but marked with RequiredFieldAttribute".Fmt(f.Field.Name));
        }
    }
Beispiel #2
0
    private static void CheckFields(SRMonoBehaviourEx instance, bool justSet = false)
    {
        if (_checkedFields == null)
        {
            _checkedFields = new Dictionary <Type, IList <FieldInfo> >();
        }

        var t = instance.GetType();

        IList <FieldInfo> cache;

        if (!_checkedFields.TryGetValue(instance.GetType(), out cache))
        {
            cache = ScanType(t);

            _checkedFields.Add(t, cache);
        }

        PopulateObject(cache, instance, justSet);
    }