I think i kinda got the hang of how to use the tool. However this class doesn't show up in types window. Removing the namespace didn't help. Am i missing something? It's just a custom 2d grid class.
--
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ChunikUtils
{
public class ChunikGrid<TGridObj>
{
private int _height;
private int _width;
private Vector3 _origin;
private float _cellSize;
private TGridObj[][] _gridArray;
private float _hPadding;
private float _vPadding;
public int Height => _height;
public int Width => _width;
public Vector3 Origin => _origin;
public float CellSize => _cellSize;
public ChunikGrid()
{
}
public ChunikGrid(int width, int height, Vector3 origin, float cellSize, float horizontalPadding, float verticalPadding,
Func<ChunikGrid<TGridObj>, Vector3, int, int, TGridObj> CreateGridObj)
{
this._width = width;
this._height = height;
this._cellSize = cellSize;
this._origin = origin;
this._hPadding = horizontalPadding;
this._vPadding = verticalPadding;
_gridArray = new TGridObj[width][];
for (var i = 0; i < _gridArray.Length; i++)
{
_gridArray = new TGridObj[height];
}
for (var q = 0; q < _gridArray.Length; q++)
{
for (var j = 0; j < _gridArray[q].Length; j++)
{
float a = (q * cellSize) + (q * horizontalPadding) + (cellSize / 2) + origin.x;
float b = (j * cellSize) + (j * verticalPadding) + (cellSize / 2) + origin.y;
float c = origin.z;
Vector3 v = new Vector3(a, b, c);
_gridArray[q][j] = CreateGridObj(this, v, q, j);
}
}
//DebugGridLines();
}
public void DebugGridLines()
{
Vector3[] corners = new Vector3[4];
for (int x = 0; x < _gridArray.Length; x++)
{
for (int y = 0; y < _gridArray[x].Length; y++)
{
GetGridWorldCorners(x, y, corners);
Debug.DrawLine(corners[0], corners[1], Color.magenta, 1000f);
Debug.DrawLine(corners[0], corners[3], Color.magenta, 1000f);
Debug.DrawLine(corners[2], corners[1], Color.magenta, 1000f);
Debug.DrawLine(corners[2], corners[3], Color.magenta, 1000f);
// Debug.DrawLine(GetWorldPos(x, y), GetWorldPos(x, y + 1), Color.magenta, 1000f);
// Debug.DrawLine(GetWorldPos(x, y), GetWorldPos(x + 1, y), Color.magenta, 1000f);
}
}
// Debug.DrawLine(GetWorldPos(0, _height), GetWorldPos(_width, _height), Color.magenta, 1000f);
// Debug.DrawLine(GetWorldPos(_width, 0), GetWorldPos(_width, _height), Color.magenta, 1000f);
}
public TGridObj[][] GetGridArray()
{
return _gridArray;
}
public void GetGridWorldCorners(int x, int y, Vector3[] arr)
{
Vector3 zero = new Vector3((x * (_cellSize + _hPadding)) + _origin.x, (y * (_cellSize + _vPadding)) + _origin.y, _origin.z);
Vector3 one = new Vector3((x * (_cellSize + _hPadding)) + _origin.x, (y * (_cellSize + _vPadding)) + _origin.y + _cellSize, _origin.z);
Vector3 two = new Vector3((x * (_cellSize + _hPadding)) + _origin.x + _cellSize, (y * (_cellSize + _vPadding)) + _origin.y + _cellSize, _origin.z);
Vector3 three = new Vector3((x * (_cellSize + _hPadding)) + _origin.x + _cellSize, (y * (_cellSize + _vPadding)) + _origin.y, _origin.z);
if (arr != null)
{
arr[0] = zero;
arr[1] = one;
arr[2] = two;
arr[3] = three;
}
}
public Vector3 GetWorldPos(int x, int y)
{
Vector2 origin = new Vector2(_origin.x, _origin.y);
float a = x * (_cellSize + _hPadding) + origin.x;
float b = y * (_cellSize + _vPadding) + origin.y;
return new Vector3(a, b, _origin.z);
}
public void GetGridXY(Vector3 worldPos, out int x, out int y)
{
x = Mathf.FloorToInt((worldPos.x - _origin.x) / (_cellSize + _hPadding));
y = Mathf.FloorToInt((worldPos.y - _origin.y) / (_cellSize + _vPadding));
}
public TGridObj GetElementAtWorldPos(Vector3 worldPos)
{
GetGridXY(worldPos, out var x, out var y);
if (x >= 0 && x < _width && y >= 0 && y < _height)
{
return getElementAtXY(x, y);
}
return default(TGridObj);
}
public TGridObj getElementAtXY(int x, int y)
{
if (x >= 0 && y >= 0 && x < _width && y < _height)
{
var element = _gridArray[x][y];
return element;
}
return default(TGridObj);
}
}
}
Can't sereliaze this gneric class
Re: Can't sereliaze this gneric class
Hi there,
Classes need to be non-generic for them to be serialisable (see the Supported Types guide: https://docs.moodkie.com/easy-save-3/es ... ted-types/).
In this case you would need to make a non-generic version of the class with the specific type you're saving. For example:
As your fields are private, you would also either need to add a SerializeField attribute to these, or make them protected.
All the best,
Joel
Classes need to be non-generic for them to be serialisable (see the Supported Types guide: https://docs.moodkie.com/easy-save-3/es ... ted-types/).
In this case you would need to make a non-generic version of the class with the specific type you're saving. For example:
Code: Select all
public class NonGenericChunikGrid : ChunikGrid<MyNonGenericType>
{
}
All the best,
Joel