Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 11, 2017 at 03:54 PM by fastwings12 for the following reason:

found a solotion

avatar image
0
Question by fastwings12 · Apr 11, 2017 at 05:41 PM · spriteresources

how to dynamic create a prefabs from existing sprite (png file)

i have an issue that have alot of assets of png files a.k.a sprite that loaded into the unity project i try build small tool that will load a from them a prefabs (have more then 30 and it is a pain create prefab each of them and they may change) i build this code:

   void GeneratorPrefab(AssetSpriteObject assetSprite)
             {
                 GameObject obj = new GameObject();
                 obj.name = assetSprite.Name;
                 Sprite sprite = new Sprite();
                 string spritePath = assetSprite.RelativePath.Replace(".png", "");
     
                 Debug.Log(String.Format("Loading Res {0}", spritePath));
                 sprite = Resources.Load<Sprite>(spritePath);
                 obj.AddComponent<SpriteRenderer>();
                 SpriteRenderer SR = obj.GetComponent<SpriteRenderer>();
                 SR.sprite = sprite;
     
                 String pathString = Path.GetFullPath(new Uri(Path.Combine(Path.GetDirectoryName(globalPath), String.Format(@"../../Assets/Prefabs/Gen/{0}", prefixName))).AbsolutePath);
                 if (!Directory.Exists(pathString))
                     Directory.CreateDirectory(pathString);
                 UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab(String.Format(@"Assets/Prefabs/Gen/{0}/{0}_{1}.prefab", prefixName, assetSprite.Name.Replace(".png", "")));
                 PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ReplaceNameBased);
                 DestroyImmediate(obj);
             }
 

now it create the prefab but it without a sprite like the code "sprite = Resources.Load(spritePath);" if i debug i see at as null and don't understand why or how to fix my question what i do wrong here P.S the link for the source file here

Comment
Add comment · Show 1
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 fastwings12 · Apr 11, 2017 at 03:53 PM 0
Share

Ok i found the issue it look like i load wrong i change the sprite = Resources.Load(spritePath); into Sprite s = (Sprite)Resources.Load(spritePath, typeof(Sprite)); and it did the trick look like is has casting wrong

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Loading Addressable Sprites using their Address 0 Answers

Resources.Load("sprite") returning nil 0 Answers

Accessing 4.6 GUI build-in sprites in C# 0 Answers

How can I load the UISprite builtin resource at runtime? 1 Answer

Changing a UI image sprite in scripts results in a black texture 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