Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by morbidcamel · Sep 09, 2014 at 06:47 AM · editorunityeditoreditor-scriptingfile-ioeditor extension

Creating an texture asset in editor script

Hi Guys,

I wanted to experiment with creating a png from the AssetPreview image and saving it to a folder in my project then creating a component and point it to that texture as an editor extension. Sounds simple but for some reason, even if I edit the permissions of the target folder I get

 UnauthorizedAccessException: Access to the path 'Assets/Space Station/Textures/Previews' is denied.
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:259)
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
 (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:135)
 System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:130)
 System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:594)
 DeepspaceUtilities.CreatePreview () (at Assets/Space Station/Scripts/Editor/DeepspaceUtilities.cs:29)
 

Here is the code, it can be useful but this I/O error has got me stumped!

 public class DeepspaceUtilities : ScriptableObject
 {
     private const string previewPath = "Assets/Space Station/Textures/Previews";
     [MenuItem("Deepspace/Create Preview...")]
     static void CreatePreview()
     {
         var transforms = Selection.GetTransforms (SelectionMode.TopLevel);
 
         if (transforms.Length > 0)
         {
             for(int i = 0; i < transforms.Length; i++)
             {
                 var t = transforms[i];
                 if (t.GetComponent<PreviewProvider>() == null
                     && EditorUtility.DisplayDialog("Create Preview?", 
                                                 string.Format("Do you want to create and add a preview component to {0}", t),
                                                 "Create", "Cancel"))
                 {
 
                     var prev = AssetPreview.GetAssetPreview(t.gameObject);
                     if (prev != null)
                     {
                         string path = previewPath +string.Format("/{0}.png", t.gameObject.name);
 
                         File.WriteAllBytes(previewPath,  prev.EncodeToPNG ());
                         Hawk.Log("Created asset at {0}", path);
                         var provider = t.gameObject.AddComponent<PreviewProvider>();
                         provider.preview = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
                     }
                 }
             }
 
         
         }
     }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave29483 · Sep 09, 2014 at 07:55 AM 0
Share

You are using system.io directly which is not unity specific. Also you are using a relative path. But relative to what? It seems the path does not exist. Try testing with a known path like your desktop. If you want the assets folder I think you can get the assets path with Application.dataPath.

avatar image morbidcamel · Sep 10, 2014 at 02:14 AM 0
Share

Dave I think you right, it's actually pretty obvious.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gizmoi · Sep 09, 2014 at 07:58 AM

Might be easier to use UnityEditor.AssetDatabase.CreateAsset rather than WriteAllBytes

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by morbidcamel · Sep 10, 2014 at 02:34 AM

OK Dave you're right - here's the solution... I'm posting it here in case someone wants to make a preview of their assets quickly to use in the game gui for example...

 private const string previewPath = "Space Station/Textures/Previews";
     [MenuItem("Deepspace/Create Preview...")]
     static void CreatePreview()
     {
         var transforms = Selection.GetTransforms (SelectionMode.TopLevel);
 
         if (transforms.Length > 0)
         {
             for(int i = 0; i < transforms.Length; i++)
             {
                 var t = transforms[i];
                 if (t.GetComponent<PreviewProvider>() == null
                     && EditorUtility.DisplayDialog("Create Preview?", 
                                                 string.Format("Do you want to create and add a preview component to {0}", t),
                                                 "Create", "Cancel"))
                 {
 
                     var prev = AssetPreview.GetAssetPreview(t.gameObject);
                     if (prev != null)
                     {
                         string path = Path.Combine(Application.dataPath, string.Format("{0}/{1}.png", previewPath, t.gameObject.name));
                         Hawk.Log("Creating asset at {0}", path);
                         File.WriteAllBytes(path,  prev.EncodeToPNG ());
 
                         var provider = t.gameObject.AddComponent<PreviewProvider>();
                         provider.preview = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/"+path, typeof(Texture2D));
                     }
                 }
             }
 
         
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave29483 · Sep 11, 2014 at 06:59 AM 0
Share

Glad I could help :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Event or hook for executing code before unity opens and compiles/loads the project. 1 Answer

Finding a GameObject in an editor script based on position 2 Answers

Working with Unity Editor 1 Answer

Converting XML-files into assets 1 Answer

How to get unity3d editor account id or unique id 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges