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 theportalmasters · Jun 25, 2020 at 09:50 PM · spritescriptableobjectstatic

How does one get sprites into a static class?

I have a set of tiles. Each of these tiles has a number attached called the sprite index. When reading these tiles to place them into the world, I take that number and use it to look up a sprite from a scriptable object which holds an array of sprites, called a sprite pallet. I need to access a pallet from the static class that holds the map generation. I also need to be able to set that pallet beforehand and to put sprites in it somehow. I can do one or the other, but not both. I either have a palate I cant use or a sad empty palate that holds no sprites

I have tried making a mono behavior that has an instance variable that is hooked up with get/set into a static class with an array of pallets but It does not show up in the editor, making it rather hard to assign pallets to.

Does anyone know a good way of getting premade, persistent objects, be they scriptable or image-based, into a static class?

I don't know how much the code will help with this question, but here it is, just in case

 [Serializable]
 public class Map 
 {
     //map data
     public int sizeX;
     public int sizeY;
     public int spritePallate;
     public float[,] heightMap;
     public bool[,] walkableMap;
     public int[,] spriteIndexMap;
 }

 public static class MapReader
 {  
   public static Tile[,] GeneratePhysicalMap(Map map)
     {
         DestroyPhysicalMapTiles();
         if (map == null)
         {
             map = new Map(backupMapSize.x, backupMapSize.y);
         }
         tileParent = new GameObject("Tile Parent").transform;
         tiles = new Tile[map.sizeX,map.sizeY];
         Vector2 mapHalfHeight = new Vector2(map.sizeX / 2.5f, map.sizeY / 2.5f);
         for (int x = 0; x < map.sizeX; x++)
         {
             for (int y = 0; y < map.sizeY; y++)
             {
                 Tile tile = map.SetTileProperties(x, y);
 
                 GameObject gameObjectTile = InstantateTileGameObject(mapHalfHeight, tile);
 
                 gameObjectTile.AddComponent<TileBehaviour>().tile = tile;
                 tiles[x, y] = tile;
 
                 gameObjectTile.AddComponent<BoxCollider>().size = new Vector3(1, 1, .2f);
 
                 gameObjectTile.AddComponent<SpriteRenderer>().sprite = GetSpriteFromIndexAndPallete(tile.spriteIndex,/*ERROR*/);
             }
         }
         return tiles;
     }
 
     public static Sprite GetSpriteFromIndexAndPallete(int spriteIndex, SpritePallate spritePallate)
     {
         return spritePallate.sprites[spriteIndex];
     }
 }

 [CreateAssetMenu(fileName = "Pallate", menuName = "SpritePallate", order = 0)]
 public class SpritePallate : ScriptableObject
 {
     public int ID = 0;
     public Sprite[] sprites;
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jkpenner · Jun 25, 2020 at 11:04 PM

If you're calling GeneratePhysicalMap from a GameObject, you could make the SpritePallate a parameter of the GeneratePhysicalMap method. The GameObject could then store a reference to a SpritePallate and pass it to the method when it is called.

 public static Tile[,] GeneratePhysicalMap(Map map, SpritePallate spritePallate)
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 theportalmasters · Jun 26, 2020 at 11:23 PM 0
Share

That will work for me for now. Thank you for such a swift reply! It makes me feel a little silly for not thinking of it sooner.

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

176 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers

Can I active some static function in ScriptableObject? 0 Answers

Scriptableobject sprites 0 Answers

How can i use static function in ScriptableObject? 3 Answers

Unity : Singleton ScriptableObjects resets after play 1 Answer


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