Loading 64*5 from a 64*64 Color[][] Array
Hello,
New try with a new project design highlighting the problem. Therefore i deleted my old post.
With the following code i try to make a plane with a user specific material on it. To do this I have a image that contains all the tiles to make this material. For this example i created this img:
If u add this image in a new project and add a plane and a empty material the following code will run after some settings:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class test : MonoBehaviour {
public int size_x;
public int size_z;
public float tileSize = 1.0f;
public int tileResolution;
public Texture2D terrainTiles;
int[,] textureTile;
int number;
void Loadmap()
{
//toekomst map uit database halen
textureTile = new int[size_z, size_x];
for (int z = 0; z < size_z; z++)
{
for (int x = 0; x < size_x; x++)
{
if (z == 0)
{
textureTile[z, x] = 0;
}
else
{
textureTile[z, x] = 2;
}
}
}
}
void Start()
{
int textWidth = size_x * tileResolution;
int textHeight = size_z * tileResolution;
Texture2D texture = new Texture2D(textWidth, textHeight);
Texture2D normal = new Texture2D(textWidth, textHeight, TextureFormat.ARGB32, true);
Loadmap();
Color[][] tiles = chopUpTiles();
for (int z = 0; z < size_z; z++)
{
for (int x = 0; x < size_x; x++)
{
Color[] p = tiles[textureTile[z, x]];
texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, p);
if (z > 0)//borderchecker
{
if (!(textureTile[z - 1, x] == textureTile[z, x]))
{
Color[] t = tiles[textureTile[z, x] + 1];
Color[] v = tiles[textureTile[z - 1, x] + 1];
texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, 5, t);
texture.SetPixels(x * tileResolution, z * tileResolution - 5, tileResolution, 5, v);
}
}
}
}
texture.filterMode = FilterMode.Bilinear;
texture.wrapMode = TextureWrapMode.Clamp;
texture.Apply();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
mesh_renderer.sharedMaterials[0].mainTexture = texture;
}
Color[][] chopUpTiles()
{
int numTiles_x = terrainTiles.width / tileResolution;
int numTiles_z = terrainTiles.height / tileResolution;
Color[][] tiles = new Color[numTiles_x * numTiles_z][];
for (int z = 0; z < numTiles_z; z++)
{
for (int x = 0; x < numTiles_x; x++)
{
tiles[z * numTiles_x + x] = terrainTiles.GetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution);
}
}
return tiles;
}
}
the settings are:
The image needs to be Read/Write Enabled in the inspector
( edit forgot this) Set the img on clamp and Mobile defuse in the inspector.
drag the code on the plane
this enables the settings. select the plane and fill the public variables with info :)
Size_x = 2
Size_z = 2
Tile Size = 1
Tile Resolution = 64
Drag the image on Terrain Tiles
Also set the empty material to the Plane.
If u run the project you can see it adds the border (right side of the image) correctly but it shows the bottom border of the image on the green and the blue square while the blue one should show the top one.
I know i should address the Color[][] for the top part but i can't figure out how to do so. Got that part from a tutorial i followed before i went in a different direction.
Please help me out.
Nivey
So the fundamental problem is you have a source array of Color[][] that represents a 64x64 grid and you want to copy just a "strip" of that into another Color[][] array represents a 64x5 grid?
Yes, right now i just tell it to draw in a 64*5 field and starts doing that from the bottom up. it is stored in there i just don't get how to retrieve the specific field. was thinking about trying to flip the image or even make more Color[][]'s with only one of the borders in there. But this is a workaround and i know it can be done more efficient.
I am kinda new in using arrays so ill try to explain what i read from the code. Color[][] means that every Color[] has another array with data. In Color[] i store the "tile id" and in the second array the 64*64 field from the source img.
now i address the tile id and get the second array in a new one and display that on the tile map.
since I'm new to arrays i might read this all wrong.
thanks for the help tough, u where right in my previous post that was a mess.
I'm trying to understand what you want but aren't getting.
I think you have an array of 64x64 colors for each tile. So tiles[0] represents one array of 4096 colors for the first tile, tiles[1] represents another array of 4096 colors for the next 64x64 tile.
I think you want to reduce one of those 4096-color arrays representing a tile down to a 320-color array representing a five-pixel-wide edge of one of the tiles.
I don't understand what it is you are actually trying to accomplish, other than something to do with compiling a texture out of a tile map and an image with all the tiles in it.
I really don't understand this business about pulling everything into a bunch of arrays and then trying to slice those arrays into other arrays. If it were me, I'd just pull all the arrays of color I directly from the source image in exactly the shape I wanted them from the start. It seems like you're trying to maybe do some kind of 9-slicing. If that's the case, I'd create a class called Tile, with a property for each of the kinds of arrays I need (center, left-border, right-border, top-border, bottom-border, and each of the four corners). When I was decomposing the source image, I'd pull all of the parts of the tile square I might need out at that time. Then I can just reference them semantically, whenever I need them.
Answer by Nivey · Sep 30, 2017 at 11:05 AM
I just thought of another option. instead of loading the desired array in another array i could paint it on a material 64*64 and then grab the desired field from there with getpixels. This will have the same amount of code lines but needs another texture in code.
I think i go for this or is this bad too? @MaxGuernseyIII I read you got tons of experience and since i only work with c# and unity for 3 months, I do know other programming languages for many years now, it would not be wise to go against your idea. So I'm curious what your thoughts are for this approach? Also thanks for telling me i can address the color array from its first array instead of loading a second.
Regards Nivey
The above works. I keep the current tile border as it is and the one beneath it i spray on a new 64*64 texture and grab the desired part with getpixels from the new texture. gonna test some code that keeps track whats on it so it doesn't always repaint the new texture. and see what runs faster :).
Your answer
![](https://koobas.hobune.stream/wayback/20220612135915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
C# Unity dot syntax exercise correct solution? 1 Answer
How do I fix my scrolling background,How do I fix my scrolling background? 0 Answers
Getting Int value from script to other script 2 Answers
Player rotation always resets. 0 Answers