static void _MakeAtlas() { //Debug.Log(string.Format("UI Texture To Prefab")); string spriteDir = Application.dataPath + "/Resources/UI/Sprites/"; var filesPath = GKFileUtil.GetFilesInDirectory(Application.dataPath + "/Art/_UI/Sprite2Prefab/"); foreach (var str in filesPath) { if (string.Empty != GKString.GetFromSuffix(str, ".png", true) || string.Empty != GKString.GetFromSuffix(str, ".jpg", true)) { string allPath = str; string assetPath = allPath.Substring(allPath.IndexOf("Assets")); Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath); GameObject go = new GameObject(sprite.name); go.AddComponent <SpriteRenderer>().sprite = sprite; var dir = GKFileUtil.GetDirctoryName(allPath); allPath = string.Format("{0}{1}/{2}.prefab", spriteDir, dir, sprite.name); GKFileUtil.CreateDirectoryFromFileName(allPath); string prefabPath = allPath.Substring(allPath.IndexOf("Assets")); PrefabUtility.CreatePrefab(prefabPath, go); GameObject.DestroyImmediate(go); } } }
static public void CreateAsset(UnityEngine.Object asset, string filename) { GKFileUtil.CreateDirectoryFromFileName(filename); Debug.Log("Create Asset File " + filename); AssetDatabase.CreateAsset(asset, filename); }
//! create prefab by filename and keep prefab connection if previous prefab exists static public GameObject CreateOrReplacePrefab(string filename, GameObject obj) { //Debug.Log("CreateOrReplacePrefab " + filename ); var prefab = AssetDatabase.LoadMainAssetAtPath(filename) as GameObject; if (prefab == null) { GKFileUtil.CreateDirectoryFromFileName(filename); return(PrefabUtility.CreatePrefab(filename, obj)); } else { var o = PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ReplaceNameBased); if (!o) { Debug.LogError("Cannot Replace Prefab " + filename); } return(o); } }
static public void CopyAsset(string srcPath, string dstPath) { if (!File.Exists(srcPath)) { Debug.LogError(string.Format("The source file cannot be found. file path: {0} ", srcPath)); return; } GKFileUtil.CreateDirectoryFromFileName(dstPath); // Copy asset to destination directory. File.Copy(srcPath, dstPath, true); string dstMetaPath = string.Format("{0}.meta", dstPath); string dstMetaGuid = null; string dstMetaTime = null; //Read the guid and timeCreated in the file. if (File.Exists(dstMetaPath)) { using (StreamReader sr = new StreamReader(dstMetaPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Contains("guid")) { dstMetaGuid = line; } else if (line.Contains("timeCreated")) { dstMetaTime = line; break; } } sr.Close(); } } // Copy meta to destination directory. File.Copy(string.Format("{0}.meta", srcPath), dstMetaPath, true); // Modify meta data. StringBuilder sb = new StringBuilder(); using (StreamReader sr = new StreamReader(dstMetaPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Contains("guid")) { #if UNITY_2017_1_OR_NEWER sb.AppendLine(dstMetaGuid ?? "guid: " + GUID.Generate().ToString()); #else sb.AppendLine(dstMetaGuid ?? "guid: " + Guid.NewGuid().ToString()); #endif } else if (line.Contains("timeCreated")) { sb.AppendLine(dstMetaTime ?? "timeCreated: " + GetGreenwichTime()); } else { sb.AppendLine(line); } } sr.Close(); } using (StreamWriter sw = new StreamWriter(dstMetaPath)) { sw.Write(sb.ToString()); sw.Close(); } }