internal static void DuplicateSelectedAssets()
        {
            AssetDatabase.Refresh();
            UnityEngine.Object[] objects = Selection.objects;
            bool flag = true;

            UnityEngine.Object[] array = objects;
            for (int i = 0; i < array.Length; i++)
            {
                UnityEngine.Object @object       = array[i];
                AnimationClip      animationClip = @object as AnimationClip;
                if (animationClip == null || (animationClip.hideFlags & HideFlags.NotEditable) == HideFlags.None || !AssetDatabase.Contains(animationClip))
                {
                    flag = false;
                }
            }
            ArrayList arrayList = new ArrayList();
            bool      flag2     = false;

            if (flag)
            {
                UnityEngine.Object[] array2 = objects;
                for (int j = 0; j < array2.Length; j++)
                {
                    UnityEngine.Object object2        = array2[j];
                    AnimationClip      animationClip2 = object2 as AnimationClip;
                    if (animationClip2 != null && (animationClip2.hideFlags & HideFlags.NotEditable) != HideFlags.None)
                    {
                        string path = AssetDatabase.GetAssetPath(object2);
                        path = Path.Combine(Path.GetDirectoryName(path), animationClip2.name) + ".anim";
                        string        text           = AssetDatabase.GenerateUniqueAssetPath(path);
                        AnimationClip animationClip3 = new AnimationClip();
                        EditorUtility.CopySerialized(animationClip2, animationClip3);
                        AssetDatabase.CreateAsset(animationClip3, text);
                        arrayList.Add(text);
                    }
                }
            }
            else
            {
                UnityEngine.Object[] filtered = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);
                UnityEngine.Object[] array3   = filtered;
                for (int k = 0; k < array3.Length; k++)
                {
                    UnityEngine.Object assetObject = array3[k];
                    string             assetPath   = AssetDatabase.GetAssetPath(assetObject);
                    string             text2       = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    if (text2.Length != 0)
                    {
                        flag2 |= !AssetDatabase.CopyAsset(assetPath, text2);
                    }
                    else
                    {
                        flag2 |= true;
                    }
                    if (!flag2)
                    {
                        arrayList.Add(text2);
                    }
                }
            }
            AssetDatabase.Refresh();
            UnityEngine.Object[] array4 = new UnityEngine.Object[arrayList.Count];
            for (int l = 0; l < arrayList.Count; l++)
            {
                array4[l] = AssetDatabase.LoadMainAssetAtPath(arrayList[l] as string);
            }
            Selection.objects = array4;
        }
示例#2
0
        internal static IEnumerable <Object> DuplicateAssets(IEnumerable <Object> assets)
        {
            AssetDatabase.Refresh();

            var    copiedPaths = new List <string>();
            Object firstDuplicatedObjectToFail = null;

            foreach (var asset in assets)
            {
                var assetPath = AssetDatabase.GetAssetPath(asset);

                // if duplicating a sub-asset, then create a copy next to the main asset file
                if (!String.IsNullOrEmpty(assetPath) && AssetDatabase.IsSubAsset(asset))
                {
                    if (asset is ISubAssetNotDuplicatable)
                    {
                        firstDuplicatedObjectToFail = firstDuplicatedObjectToFail ? firstDuplicatedObjectToFail : asset;
                        continue;
                    }

                    var extension = NativeFormatImporterUtility.GetExtensionForAsset(asset);

                    // We dot sanitize or block unclean the asset filename (asset.name)
                    // since the assertdb will do it for us and has a whole tailored logic for that.

                    // It feels wrong that the asset name (that can apparently contain any char)
                    // is conflated with the orthogonal notion of filename. From the user's POV
                    // it will force an asset dup but with mangled names if the original name contained
                    // "invalid chars" for filenames.
                    // Path.Combine is not used here to avoid blocking asset names that might
                    // contain chars not allowed in filenames.
                    if ((new HashSet <char>(Path.GetInvalidFileNameChars())).Intersect(asset.name).Count() != 0)
                    {
                        Debug.LogWarning(string.Format("Duplicated asset name '{0}' contains invalid characters. Those will be replaced in the duplicated asset name.", asset.name));
                    }

                    var newPath = AssetDatabase.GenerateUniqueAssetPath(
                        string.Format("{0}{1}{2}.{3}",
                                      Path.GetDirectoryName(assetPath),
                                      Path.DirectorySeparatorChar,
                                      asset.name,
                                      extension)
                        );
                    AssetDatabase.CreateAsset(Object.Instantiate(asset), newPath);
                    copiedPaths.Add(newPath);
                }
                // otherwise duplicate the main asset file
                else if (EditorUtility.IsPersistent(asset))
                {
                    var newPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    if (newPath.Length > 0 && AssetDatabase.CopyAsset(assetPath, newPath))
                    {
                        copiedPaths.Add(newPath);
                    }
                }
            }

            if (firstDuplicatedObjectToFail != null)
            {
                var errString = string.Format(
                    "Duplication error: One or more sub assets (with types of {0}) can not be duplicated directly, use the appropriate editor instead",
                    firstDuplicatedObjectToFail.GetType().Name
                    );

                Debug.LogError(errString, firstDuplicatedObjectToFail);
            }

            AssetDatabase.Refresh();

            return(copiedPaths.Select(AssetDatabase.LoadMainAssetAtPath));
        }
示例#3
0
        public static void CreateAsset(DataTable table)
        {
            if (!Enter(table))
            {
                return;
            }

            if (CreateCSharp(table))
            {
                Debug.LogError("Create Scripte! Please Try Again!!!");
                return;
            }
            try
            {
                string script = string.Format("Data{0}", table.TableName);

                string path = string.Format("Assets/Package/Data/{0}.asset", script);

                Assembly assembly = typeof(DataBase).Assembly;

                Type TA = assembly.GetType(string.Format("Data.Data{0}", table.TableName));

                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                ScriptableObject asset = ScriptableObject.CreateInstance(script);

                asset.name = script;

                Type TI = assembly.GetType(string.Format("Data.{0}Information", table.TableName));

                object list = TI.GenerateCollection();

                for (int i = 2; i < row; i++)
                {
                    object target = Activator.CreateInstance(TI);

                    for (int j = 0; j < column; j++)
                    {
                        var field = TI.GetField(table.Rows[0][j].ToString(), BindingFlags.Instance | BindingFlags.Public);

                        object value;

                        switch (table.Rows[1][j].ToString())
                        {
                        case "int[]":
                        {
                            if (!table.Rows[i][j].ToString().TryParseArrayInt(out int[] array_int))
                            {
                            }
                            value = array_int;
                        }
                        break;

                        case "float[]":
                        {
                            if (!table.Rows[i][j].ToString().TryParseArrayFloat(out float[] array_float))
                            {
                            }
                            value = array_float;
                        }
                        break;

                        case "DateTime":
                        {
                            if (!table.Rows[i][j].ToString().TryParseDateTime(out DateTime time))
                            {
                            }
                            value = time;
                        }
                        break;

                        default:
                            value = Convert.ChangeType(table.Rows[i][j], field.FieldType);
                            break;
                        }
                        field.SetValue(target, value);
                    }
                    list.Call("Add", new object[] { target });
                }
                asset.SetMember("list", list);

                AssetDatabase.CreateAsset(asset, path);

                AssetDatabase.Refresh();
            }
            catch (Exception e)
            {
                Debuger.LogException(Author.Data, e);
            }
        }