Beispiel #1
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var        importer = new StyleSheetImporterImpl();
            string     contents = File.ReadAllText(ctx.assetPath);
            StyleSheet asset    = ScriptableObject.CreateInstance <StyleSheet>();

            asset.hideFlags = HideFlags.NotEditable;

            if (importer.Import(asset, contents))
            {
                ctx.AddObjectToAsset("stylesheet", asset);

                // Force the pre processor to rebuild its list of referenced asset paths
                // as paths may have changed with this import.
                StyleSheetAssetPostprocessor.ClearReferencedAssets();

                // Clear the style cache to force all USS rules to recompute
                // and subsequently reload/reimport all images.
                StyleContext.ClearStyleCache();
            }
            else
            {
                foreach (string importError in importer.errors.FormatErrors())
                {
                    Debug.LogError(importError);
                }
            }
        }
        internal static Object LoadResource(string pathName, System.Type type, bool lookForRetinaAssets)
        {
            Object resource  = null;
            string hiResPath = string.Empty;

            lookForRetinaAssets &= (type == typeof(Texture2D));

            if (lookForRetinaAssets)
            {
                string ext         = Path.GetExtension(pathName);
                string fileRenamed = Path.GetFileNameWithoutExtension(pathName) + "@2x" + ext;
                hiResPath           = Path.Combine(Path.GetDirectoryName(pathName), fileRenamed);
                lookForRetinaAssets = !string.IsNullOrEmpty(hiResPath);
            }

            if (lookForRetinaAssets)
            {
                resource = EditorGUIUtility.Load(hiResPath);
            }

            if (resource == null)
            {
                resource = EditorGUIUtility.Load(pathName);
            }

            if (resource == null && lookForRetinaAssets)
            {
                resource = Resources.Load(hiResPath, type);
            }

            if (resource == null)
            {
                resource = Resources.Load(pathName, type);
            }

            if (resource == null && lookForRetinaAssets)
            {
                resource = AssetDatabase.LoadMainAssetAtPath(hiResPath);
            }

            if (resource == null)
            {
                resource = AssetDatabase.LoadMainAssetAtPath(pathName);
            }

            if (resource != null)
            {
                Debug.Assert(type.IsAssignableFrom(resource.GetType()), "Resource type mismatch");

                // Add to post processor for tracking asset moves/deletes.
                var absoluteAssetPath = AssetDatabase.GetAssetPath(resource);
                if (type != typeof(UnityEngine.StyleSheets.StyleSheet) && !absoluteAssetPath.StartsWith("Library"))
                {
                    StyleSheetAssetPostprocessor.AddReferencedAssetPath(absoluteAssetPath);
                }
            }

            return(resource);
        }
Beispiel #3
0
 private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
 {
     if (StyleSheetAssetPostprocessor.s_StyleSheetReferencedAssetPaths != null)
     {
         for (int i = 0; i < deletedAssets.Length; i++)
         {
             string assetPath = deletedAssets[i];
             StyleSheetAssetPostprocessor.ProcessAssetPath(assetPath);
         }
         for (int j = 0; j < movedAssets.Length; j++)
         {
             StyleSheetAssetPostprocessor.ProcessAssetPath(movedAssets[j]);
             StyleSheetAssetPostprocessor.ProcessAssetPath(movedFromAssetPaths[j]);
         }
     }
 }
        internal static UnityEngine.Object LoadResource(string pathName, Type type, bool lookForRetinaAssets)
        {
            UnityEngine.Object @object = null;
            string             text    = string.Empty;

            lookForRetinaAssets &= (type == typeof(Texture2D));
            if (lookForRetinaAssets)
            {
                string extension = Path.GetExtension(pathName);
                string path      = Path.GetFileNameWithoutExtension(pathName) + "@2x" + extension;
                text = Path.Combine(Path.GetDirectoryName(pathName), path);
                lookForRetinaAssets = !string.IsNullOrEmpty(text);
            }
            if (lookForRetinaAssets)
            {
                @object = EditorGUIUtility.Load(text);
            }
            if (@object == null)
            {
                @object = EditorGUIUtility.Load(pathName);
            }
            if (@object == null && lookForRetinaAssets)
            {
                @object = Resources.Load(text, type);
            }
            if (@object == null)
            {
                @object = Resources.Load(pathName, type);
            }
            if (@object == null && lookForRetinaAssets)
            {
                @object = AssetDatabase.LoadMainAssetAtPath(text);
            }
            if (@object == null)
            {
                @object = AssetDatabase.LoadMainAssetAtPath(pathName);
            }
            if (@object != null)
            {
                string assetPath = AssetDatabase.GetAssetPath(@object);
                if (type != typeof(StyleSheet) && !assetPath.StartsWith("Library"))
                {
                    StyleSheetAssetPostprocessor.AddReferencedAssetPath(assetPath);
                }
            }
            return(@object);
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            StyleSheetImporterImpl styleSheetImporterImpl = new StyleSheetImporterImpl();
            string     contents   = File.ReadAllText(ctx.assetPath);
            StyleSheet styleSheet = ScriptableObject.CreateInstance <StyleSheet>();

            styleSheet.hideFlags = HideFlags.NotEditable;
            if (styleSheetImporterImpl.Import(styleSheet, contents))
            {
                ctx.AddObjectToAsset("stylesheet", styleSheet);
                StyleSheetAssetPostprocessor.ClearReferencedAssets();
                StyleContext.ClearStyleCache();
            }
            else
            {
                foreach (string current in styleSheetImporterImpl.errors.FormatErrors())
                {
                    Debug.LogError(current);
                }
            }
        }