예제 #1
0
        public static string[] GetUserData(this UnityEditor.AssetImporter importer, string param)
        {
            string userData = importer.userData;

            if (userData == null)
            {
                return(null);
            }
            if (userData.Length == 0)
            {
                return(new string[0]);
            }

            string[] userDataSplit = userData.Split('\n', ';');
            for (int i = 0; i < userDataSplit.Length; i++)
            {
                if (userDataSplit[i].StartsWith(param + ":"))
                {
                    userDataSplit[i] = userDataSplit[i].Remove(0, param.Length + 1);
                    return(userDataSplit[i].Split(','));
                }
            }

            return(new string[0]);
        }
        public static void Rewrite(ref Texture2DArray texArr, Texture2DArray newArr)
        {
                        #if UNITY_EDITOR
            bool isSelected = UnityEditor.Selection.activeGameObject == texArr;

            UnityEditor.AssetImporter texImporter = texArr.GetImporter();
            if (texImporter == null)
            {
                return;
            }

            //string userData = texImporter.userData; //do not owerwrite userdata
            UnityEditor.EditorUtility.CopySerialized(newArr, texArr);
            UnityEditor.EditorUtility.SetDirty(texArr);
            UnityEditor.AssetDatabase.SaveAssets();

            if (isSelected)
            {
                UnityEditor.Selection.activeObject = texArr;
            }


            //texArr = UnityEditor.AssetDatabase.LoadAssetAtPath<Texture2DArray>(path);
            //texArr.GetImporter().userData = userData;
                        #endif
        }
        public static void LinkTexture(string texGuid, string arrGuid, bool isAlpha = false)
        {
                        #if UNITY_EDITOR
            string userDataTag = "TexArr_textureArray_as" + (isAlpha? "Source" : "Alpha");
            UnityEditor.AssetImporter importer = AssetsExtensions.GetImporter(texGuid);

            if (importer == null)
            {
                Debug.Log("Could not get importer for " + UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid) + " to assign texture");
                return;
            }

            string[] texData = AssetsExtensions.GetUserData(importer, userDataTag);
            if (!texData.Contains(arrGuid))             //not already signed as used
            {
                ArrayTools.Add(ref texData, arrGuid);
                AssetsExtensions.SetUserData(texGuid, userDataTag, texData, reload: false);
            }

            //writing hash for the newly assigned textures
            string    path    = UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid);
            Texture2D tex     = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(path);
            string    texHash = tex.GetHash().ToString();                                            //tex.imageContentsHash.ToString();
            AssetsExtensions.SetUserData(importer, "Hash", new string[] { texHash }, reload: false); //will write only if hash changed
                        #endif
        }
예제 #4
0
        // 为了模拟Resources.Load不输入扩展名,折腾下
        public static void Load(string name, Type type, Action <UnityEngine.Object> cb = null)
        {
#if UNITY_EDITOR
            Dictionary <string, bool> spriteDic = EditLoadCheck.GetSpriteDic();


            // 拆分路径名和文件名
            string file = Path.GetFileNameWithoutExtension(name);
            string path = Path.GetDirectoryName(name);
            // 添加资源路径
            string updata_path = "Assets/res_updata/" + path;
            path = "Assets/res/" + path;

            // 去掉路径最后的路径分隔符
            path = path.TrimEnd(new char[2] {
                '/', '\\'
            });
            updata_path = updata_path.TrimEnd(new char[2] {
                '/', '\\'
            });
            UnityEngine.Object obj = null;

            string[] assets = UnityEditor.AssetDatabase.FindAssets(file);

            for (int i = 0; i < assets.Length; ++i)
            {
                string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assets[i]);

                path        = path.Replace('/', '\\');
                updata_path = updata_path.Replace('/', '\\');

                if (Path.GetFileNameWithoutExtension(assetPath) != file || (path != Path.GetDirectoryName(assetPath) && updata_path != Path.GetDirectoryName(assetPath)))
                {
                    continue;
                }

                obj = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, type);
                if (obj != null)
                {
                    UnityEditor.AssetImporter ai = UnityEditor.AssetImporter.GetAtPath(assetPath);
                    if (ai != null && !spriteDic.ContainsKey(ai.assetBundleName) && obj.GetType() == typeof(Sprite))
                    {
                        Debug.LogErrorFormat("Fond Sprite[{0}] but not set ab name[{1}]", assetPath, ai.assetBundleName);
                        obj = null;
                        break;
                    }
                    break;
                }
            }
            cb(obj);
            if (obj == null)
            {
                Debug.LogError("No " + name);
            }
#else
            Debug.LogWarning("Flz.Core.Core.Load only in Editor mode");
            return;
#endif
        }
예제 #5
0
 public static void SetUserData(string guid, string param, string[] data, bool reload = false)
 {
                 #if UNITY_EDITOR
     UnityEditor.AssetImporter importer = GetImporter(guid);
     if (importer == null)
     {
         return;
     }
     SetUserData(importer, param, data, reload);
                 #else
     Debug.LogError("SetUserData does not work in build");
                 #endif
 }
예제 #6
0
 public static string[] GetUserData(string guid, string param)
 {
                 #if UNITY_EDITOR
     UnityEditor.AssetImporter importer = GetImporter(guid);
     if (importer == null)
     {
         return(null);
     }
     return(GetUserData(importer, param));
                 #else
     Debug.LogError("GetUserData does not work in build");
     return(null);
                 #endif
 }
예제 #7
0
 public static string[] GetUserData(this UnityEngine.Object obj, string param)
 {
                 #if UNITY_EDITOR
     UnityEditor.AssetImporter importer = obj.GetImporter();
     if (importer == null)
     {
         return(null);
     }
     return(GetUserData(importer, param));
                 #else
     Debug.LogError("GetUserData does not work in build");
     return(null);
                 #endif
 }
예제 #8
0
 public static void Reimport(this UnityEngine.Object obj)
 {
                 #if UNITY_EDITOR
     string path = UnityEditor.AssetDatabase.GetAssetPath(obj);
     if (path == null || path.Length == 0)
     {
         return;
     }
     UnityEditor.AssetImporter importer = UnityEditor.AssetImporter.GetAtPath(path);
     importer.userData = importer.userData;
     UnityEditor.EditorUtility.SetDirty(importer);
     importer.SaveAndReimport();
                 #else
     Debug.LogError("Reimport does not work in build");
                 #endif
 }
        public static void UnlinkTexture(string texGuid, string arrGuid, bool isAlpha = false)
        {
                        #if UNITY_EDITOR
            string userDataTag = "TexArr_textureArray_as" + (isAlpha? "Source" : "Alpha");
            UnityEditor.AssetImporter importer = AssetsExtensions.GetImporter(texGuid);

            if (importer == null)
            {
                Debug.Log("Could not get importer for " + UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid) + " to unassign texture");
                return;
            }

            string[] prevTexData = AssetsExtensions.GetUserData(importer, userDataTag);
            if (prevTexData == null || prevTexData.Length == 0)
            {
                //Debug.Log("Previous texture has no data assigned");
                return;
            }
            ArrayTools.RemoveAll(ref prevTexData, arrGuid);
            AssetsExtensions.SetUserData(importer, userDataTag, prevTexData, reload: false);
                        #endif
        }
예제 #10
0
        public static void SetUserData(this UnityEditor.AssetImporter importer, string param, string[] data, bool reload = false)
        {
            char endline = '\n';             //';'

            string userData = importer.userData;

            string[] userDataSplit = userData.Split('\n', ';');

            //preparing new data line
            if (data == null)
            {
                data = new string[0];
            }
            string newDataString = param + ":" + data.ToStringMemberwise(separator: ",");

            //param line number (-1 if not found)
            int numInSplit = -1;

            for (int i = 0; i < userDataSplit.Length; i++)
            {
                if (userDataSplit[i].StartsWith(param + ":"))
                {
                    numInSplit = i;
                }
            }

            //erasing empty data
            if (numInSplit >= 0 && data.Length == 0)
            {
                ArrayTools.RemoveAt(ref userDataSplit, numInSplit);
            }

            //replacing line
            if (numInSplit >= 0 && data.Length != 0)
            {
                userDataSplit[numInSplit] = newDataString;
            }

            //adding new line
            if (numInSplit == -1 && data.Length != 0)
            {
                ArrayTools.Add(ref userDataSplit, newDataString);
            }

            //to string
            string newUserData = "";

            for (int i = 0; i < userDataSplit.Length; i++)
            {
                if (userDataSplit[i].Length == 0)
                {
                    continue;
                }
                newUserData += userDataSplit[i];
                if (i != userDataSplit.Length - 1)
                {
                    newUserData += endline;
                }
            }

            //writing
            if (newUserData != userData)
            {
                importer.userData = newUserData;

                UnityEditor.EditorUtility.SetDirty(importer);
                UnityEditor.AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath);
                if (reload)
                {
                    UnityEditor.AssetDatabase.Refresh();
                }
            }
        }
예제 #11
0
 public virtual void ApplySettings(UnityEditor.AssetImporter importer)
 {
 }