Example #1
0
        public override void Draw(object input, string name, PropertyInfo property, Action <object> setValueCallback)
        {
            string value = (string)input ?? "";
            string path  = AssetDatabase.GUIDToAssetPath(value);

            UnityEngine.Object asset = AssetDatabase.LoadMainAssetAtPath(path);

            object[] attr = property.GetCustomAttributes(typeof(AssetReferenceAttribute), true);
            AssetReferenceAttribute drawerAttr = attr[0] as AssetReferenceAttribute;

            if (drawerAttr != null)
            {
                switch (drawerAttr.AssetType)
                {
                case AssetReferenceAttribute.AssetReferenceType.Material:
                    asset = EditorGUILayout.ObjectField(name, asset, typeof(Material), false);
                    break;

                case AssetReferenceAttribute.AssetReferenceType.AudioClip:
                    asset = EditorGUILayout.ObjectField(name, asset, typeof(AudioClip), false);
                    break;

                case AssetReferenceAttribute.AssetReferenceType.GameObject:
                    asset = EditorGUILayout.ObjectField(name, asset, typeof(GameObject), false);
                    break;

                case AssetReferenceAttribute.AssetReferenceType.Texture:
                    asset = EditorGUILayout.ObjectField(name, asset, typeof(Texture), false);
                    break;

                case AssetReferenceAttribute.AssetReferenceType.Animation:
                    asset = EditorGUILayout.ObjectField(name, asset, typeof(AnimationClip), false);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            path  = AssetDatabase.GetAssetPath(asset);
            value = AssetDatabase.AssetPathToGUID(path);
            setValueCallback(value);
        }
        private static void ExtractBundleReference(string inDataBundleName, object target,
                                                   ref Dictionary <string, Bundle> dataBundles)
        {
            PropertyInfo[]             properties          = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            IEnumerable <PropertyInfo> referenceProperties = from property in properties
                                                             where property.GetCustomAttributes(typeof(AssetReferenceAttribute), true).Length > 0
                                                             select property;

            foreach (PropertyInfo property in referenceProperties)
            {
                if (property.PropertyType == typeof(string))
                {
                    string guid = property.GetValue(target, null) as string;
                    if (string.IsNullOrEmpty(guid))
                    {
                        continue;
                    }

                    string path = AssetDatabase.GUIDToAssetPath(guid);
                    if (string.IsNullOrEmpty(path))
                    {
                        continue;
                    }
                    AssetReferenceAttribute attribute =
                        property.GetCustomAttributes(typeof(AssetReferenceAttribute), true)[0] as AssetReferenceAttribute;
                    bool customBundle = !string.IsNullOrEmpty(attribute?.BundleOverride);

                    Bundle currentBundle;
                    if (!customBundle)
                    {
                        currentBundle = dataBundles[inDataBundleName];
                    }
                    else
                    {
                        if (dataBundles.ContainsKey(attribute.BundleOverride))
                        {
                            currentBundle = dataBundles[attribute.BundleOverride];
                        }
                        else
                        {
                            dataBundles[attribute.BundleOverride] =
                                currentBundle = new Bundle(attribute.BundleOverride, new List <string>(), new List <string>());
                        }

                        // make the parent bundle depend on this custom bundle
                        dataBundles[inDataBundleName].Dependecies.Add(currentBundle.Name.ToLower());
                    }

                    currentBundle.Guids.Add(guid);
                    currentBundle.AssetPaths.Add(path);
                }
            }

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.IsPrimitive)
                {
                    continue;
                }
                object subTarget = property.GetValue(target, null);
                if (subTarget == null)
                {
                    continue;
                }

                if (subTarget.GetType().IsArray)
                {
                    Array array = (Array)subTarget;
                    for (int i = 0; i < array.Length; i++)
                    {
                        ExtractBundleReference(inDataBundleName, array.GetValue(i), ref dataBundles);
                    }
                }
                else
                {
                    ExtractBundleReference(inDataBundleName, subTarget, ref dataBundles);
                }
            }
        }