Author Topic: [Grids 2D] Looking for function to set cell as highlighted  (Read 142 times)

copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Grids 2D] Looking for function to set cell as highlighted
« on: January 10, 2021, 01:16:51 AM »
https://streamable.com/dgw04t

I'm working on moving this cursor and highlight across my Grids2D cells.

I am working if there's a way to use the same highlight effect, but on keyboard WASD/arrow movement. Something where I can set a cellIndex as highlighted, and clear the rest.

copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #1 on: January 10, 2021, 01:28:09 AM »
One other thing, can I control what sprite layer the grid should be drawn on?

Right now, it's drawing lines over portions of my map that aren't traversable. I thought it was rendering on the Default sprite layer, but even buildings I have set on a higher sprite layer still get drawn over.

You can see it at the foot of the castle in the video.

EDIT: It seems adjusting the camera size fixed it grid rendering. Question still stands though.
« Last Edit: January 10, 2021, 04:46:40 AM by copyleft »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #2 on: January 11, 2021, 09:31:15 AM »
Hi,

Yes, download latest beta first.
Then, you should be able to use a code like this, which manually highlights any cell:

Code

GameObject currentlySelected;

private void Update() {
grid.highlightMode = HIGHLIGHT_MODE.None;
        if (Input.GetKeyDown(KeyCode.A)) {
if (currentlySelected != null) {
grid.HideHighlightedObject(currentlySelected);
}
int row = 5;
int column = 5;
int cellIndex = grid.CellGetIndex(row, column);
currentlySelected = grid.HighlightCellRegion(cellIndex, true);
            }
        }

When pressing "A", the above code will highlight a random cell. Of course modify it so it reacts to cursor keys and have into account

copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #3 on: January 22, 2021, 01:58:43 AM »
Thanks, this helped. The cell is indeed highlighted, BUT only on mouse hover. Can I somehow detach highlighting from mouse hover? The highlight also doesn't fade in/out, but remains solid. Check the video, you can see both the solid highlight and the mouse cursor's influence.

https://streamable.com/naopzm

EDIT AGAIN: I am not sure why, but after a restart, the cell is now highlighted regardless of mouse interaction. The only issue now is the lack of opacity changing and the similar highlight functions only showing solid black color.

Within my Update() call:
Code
        _grid.highlightMode = HIGHLIGHT_MODE.None;
        _grid.HighlightCellRegion(_selectedCell.index, true);

The ideal solution would be for this to work mainly with keyboard/gamepad input, and for the highlight to display indefinitely (until hidden via code).

Also curious, would it be possible to highlight at a different color at runtime? Without changing the highlight color.

I suppose I can use CellFlash(cellIndex, color, duration) or CellBlink(cellIndex, color, duration) to mimic a highlight effect.
EDIT: I tried these, I just get a solid black colored cell.

Code
        _grid.CellBlink(_selectedCell.index, new Color(54, 99, 99), 0.567f);
        _grid.CellFadeOut(_selectedCell.index, new Color(54, 99, 99), 6.567f);
        _grid.CellFlashOut(_selectedCell.index, new Color(54, 99, 99), 6.567f);

        // Tried each one individually.


Which sucks, because I want to be able to do a blinking highlight in different colors to show movement & attack ranges. Rather than a solid color.

Another question, with tgs there was a .CellGetRectWorldSpace method that returns Bounds for a Cell.

In Grids2D, the best I can get is the center position of a given cell.

Is there a way to get Bounds for a cell with Grids2D? Otherwise, I'll have to create my own bounds by doing something like:

The cell size is 32px per cell.

Code
            protected Dictionary<int, Bounds> _cellPositions = new Dictionary<int, Bounds>();

            foreach(Cell cell in _grid.cells) {
                Vector3 position    = _grid.CellGetPosition(cell.index);
                Vector3 boundsSize  = new Vector3(0.32f, 0.32f, 1);

                Bounds cellBounds = new Bounds(position, boundsSize);

                _cellPositions[cell.index] = cellBounds;
            }

EDIT: The above code works fine for my purposes. You'll have to edit this if you want to do something similar, since I can't find a way to dynamically pull the bounds of a Grid2d.Cell... maybe if it had a renderer or something.
« Last Edit: January 22, 2021, 08:51:03 AM by copyleft »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #4 on: January 22, 2021, 08:48:24 AM »
I've addded such behaviour in latest beta (to which you now have access btw) so setting highlightMode to NONE allows you to highlight cells in a persistent way.

The CellBlink/CellFlash/etc functions should work fine, just make sure you pass 0-1 values to new Color(x,x,x) or use Color32 for 0-255 ranges.

Also, I added CellGetRectWorldSpace and TerritoryGetRectWorldSpace methods from TGS.


copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #5 on: January 22, 2021, 08:53:51 AM »
Sorry, how do I access the latest beta? Is it through the Asset Store? I'm on version 6.5, publish Jan 19th.

About CellFlash/CellBlink, still no dice after switching to Color32:

video : https://streamable.com/tktzfs

Code
        _grid.CellFlash(_selectedCell.index, new Color32(54, 99, 99, 254), 6.567f);

EDIT: I found the sub-forum! Thanks, I'll try it out.
« Last Edit: January 22, 2021, 09:05:52 AM by copyleft »

copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #6 on: January 22, 2021, 09:24:37 AM »
I updated to the latest beta, and I'm seeing the same issues.

The highlight is present, but with no flashing/opacity changes. I wonder if it has to do with the Update calling it so frequently, it just redraws over and over? I have to imagine that there's a similar Update call for the mouseover, and that highlight fades according to the set duration with no problem.

This is the entirety of my Update call, pretty simple (base.Update doesn't touch Grid2D at all, so that should be safe):

Code
    new void Update()
    {
        base.Update();

        _grid.highlightMode = HIGHLIGHT_MODE.None;
        _grid.HighlightCellRegion(_selectedCell.index, true);
    }

what it looks like: https://streamable.com/voz081

CellBlink/CellFlash still show that solid black square. I did some investigation, maybe it has something to do with the transparent material: https://i.imgur.com/4sWsL7r.png


I opened the CanvasBackgroundClear unitypackage and tried using that instead, but had the same results, black square.

EDIT: Actually, the material is swapped to Grids 2D System/Canvas Background Transparent once I press Play.

3d view: https://streamable.com/vq28d6

I figure if the highlight at least shows in the given color, those similar functions should too.

---

in the end, these aren't huge issues. I can live with the solid highlight and use solid cell colors instead of Flash/Blink and move on. But, the flashing highlight is very visually appealing. It would also prove useful as HUD indicators on the battle map, like an end-goal cell that you must seize.
« Last Edit: January 22, 2021, 07:53:33 PM by copyleft »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #7 on: January 25, 2021, 11:01:00 AM »
The demo scene 1 uses CellFadeOut() method. Do you see any difference with your settings? Because it should work out of the box. Perhaps the issue can be reproduced if you can copy/paste your settings into the grid of this demo scene and run it.

copyleft

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [Grids 2D] Looking for function to set cell as highlighted
« Reply #8 on: January 29, 2021, 06:08:14 AM »
I ended up just rolling out a simple flash myself.