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 JitterByte · Nov 27, 2014 at 03:00 PM · arrayleveldictionaryoptimize

Storing level data from a texture file, and drawing at speed.

Hello,

I am working with a partner to create a Zelda style Action RPG game. We have opted to create levels using texture files, and then draw them by converting pixel data into objects. However when drawing the levels it will lag quite badly during redraws, I believe this is because of the way I am storing them ingame. However this method is giving me about 18fps on a 4096x map which is fairly big, but we wish to have a lot going on within our game, so I'd like to minimize the lag as much as possible. We are testing with a 512x map at the moment, that has a steady fps of about 80, but if we move too quickly, it can drop to 40fps very quickly. Below is the code that I am currently working with:

 public class Level : MonoBehaviour 
 {
     [HideInInspector]     public int levelWidth;
     [HideInInspector]     public int levelHeight;
 
     public List<Transform> tiles;
     public List<Color> tileColors;
     public List<Transform> treeObjects;
     public List<Transform> mobObjects;
     public Transform spawnerPrefab;
 
     public Texture2D levelTexture;
     public TextAsset levelJSON;
     private string json;
 
     private Color[] levelColors;
     
     private GameObject player;
 
     private int posX;
     private int posY;
     
     private int lastPosX = 0;
     private int lastPosY = 0;
 
     private int moveDistanceBeforeRedraw = 1;
     private int drawDistanceX = 25;
     private int drawDistanceY = 25;
     
     private List<Transform> drawnTiles;
     private List<Transform> drawnObjects;
 
     private JsonData data;
 
     // Use this for initialization
     void Start () 
     {
         levelWidth = levelTexture.width;
         levelHeight = levelTexture.height;
 
         json = levelJSON.text;
         data = JsonMapper.ToObject(json);
         
         player = GameObject.FindGameObjectWithTag("Player");
         SetupPlayerSpawn();
         
         drawnTiles = new List<Transform>();
         drawnObjects = new List<Transform>();
         DrawEntites();
     }
     
     // Update is called once per frame
     void Update () 
     {
         posX = (int)Camera.main.transform.position.x;
         posY = (int)Camera.main.transform.position.y;
 
         if(  Mathf.Abs( posX - lastPosX ) > moveDistanceBeforeRedraw || Mathf.Abs( posY - lastPosY ) > moveDistanceBeforeRedraw )
         {
             DrawLevel();
             lastPosX = posX;
             lastPosY = posY;
         }
 
         HandleEntities();
     }
 
     private void SetupPlayerSpawn()
     {
         if( (bool) data["Entities"]["Player"]["UseDefault"] )
         {
             int xSpawnPoint = (int)data["Entities"]["Player"]["DefaultSpawn"][0];
             int ySpawnPoint = (int)data["Entities"]["Player"]["DefaultSpawn"][1];
 
             Debug.Log("Setting player position: " + xSpawnPoint + ", " + ySpawnPoint );
             player.transform.position = new Vector3( xSpawnPoint, ySpawnPoint, 0 );
         }
     }
     
     public void DrawLevel()
     {
         drawDistanceX = Mathf.CeilToInt( Camera.main.orthographicSize * Camera.main.aspect + 3 );
         drawDistanceY = Mathf.CeilToInt( Camera.main.orthographicSize + 3 );
 
         levelColors = levelTexture.GetPixels ();
 
         foreach( Transform t in drawnTiles )
         {
             Destroy( t.gameObject );
         }
         drawnTiles.Clear();
         
         for( int x=posX - drawDistanceX; x<posX + drawDistanceX; x++ )
         {
             for( int y=posY - drawDistanceY; y<posY + drawDistanceY; y++ )
             {
                 int pos = x + y*levelWidth;
 
                 if( pos < 0 || pos > levelColors.Length-1 ) continue;
 
                 Color c = levelColors[pos];
 
                 if( tileColors.Contains( c ) )
                 {
                     Transform t = tiles[tileColors.IndexOf( c )];
                     drawnTiles.Add(Instantiate ( t, new Vector2( x, y ), Quaternion.identity ) as Transform);
                 }
             }
         }
     }
 
     public void DrawEntites()
     {
         for( int i=0; i<(int)data["Entities"]["Trees"]["NumberOfTrees"]; i++ )
         {
             int xPos = (int)data["Entities"]["Trees"][""+i+"-pos"][0];
             int yPos = (int)data["Entities"]["Trees"][""+i+"-pos"][1];
 
             Transform t = Instantiate (treeObjects[ (int)data["Entities"]["Trees"][""+i+"-treeID"] ], new Vector2( xPos, yPos), Quaternion.identity) as Transform;
             drawnObjects.Add( t );
         }
         
         for( int j=0; j<(int)data["Entities"]["MobSpawners"]["NumberOfSpawners"]; j++ )
         {
             int xPos = (int)data["Entities"]["MobSpawners"][j.ToString ()]["pos"][0];
             int yPos = (int)data["Entities"]["MobSpawners"][j.ToString ()]["pos"][1];
 
             Transform t = Instantiate (spawnerPrefab, new Vector2( xPos, yPos), Quaternion.identity) as Transform;
             drawnObjects.Add( t );
             
             Spawner s = t.GetComponent<Spawner>();
             
             s.mobPrefab = mobObjects[(int)data["Entities"]["MobSpawners"][j.ToString ()]["mobID"]];
             s.numberToSpawn = (int)data["Entities"]["MobSpawners"][j.ToString ()]["numberToSpawn"];
             s.spawnInterval = ((int)data["Entities"]["MobSpawners"][j.ToString ()]["spawnIntervalMilli"])/1000f;    
             s.regenerationInterval = (int)data["Entities"]["MobSpawners"][j.ToString()]["regenIntervalSeconds"];
         }
     }
 
     public void HandleEntities()
     {
         foreach( Transform g in drawnObjects )
         {
             g.GetComponent<Entity>().CheckVisibility();
         }
     }
 }




I am using the LitJson C# plugin to handle my Json requirements to handle entities such as trees etc, I'm not too worried about the annoyance in level design because I am intending to create a level editor as soon as I have the drawing part optimized.

I believe one method of improving speed is to use a Dictionary method to store map data, but I have no idea where to start with this, so I'd appreciate any advice on good tutorials for that.

Is there any other way I could optimize this to load data then spawn the level faster?

Kind Regards, John

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 meat5000 ♦ · Nov 27, 2014 at 02:57 PM 0
Share

$$anonymous$$aybe ins$$anonymous$$d of the usual 0,0 -> x,y you should start your process at your character first and do offscreen-processing in subsequent frames. If it takes your character 30 seconds to get to the edge of the map thats at least 25 seconds of processing you can fit in, easing the process.

Also ins$$anonymous$$d of creating so many GOs, look in to voxels.

avatar image JitterByte · Nov 27, 2014 at 03:46 PM 0
Share

Thanks $$anonymous$$eat5000, I'll look into voxeling now. Am I right in thinking you are referring to creating meshes ingame and applying the textures to them?

The reason I've not tried this yet is because I'm not really sure How I would go about applying collision regions to them.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Array member treated as null even though it is not. 1 Answer

Missing Materials on build 1 Answer

Change List name as the "Name" Property in the list 1 Answer

Add elements to array/list in inspector? 1 Answer

Assigning scriptable objects to dictionary through array? 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