- Home /
What's a better way to code in chunks
I've spent about 2 weeks trying to fix this one block of code to no avail. I've been trying to take small steps but I've barely made any progress and my code has turned into spaghetti, please help before I ruin my code any further.
The purpose of this section is to spawn in menu items that I'll need to see and despawn them when they get out of range. It sound so simple in words but I can't for the life of me actually do it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
public class ChunkInstantiate : MonoBehaviour
{
//Default image in case none is cast.
public Sprite newImage;
//Used to determine number of instantiated objects
public int objectNumber;
//Used to hold object prefab
public GameObject myPrefab;
//Used to parent prefab to canvas
private GameObject myObject;
public Transform myCanvas;
//Used to control chunks
public int iconNumber;
public int currentNumber;
public int positionNumber;
public int setUp = 0;
public int prevSpace;
public int maxNumber;
// Start is called before the first frame update
void Start()
{
//Typecast path into displayable image
Texture2D tex = Resources.Load("DisplayImages/sunset") as Texture2D;
Sprite newImage = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
//Instantiates initial chunk of prefabs
for (int i = 0; i < objectNumber; i++)
{
//Parents prefab to canvas so it appears visible
myObject = Instantiate(myPrefab);
myObject.transform.SetParent(myCanvas);
myObject.transform.localScale = new Vector3(1, 1, 1);
myObject.transform.name = Convert.ToString(i);
myObject.GetComponentInChildren<Text>().text = Convert.ToString(i);
myObject.GetComponent<Image>().sprite = newImage;
}
//SetUp For Chunks
currentNumber = (objectNumber * positionNumber);
}
//Instantiates prefabs as chunks
void OnTriggerEnter2D(Collider2D icon)
{
iconNumber = Convert.ToInt32(icon.transform.name);
//Instantiates prefab chunks
if ((iconNumber + 1) % objectNumber == 0 && setUp == 1)
{
//Test
Debug.Log(iconNumber);
//Set Current Number
currentNumber = (objectNumber * positionNumber);
//Typecast path into displayable image
Texture2D tex = Resources.Load("DisplayImages/sunset") as Texture2D;
Sprite newImage = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
for (int i = currentNumber; i < (currentNumber + objectNumber); i++)
{
//Parents prefab to canvas so it appears visible
myObject = Instantiate(myPrefab);
myObject.transform.SetParent(myCanvas);
myObject.transform.localScale = new Vector3(1, 1, 1);
myObject.transform.name = Convert.ToString(i);
myObject.transform.SetSiblingIndex(currentNumber - (objectNumber + i));
myObject.GetComponentInChildren<Text>().text = Convert.ToString(i);
myObject.GetComponent<Image>().sprite = newImage;
}
if (iconNumber > prevSpace)
{
positionNumber++;
prevSpace = iconNumber;
}
if (iconNumber < prevSpace)
{
positionNumber--;
prevSpace = iconNumber;
}
}
//Wait
if (iconNumber == 35)
{
setUp = 1;
}
}
//Destroys previous prefabs
void OnTriggerExit2D(Collider2D iconic)
{
iconNumber = Convert.ToInt32(iconic.transform.name);
if (currentNumber > objectNumber)
{
if ((iconNumber + 1) % objectNumber == 0)
{
for (int i = 0; i < objectNumber; i++)
{
Debug.Log("Damn");
}
}
}
}
I'm not realistically expecting anyone to help, but I figure I may as well scream into the uncaring void.
Answer by Llama_w_2Ls · Sep 30, 2021 at 10:28 AM
Define a chunk class below your class like so:
public class Chunk
{
public List<GameObject> menuItems;
public Vector2 Bounds;
}
The bounds property defines what area your menu items are in. You can predefine this or calculate it like so:
For example: If a menu item takes up 2x2 space, a 5x3 chunk would have bounds of new Vector2(10, 6);
.
Next, initialize your chunk when creating objects, so for example:
var chunk = new Chunk();
chunk.Bounds = new Vector2(menuItems.x, menuItems.y);
for (int x = 0; x < menuItemsX; x++)
for (int y = 0; y < menuItemsY; y++)
{
var obj = Instantiate(?, new Vector2(x, y), Quaternion.identity);
chunk.Items.Add(chunk);
}
This is for one chunk. Keep doing this for all your items, using offsets for each chunk. Also store the position and chunk in a dictionary.
Last, foreach chunk in the dictionary, if the distance between your object and the chunk is out of view, deactivate all items in the chunk. Else, activate them.
I hope that helps. I dont honestly know what youre asking for exactly. Could you show some images or an example of what result you want?. My code is for chunking objects like in minecraft..
Your answer
Follow this Question
Related Questions
Instantiating Objects 1 Answer
How to store a prefab in a variable 1 Answer
Show/hide object in C# 2 Answers
Delete children of Instantiated object 2 Answers