Exemplo n.º 1
0
        /// <summary>
        /// Checks .resx files and converts them into text assets that can be used at runtime
        /// </summary>
        public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            //Only use this if there's a localization system created
            if (!LocalizationWorkspace.Exists())
            {
                return;
            }

            foreach (string asset in importedAssets)
            {
                if (asset.EndsWith(LocalizationWorkspace.resXFileEnding))
                {
                    string newFileName = LocalizationWorkspace.ResourcesFolderFilePath() + "/" + Path.GetFileNameWithoutExtension(asset) + LocalizationWorkspace.txtFileEnding;

                    if (!DirectoryUtility.CheckAndCreate(LocalizationWorkspace.ResourcesFolderFilePath()))
                    {
                        return;
                    }

                    //Delete the file if it already exists
                    if (FileUtility.Exists(newFileName))
                    {
                        FileUtility.Delete(newFileName);
                    }

                    string fileData = "";
                    using (StreamReader reader = new StreamReader(asset))
                    {
                        fileData = reader.ReadToEnd();
                    }


                    FileUtility.WriteToFile(newFileName, fileData);

                    SmartCultureInfoCollection allCultures = SmartCultureInfoEx.Deserialize(LocalizationWorkspace.CultureInfoCollectionFilePath());
                    LanguageHandlerEditor.CheckAndSaveAvailableLanguages(allCultures);

                    AssetDatabase.Refresh(ImportAssetOptions.Default);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary> Copies the file into the resources folder. Naming the new asset to KEY </summary>
        public static string CopyFileIntoResources(SerializableLocalizationObjectPair objectPair, SmartCultureInfo thisCultureInfo)
        {
            if (!DirectoryUtility.CheckAndCreate(LocalizationWorkspace.LanguageRuntimeFolderPath(thisCultureInfo.languageCode)))
            {
                return("");
            }


            string          newFileName      = objectPair.keyValue;
            string          filePath         = string.Empty;
            string          currentAssetPath = string.Empty;
            LocalizedObject objectToCopy     = objectPair.changedValue;

            if (objectToCopy.ObjectType == LocalizedObjectType.AUDIO && objectToCopy.ThisAudioClip != null)
            {
                filePath         = LocalizationWorkspace.LanguageAudioFolderPath(thisCultureInfo.languageCode);
                currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisAudioClip);
            }
            else if (objectToCopy.ObjectType == LocalizedObjectType.TEXTURE && objectToCopy.ThisTexture != null)
            {
                filePath         = LocalizationWorkspace.LanguageTexturesFolderPath(thisCultureInfo.languageCode);
                currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisTexture);
            }
            else if (objectToCopy.ObjectType == LocalizedObjectType.GAME_OBJECT && objectToCopy.ThisGameObject != null)
            {
                filePath         = LocalizationWorkspace.LanguagePrefabsFolderPath(thisCultureInfo.languageCode);
                currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisGameObject);
            }
            else if (objectToCopy.ObjectType == LocalizedObjectType.TEXT_ASSET && objectToCopy.ThisTextAsset != null)
            {
                filePath         = LocalizationWorkspace.LanguageTextAssetsFolderPath(thisCultureInfo.languageCode);
                currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisTextAsset);
            }
            else if (objectToCopy.ObjectType == LocalizedObjectType.FONT && objectToCopy.Font != null)
            {
                filePath         = LocalizationWorkspace.LanguageFontsFolderPath(thisCultureInfo.languageCode);
                currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.Font);
            }
            else
            {
                return(string.Empty);
            }

            if (!DirectoryUtility.CheckAndCreate(filePath))
            {
                return("");
            }

            //Get the fileExtension of the asset
            string fileExtension = FileUtility.GetFileExtension(Application.dataPath + currentAssetPath);

            if (objectToCopy.ObjectType != LocalizedObjectType.GAME_OBJECT)
            {
                //Copy or replace the file to the new path
                FileUtil.ReplaceFile(currentAssetPath, filePath + "/" + newFileName + fileExtension);

                string metaFile = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) +
                                  currentAssetPath.Substring(0, currentAssetPath.Length - fileExtension.Length) + fileExtension + ".meta";
                if (File.Exists(metaFile))
                {
                    FileUtil.ReplaceFile(metaFile, filePath + "/" + newFileName + fileExtension + ".meta");
                }
            }
            else
            {
                string relativePath = filePath + "/" + newFileName + fileExtension;
                relativePath = "Assets" + relativePath.Substring(Application.dataPath.Length);
                //1PrefabUtility.CreatePrefab(relativePath, objectToCopy.ThisGameObject);
                PrefabUtility.SaveAsPrefabAsset(objectToCopy.ThisGameObject, relativePath);             // TODO Перепроверить
            }

            return(AssetDatabase.AssetPathToGUID(currentAssetPath));
        }