Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Grid Framework [scripting and editor plugins]

Discussion in 'Assets and Asset Store' started by hiphish, Jul 24, 2012.

  1. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    You seem to have linked the wrong image, it is tiny.
     
  2. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    ok , new image ;)
     
  3. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I think I know it: You give integer coordinates, which are on the edges of the grid. Boxes however, are on faces, i.e. in between edges. At this point it is ambiguous which face to pick because both are equally distant from the edge, so the computer picks on arbitrarily depending on the floating point precision.

    You have to nudge the coordinates in the right direction: add 0.5f * Vector3.one to the grid coordinates:
    Code (csharp):
    1. myGrid.NearestBoxW(myGrid.GridToWorld(newPosition + 0.5f * Vector3.one))
    Note that I have stripped the line down a bit. In fact you can even skip looking for the box when your coordinates are always integers, the box will always be half a cube forward in grid-coordinates:
    Code (csharp):
    1. myGrid.GridToWorld(newPosition + 0.5f * Vector3.one)
     
  4. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    thank you ;) that was my mistake now it work.
     
  5. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Official extension methods
     
    Tinjaw likes this.
  6. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    @hiphish i have found a new problem for me ;)

    i have a empy gameobject , with 3 child object in it.
    now i have a grid and assign a grid position for this one object.
    It should ignor the 3 child objects right ? or try it to fit all 3 objects ?
    if i use one object it work , with this object with 3 childs it didnt work.

    example code:
    Code (CSharp):
    1. public Transform StackGroupObjTransform;
    2. public GFRectGrid MyStackGrid;
    3.  
    4. StackGroupObjTransform.transform.position = MyStackGrid.GridToWorld (MyStackGrid.NearestBoxG (MyStackGrid.GridToWorld (new Vector3 (-1,0,0)+ 0.5f * Vector3.one)));
     
  7. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    This is a general Unity thing: If you set a parent's position all children will inherit its position. Assuming the local position of the first child is (0.5, 0.3, 0) and the global position of the parent is (1, 2, 3), then the global position of the child will be (1.5, 2.3, 3) (ignoring rotation here). This means you are only aligning the parent, the children will follow it but that's no guarantee that they are really aligned themselves.

    If you want to align the children you will have to step recursively through the parenting tree:
    Code (csharp):
    1. void AlignTree(Transform root) {
    2.     AlignTransform(root);  // some general alignment function;
    3.     foreach (var child in root) {
    4.         AlignTree(child);
    5.     }
    6. }
    7.  
    8. Transform parent;  // root of the parenting tree
    9. AlignTree(parent);  // this will recursively traverse the tree
    Here AlignTransform is whatever function you want to call for aligning your objects. If you intend to use this approach often you can make it an extension method of the grid (see the user manual).
     
  8. olszewskim

    olszewskim

    Joined:
    Oct 26, 2015
    Posts:
    11
    Another question. I have a problem with objects which size is bigger than one grid cell. I wish to join this two objects but grid behaviour doesn't allow to do this. Is it any way to do this?
    upload_2016-3-16_13-19-6.png

    upload_2016-3-16_13-19-28.png

    I wish to put this objects something like this:
    upload_2016-3-16_13-21-22.png

    Is it possible?
     
    Last edited: Mar 16, 2016
  9. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Let me see if I understand it: your object are more than one unit wide, let's say they are 1.2 units wide, then they are 0.1 units over the edge to both sides. When you place both adjacent to each other they will intersect because they both go beyond their squares.

    The question is what do you want to accomplish? If you join two objects with a width of 1.1 each, then their combined width will be 2.4. How should this be aligned? Do you place the centre on the edge and have each object? Then the compound object is going 0.2 units over the edge on each side. In your third image you leave one object in place, that means the overflow stays 0.1 on one side, but becomes 0.3 on the other side. Another idea would be to merge them into one wholly new object that consists of the union of the two, but that would require modifying the GameObject or replacing two GameObjects with a new one. Or you could just make it such that the two objects just pass through each other (no collision detection between them).

    Regardless of which approach you want to take, you need to hook into the intersection detection code. I assume you are using the snapping units example that's included. There is a part of the code near the end that is devoted to tinting the object red and rejecting the position when two objects intersect. If you want to allow intersection just remove that part completely, they will then pass through each other like nothing. If you want to manipulate your objects you will have to replace the tinting and reverting logic with your own. (Make a copy of the example script and edit that, so your changes don't get overwritten with the next update to Grid Framework)
     
  10. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    I purchased this asset a while back. I would appreciate if you could give me some general directions on how to accomplish the following:

    1. Create a 8 * 8 grid and place 2d sprites in each of the cells. I am starting to look at the examples so I assume this should be standard stuff.

    2. The grid should auto-resize so that it maintains the same aspect ratio and percentage in different screen resolutions.

    3. I want to have a shooter under the grid like in Space Invaders and be able to fire at one particular column and destroy the sprites in that column. So when the bullet hits the column, I would need to convert world coords to grid coords to figure out the column.

    Do you have an example I could work off? If not, some general overview will do.

    Thanks,
    Arshad
     
  11. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi, I will answer your questions individually.
    That's very simple: construct the target coordinates in grid-postion and convert to world-space:
    Code (csharp):
    1. GFRectGrid grid;  // the grid we use
    2. Transform trans;  // the transform to align
    3. Vector3 position;  // the target position in grid-space
    4.  
    5. trans.position = grid.GridToWorld(position);

    The grid's size is in world-space, so in general there should be nothing for you to do if the world's display scales with the camera. Otherwise you will have to adjust the spacing of the grid, it's just a regular Vector3. How to compute the spacing depends on how your game is designed, it's a general Unity problem.
    Code (csharp):
    1. Vector3 newSpacing;
    2. grid.spacing = newSpacing;
    Sure thing, just like the first question, except the other way around:
    Code (csharp):
    1. Vector3 position;
    2. int column = Mathf.RoundToInt(grid.WorldToGrid(position).y - 0.5f);
    I subtracted 0.5 because if the position is between two edges it will have a +0.5 coordinate and rounding to the nearest integer will randomly round up or down. That's why I give it a bias towards the left.

    With that said, you might want to consider using the Model-View-Controller pattern to write all your game logic in grid-space. Use that as your model and the Unity scene as the view. Grid Framework can then act as the controller that connects the view and the model with each other.

    For the first question take a look at the level design example. There I read from a text file the grid-position of some blocks and convert the result to world-space.
    For the second question you can consult the seemingly endless grid example. In order to create the illusion of an endless grid I adjust the range of the grid dynamically as the camera moves. That's similar to adjusting the spacing.
    Finally take a look at the lights-out example. When the player clicks a tile its grid-coordinates are computed and used to decide which lights to toggle. The entire game logic has been written in grid-space.

    The examples are quite verbose, but most of the code is just the set up the examples to begin with. The really relevant parts are just a handful of lines. My website as summaries of all the examples:
    http://hiphish.github.io/grid-framework/examples/
     
    Last edited: Mar 20, 2016
  12. IGMarco

    IGMarco

    Joined:
    Jan 19, 2016
    Posts:
    1
    Hi @hiphish,

    thx for your great work.

    Maybe you could help me with the following problem:

    Currently we´re using a world space canvas and a screen space canvas and would like to display a hexagon grid below both layers. No matter what we´ve tried, the grid is displayed on top of the world space canvas and below the screen space canvas.
    The scene contains only one camera (orthographic, depth 1) and as shown in your examples, the grid is rendered OnPostRender using the DefaultShader. It seems to have no effect setting the "Queue" attribute in the shader or setting the material.renderQueue value directly (for example: "2" or "10000").
    Do you have any idea, what we could do here?

    Thx in advance
     
  13. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Yeah... rendering is the weak spot of Grid Framework. The problem is that Unity's graphics are basically on autopilot, there is very little that can be done from the outside. I have been looking for a proper solution, but the only good solution is to generate a mesh at runtime and use that. Basically I would have to re-invent Vectrosity. And quite frankly there is no point in doing that, it would be more restrictive than the real Vectrosity and I would have to add the extra effort to the price of Grid Framework. So what's the point, you might as well just use Vectrosity and and interface to it that Grid Framework provides.

    I know that me telling you to buy another framework is not what you would have expected, but Unity does not offer any other way of doing runtime rendering reliably in a way that would work in all cases.
     
  14. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Revisiting coordinate systems
     
  15. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Managing renderers
     
  16. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Hey! i am just trying to use the runtime feature on my phone and somehow the Dragging of the Object doesn't work on mobile somehow? so the function thats inside the SnappingUnits.cs
    Have you ever tried that?
    it works perfect in the editor and with unity remote
     
    Last edited: Apr 19, 2016
  17. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
  18. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    One unified directory
     
    Tinjaw likes this.
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The Update callback method is called on every instance every frame. Every instance of the script is constantly checking if the mouse button in being held down anywhere, and if it is they all rush to that location. You need a way so that only the item you actually clicked responds to your mouse input. The OnMouseDown and OnMouseUp callbacks on the other hand are only called on the instance of the script that is attached to a GameObject that has the collider that has been clicked.

    Please don't take any offence in this, but what you are asking is a general Unity problem, so you are more likely to get a good answer outside this thread. I don't work on mobile platforms myself, I just make sure there is nothing in the framework that would not work on mobile.

    Here is what I would do: clicking and touching is pretty similar once you know where the player is touching.
    Code (csharp):
    1. // Mouse
    2. var screenPosition = Input.mousePosition;
    3. // Touch device
    4. var screenPosition = Input.GetTouch(0).position;
    http://docs.unity3d.com/ScriptReference/Input-mousePosition.html
    http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
    http://docs.unity3d.com/ScriptReference/Touch-position.html
    Now convert the screen position to the world position of the camera, shoot a ray from that point in the direction of the camera and see what it hits:
    Code (csharp):
    1. var camera = Camera.main;
    2. var forward = camera.transform.forward
    3. var touchedPoint = camera.ScreenToWorldPoint(screenPosition);
    4. if (Pysics.Raycast(touchedPoint, forward, Mathf.infinity) {
    5.     // Do stuff here
    6. }
    7.  
    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    This is just one way of doing it, other people might have better ideas.
     
    Der_Kevin likes this.
  20. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    Putting the docs in a ZIP file and having the user expand it into a directory outside the assets folder is a fine idea.

    As for the later, any good text editor can do a search and replace in files and change them for you automagically. All you need do is give them a test after that and tweak any issues you find.
     
  21. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    ZIP file seems too dcumbersome to me. I don't write the documentation files directly, I have it generated by Doxygen from the C# source and Markdown documents. I could try using find and mv to automatically change the file ending of any JavaScript file to TXT and then use sed to change the file name of any reference to a JavaScript file inside the HTML and JavaScript files. I think that's the way to go if I can make it work.

    Doxygen is really good when it comes to gathering documentation from source files, but its output is barely customisable. I have been wanting to ditch it for Sphinx, but without a C# domain (I have written a skeleton that compiles successfully, but nothing more at the moment) I can't make the jump yet. Sphinx uses Jinja templates for the HTML, which is also what I use for my website, so they would fit together greatly.

    EDIT: I got it!
    Code (csharp):
    1. for f in `find . -type f -name '*.js'`; do stump=`dirname $f`/`basename $f .js`; mv $f $stump.txt; done
    2. for f in `find . -type f -name '*.txt' -or -name '*.html'`; do sed -E -i "" 's/([a-z0-9]*)\.js/\1.txt/' $f; done
    OK, text files it is then, no stupid hackery with ZIP files. There is nothing Unix can't fix with some spit and shell glue.
     
    Last edited: Apr 22, 2016
  22. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    Dear hiphish,

    my grid have the spacing 0.2,objects are smaller than the size of the grid.
    with follow code i will set the objects to the grid center, the should be stand in the middle in one grid.

    Code (CSharp):
    1. transform.position = MyStackGrid.NearestBoxW (transform.position);        
    to align the the object inner the grid should we use align ?

    Code (CSharp):
    1. //  transform.position = MyStackGrid.AlignVector3(transform.position, new Vector3(1,1,0));
    ? i have test it already same result.
    most time , the objects are on the grid line left , or on the top line of grid. not in the middle of the grid.

    thank you ,
     
  23. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    i found the issue , sorry.
    after use AlignVector3 i have set the position again with wrong a wrong position ;)
     
    hiphish likes this.
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Fixing what has been broken
     
  25. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Documentation dilemma solved
     
    Tinjaw likes this.
  26. pixelmonk

    pixelmonk

    Joined:
    May 25, 2011
    Posts:
    8
    I would like to be able to use the "Runtime Snapping" example to place game pieces on a grid and also have the option of rotating the piece (around it's local axis) and having the piece maintain its new rotation if I then drag it again. I can currently drag a piece, stop dragging, then rotate it just fine. But if I start dragging it again, it snaps back to its original orientation. I would also like to be able to rotate the piece while dragging - i.e., click a piece, hold down the mouse button and drag it around, and while still holding the mouse button down, rotate the piece, let go the mouse button and the piece stays in its new orientation. Is there any way of doing this?

    Thanks.
     
  27. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    The AlignTransform method has three overloads, what you want is this one:
    Code (csharp):
    1.  // First argument is the Transform, second one is whether to rotate the object
    2. AlignTransform(t, false);
    This way objects will keep their current rotation. In the reference manual you can type the name of a method into the search box and it will show you all the overloads for it.
     
  28. pixelmonk

    pixelmonk

    Joined:
    May 25, 2011
    Posts:
    8
    Excellent! How did I miss that?

    Thank you!
     
    hiphish likes this.
  29. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Hey! i got two questions but the second one is somehow just out of interest and not so important..
    1) can i somehow output where a gameobject is located on the grid. lets say I have a 3x3 grid and the gameobject ABC is in the middle i want simply debug ABC is on 2-2 or B2 or whatever :)

    2) iam instantiating a simple cube on a grid during runtime like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Instantiate : MonoBehaviour {
    5.  
    6.     public Transform Building;
    7.  
    8.     public void instanciate_building() {
    9.         Instantiate(Building, new Vector3(0, 0.5f, 0), Quaternion.identity);
    10.         }
    11.        
    12.  
    13.  
    14. }
    15.  
    in this case, on a second instantiation, a second cube gets placed over the existing cube. how can i prevent this and place a cube always on the next free grid place?
     
  30. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    1) How about this:
    Code (csharp):
    1. GFGrid grid;
    2.  
    3. var p = grid.WorldToGrid(transform.position);
    4. var x = Mathf.RoundToInt(p.x);
    5. var y = Mathf.RoundToInt(p.y);
    6.  
    7. var message = x + "-" + y;
    First we convert the position to grid coordinates, then we round the coordinates we want to integers and build the message string out of them. Remember to add a bias to the grid position if your objects are between coordinates, such as on faces of rectangular grids.

    2) This one depends on how your game is set up. First of all, how does the game know a square is occupied? Do you want to be using collision detection? Raycasting? Keep track of the game using an array? For the sake of simplicity let's assume you want to cast a ray.

    You first cast a ray where you want to place the object. If the ray does not hit anything you are good to go. If the ray does hit something you have to decide what "the next free square" means. A simple solution would be to circle around your original square:
    Code (csharp):
    1.                  1      11      111    111      111    111    111
    2. 0   -> 01   -> 01   -> 01   ->  01 -> 101   -> 101 -> 101 -> 101
    3.                                                 1      11     111
    4.  
    5.                    2     22     222    22222
    6. 111     1112    1112    111     111     1112
    7. 1012 -> 1012 -> 1012 -> 101  -> 101 ->  1012 -> ... you get the idea
    8. 111     111     111     111     111     111
    For this to work you will need to use Grid Framework as an intermediate layer before casting the ray:
    Code (csharp):
    1. var desiredSquare = (0, 0, 0);
    2. var rayDestination = grid.GridToWorld(desiredSquare);
    Then for every iteration step you adjust the desired square and convert it again to world coordinates for your ray cast.
     
  31. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 1.9.1 released
     
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Version 2.0.0 has been submitted
     
    Tinjaw likes this.
  33. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 2.0.0 released
     
    Tinjaw likes this.
  34. DaveHawes667

    DaveHawes667

    Joined:
    Jun 3, 2016
    Posts:
    2
    Just updated to 2.01 and getting this error....

    Assets/Plugins/GridFramework/Grid Rendering/GFGridRenderManager.cs(4,29): error CS0246: The type or namespace name `GFGrid' could not be found. Are you missing a using directive or an assembly reference?
     
  35. LunaTrap

    LunaTrap

    Joined:
    Apr 10, 2015
    Posts:
    120
    Hi @hiphish

    Im very interested in your asset, i just i need to know if your system can do any of these things

    1-have more than 1 grid, with limited size, im not interested in having infinite grid, this is for my AI that moves in grid movement so they can move from the combat zone, i want each room of my game to have its own grid, is this possible?

    2-Can i get a reference to a list of grid positions around an object? let me explain, i want to have explosive barrels, that when they explode, they will spawn fire fields around them, so, is there any way of doing something similar to Physics.OverlapBox so i can get references to all the detected grid possible spaces, so then i will spawn a fire field on each of those spaces. like this:



    Im very insterested, thanks for your help
     
  36. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    You seem to have leftovers from version 1, there are no classes prefixed with 'GF' in version 2 anymore. You will have to delete all the files from version 1 and 2 and then import version 2 from the Asset Store again. Delete the directories 'Editor/Grid Framework', 'Grid Framework', 'Plugins/Grid Framework' and 'WebPlayerTemplates/Grid Framework'. If you were on the latest version of 1.x some of those directories might not exist. Version 2 is easier to manage, everything will be stored under 'Plugins/GridFramework'; the reason version 1 had to be spread out like this was because of limitations in earlier versions of Unity.
     
    DaveHawes667 likes this.
  37. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi LunaTrap, I will answer your questions separately.

    Sure, a grid is just a component attached to a GameObject, you can have as many of them in your scene as you wish. Grids are very small in memory (just a few float values), so that shouldn't be a concern. Grids always are infinite by their very nature, so if you only want to use a finite slice of a grid you will have to limit your logic to that slice. What I like to do is use the 'From' and 'To' of the renderer for that, that way I the visual representation matches with the logic. Here is a little demonstration:
    Code (csharp):
    1. var renderer = GetComponent<Parallelepiped>();
    2.  
    3. var left = renderer.From.x;
    4. var bottom = renderer.From.y;
    5. var right = renderer.To.x;
    6. var top = renderer.To.y;
    The 'Parallelepiped' is the renderer used to display your grid in the scene. There can be more than one way of "displaying" a grid, that's why a separate object has the task of generating a finite set of lines and points to draw.

    Just to avoid misunderstandings, a grid in Grid Framwork is not a collection of tiles, it is a grid in a mathematical sense that functions as a coordinate system. That's why all grids are infinitely large.

    With that said, if you want a discrete collection of points it's easy to get one:
    Code (csharp):
    1. // using the values we obtained above
    2. var width = right - left;
    3. var height = top - bottom;
    4. // Tile is your own type, it could be something as simple a Vector2, or a struct or a class
    5. Tile[,] tiles = new Tile[width, height];
    6. for (var i = left; i <= right; ++i) {
    7.     for (var j = bottom; j <= top; ++j) {
    8.         tiles[i, j].position = grid.GridToWorld(new Vector3(i, j, 0));
    9.     }
    10. }
    What we have done he is assume that there is some type 'Tile' which will represent a tile in your game and store a set of tiles in a 2D array. We then iterate over the grid storing the tile's world-position generated from its grid-position. You can then write your entire game logic operating on these tiles.

    Another idea if you don't want to keep track of your tiles is to query the grid position of every object in the scene:
    Code (csharp):
    1. var barrelPosition = grid.WorldToGrid(barrel.position);
    2. var maxDistance = 2f;  // just as an example
    3. foreach (var go in possibleObjects) {
    4.     var position = grid.WorldToGrid(go.transform.position);
    5.     var connection = barrelPosition - position;
    6.     if (position.sqrMagnitude < maxDistance * maxDistance) {
    7.         // accept the object
    8.     }
    9. }
    If you don't need to query objects and only want to spawn new ones we can make it simpler:
    Code (csharp):
    1. var barrelPosition;
    2. var radius = 2;  // just as an example
    3. for (var i = barrelPosition - radius; i <= barrelPosition + radius; ++i) {
    4.     for (var j = barrelPosition - radius; j <= barrelPosition + radius; ++j) {
    5.         SpawnFire(grid.GridToWorld(new Vector3(i, j, 0));
    6.     }
    7. }
    The reason there is no "just add water" solution built in is that different games have different needs. Some want to maintain the state of their game in an array, some want to filter collections, and some only want to convert coordinates. What data type should a tile even be? How should the position be stored? 2D or 3D? Float or integer? It is impossible to fit every project, so my goal is to make it as simple as possible to write a solution that fits your needs.

    I hope this answers your questions.
     
  38. DaveHawes667

    DaveHawes667

    Joined:
    Jun 3, 2016
    Posts:
    2
    Thanks that sorted it.
     
  39. LunaTrap

    LunaTrap

    Joined:
    Apr 10, 2015
    Posts:
    120
    thanks a lot for the support, therefore, sold! :D
     
  40. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 2.0.1 released
     
  41. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    I have a feature request. I would like to be able to set a grid by the distance from center point to center point. So, for example I want the center of a hexagon to be 1000 units distance from the center of an adjoining hex. All I can set now is the radius, which means I would have to manually calculate the appropriate radius. It would be so much easier to have a toggle between radius and center-to-center.

    Thanks.
     
  42. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    What you want is basically two different implementations of hex grids that have a common interface. I think that's overkill for such a simple task, but there is another way: the distance between the centres of two hex grids is the same as the radius of the inscribed circle of one hex, which is the same as the Height of a hex. If I make the Height writable it can compute the corresponding Radius for the grid. It would not increase the surface area of the interface and it would give you the functionality you want. Would that be an acceptable compromise?
     
  43. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    That would be awesome. Thank you.
     
  44. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm done, replace the following properties in Plugins/GridFramework/Grids/Layered/Hexagonal/HexGrid.cs
    Code (csharp):
    1.         /// <summary>
    2.         ///   1.5 times the radius.
    3.         /// </summary>
    4.         /// <value>
    5.         ///   <para>
    6.         ///     Shorthand writing for <c>1.5f * Radius</c>. When setting the
    7.         ///     value of this property you are implicitly setting the
    8.         ///     <c>Radius</c> of the grid. Because this is a computed property
    9.         ///     floating point rounding errors might apply.
    10.         ///   </para>
    11.         /// </value>
    12.         public float Side {
    13.             get {
    14.                 return Radius * 1.5f ;
    15.             } set {
    16.                 Radius = value / 1.5f;
    17.             }
    18.         }
    19.  
    20.         /// <summary>
    21.         ///   Full width of the hex.
    22.         /// </summary>
    23.         /// <value>
    24.         ///   This is the full vertical height of a hex, the distance from one
    25.         ///   edge to its opposite (<c>sqrt(3) * Radius</c>). When setting the
    26.         ///   value of this property you are implicitly setting the
    27.         ///   <c>Radius</c> of the grid. Because this is a computed property
    28.         ///   floating point rounding errors might apply.
    29.         /// </value>
    30.         public float Height {
    31.             get {
    32.                 return Radius * Mathf.Sqrt(3f);
    33.             } set {
    34.                 Radius = value / Mathf.Sqrt(3f);
    35.             }
    36.         }
    37.  
    38.         /// <summary>
    39.         ///   Distance between vertices on opposite sides.
    40.         /// </summary>
    41.         /// <value>
    42.         ///   This is the full horizontal width of a hex, the distance from one
    43.         ///   vertex to its opposite (<c>2 * Radius</c>). When setting the
    44.         ///   value of this property you are implicitly setting the
    45.         ///   <c>Radius</c> of the grid. Because this is a computed property
    46.         ///   floating point rounding errors might apply.
    47.         /// </value>
    48.         public float Width {
    49.             get {
    50.                 return Radius * 2f;
    51.             } set {
    52.                 Radius = value / 2f;
    53.             }
    54.         }
    And to make the properties show in the inspector change Plugins/GridFramework/Editor/Inspectors/Grids/HexGridEditor.cs
    Code (csharp):
    1.         [SerializeField]
    2.         private static bool _showProperties = true;
    3.  
    4.         public override void OnInspectorGUI() {
    5.             RadiusFields();
    6.             DepthFields();
    7.             OrientationFields();
    8.  
    9.             _showProperties = EditorGUILayout.Foldout(_showProperties, "Properties");
    10.             if (_showProperties) {
    11.                 ++EditorGUI.indentLevel;
    12.                 HeightFields();
    13.                 WidthFields();
    14.                 SideFields();
    15.                 --EditorGUI.indentLevel;
    16.             }
    17.  
    18.             if (GUI.changed) {
    19.                 EditorUtility.SetDirty(target);
    20.             }
    21.         }
    22.  
    23.         private void HeightFields() {
    24.             _grid.Height = EditorGUILayout.FloatField("Height", _grid.Height);
    25.         }
    26.  
    27.         private void WidthFields() {
    28.             _grid.Width = EditorGUILayout.FloatField("Width", _grid.Width);
    29.         }
    30.  
    31.         private void SideFields() {
    32.             _grid.Side = EditorGUILayout.FloatField("Side", _grid.Side);
    33.         }
    I'm going to see if I can do something similar for the other grids as well before I publish an update, these snippets should bridge you over in the meantime.
     
  45. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    Excellent. Thanks for the great support! I'll implement that tomorrow at work.
     
  46. BowesAcademy

    BowesAcademy

    Joined:
    Feb 18, 2014
    Posts:
    133
    Hello Hipish! Just downloaded the new Grid Framework. I cannot make sense of the documentation -there's no single pdf just a bunch of html files that I do not understand. Where is the documentation?
     
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The documentation is in HTML format. The easiest way to get to it is in Unity's menu Help -> Grid Framework Documentation, or if you want to open the files manually Assets/GridFramework/Documentation/html/index.html

    index.html is the first page, from there you will find the user manual and the scripting reference. There is no need to dig through the HTML pages manually.
     
    BowesAcademy likes this.
  48. Maxii

    Maxii

    Joined:
    May 28, 2012
    Posts:
    45
    Just downloaded 2.01. I usually put all your code into a couple of Visual Studio projects so I can compile my game code in VS prior to uploading it to Unity. The VS compiler doesn't like your use of unassigned local variables and won't compile (even though I'm sure Unity's Mono compiler will not see it as an issue...). In particular, the 'aligned' Vector3 in HexGridEditor.AlreadyAligned(Transform) is unassigned and throwing an error. I'll make the default assignment myself for now, but thought you should know so you can fix in your next update.
     
  49. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Unity and Mono don't complain. Could you please copy-paste me the list of compiler errors? I have checked the one in AlreadyAligned and my guess why the two don't complain is because the compiler has figured that there will always be a rectangular or hexagonal grid when the function is called:
    Code (csharp):
    1. Vector3 aligned;
    2. if (_rectGrid) {
    3.     aligned = ...
    4. } else if (_hexGrid) {
    5.     aligned = ...
    6. }
    7. var inPlace = // something using 'aligned'
    I'm going to make this one safer, but since I don't see errors I don't know where else these things could lurk.
     
  50. Maxii

    Maxii

    Joined:
    May 28, 2012
    Posts:
    45
    That was the only VS .Net compiler error that I found. I've seen this Mono/VS compiler result difference before wrt uninitialized local variables.