Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Grid Framework [scripting and editor plugins]

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

  1. guest1234567890

    guest1234567890

    Joined:
    Jul 17, 2012
    Posts:
    1
    Does this Framework work in Mobile Games?
     
  2. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Yes, it doesn't use any fetures the mobile versions don't have. The only problem could be rendering the grid with the built in rendering method; there is a bug in Unity 3 (I don't know about Unity 4 yet) where GL renderings using a lit shader appear white. The solution, aside from filing a bug report to the Unity developers, is to use an unlit shader, which is better for mobile games anyway. The default shader is lit, but you can just drop in any other shader you want to use. A while ago another user had this problem and here is what I replied:

    If you have any other questions don't hesitate to ask.
     
  3. Henchmen - Sebas

    Henchmen - Sebas

    Joined:
    Jan 11, 2013
    Posts:
    3
    This looks like a fantastic plugin. As I am still getting to grips with Unity, with a definite game idea in the head - I have 2 questions.

    Does it support the free version of Unity?

    Is it - or would it be possible to raise and lower individual, or groups of grid points? To create a landscape, if you will.
     
  4. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    First question: Yes, it was even written using the free version of Unity, so there is nothing you won't have access to.

    Second question: This totally depends on what exactly you want to do. All grids are three-dimensional, so assuming you want to make your landscape out of blocks arranged on a grid, then you could just align the blocks along the grid. The spacing can be set individually for each axis, so you won't need to raise blocks a full world unit if you don't want to. If you wanted to lower (or raise) a block one grid unit during runtime you could simply do something like this:
    Code (csharp):
    1.  
    2. var myBlock: GameObject;
    3. var myGrid: GFRectGrid; //the rectangular grid you want to use for reference
    4. var steps: int = -1; //how many units do you want to move up or down?
    5. myBlock.transform.position += steps * myGrid.spacing.z * Vector3.up; //spacing is the size of the grid cells in world units
    6.  
    If you can explain to me in more details what exactly you have in mind I could answer your question more precisely.
     
  5. Henchmen - Sebas

    Henchmen - Sebas

    Joined:
    Jan 11, 2013
    Posts:
    3
    Thanks for the reply! I will purchase and give it a whirl as soon as possible.

    What I meant was the ability not to add blocks, but to raise individual points of the grid itself, thus angling the vertices connecting them.

    Like in the below sketch, only the red dots are lifted. Are you familiar with games like Rollercoaster Tycoon and its grid? The game itself allows you tools to lift just corners or whole rectangles.

    This would allow your grid system to function as the basis for (classic) strategy games or even building games that require a more complex - landscape-like - grid.

    [​IMG]

    (Made in Illustrator)
     
    Last edited: Jan 17, 2013
  6. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Ah, what you mean is not a grid, but a graph representation. A grid in Grid Framework is infinitely large and three-dimensional, a graph is just a finite set of vertices and edges between the vertices. A representation of a graph is a drawing that visualies the graph.

    In you case what you want is to place the vertices of the graph onto the grid vertices. This is no problem using Grid Framework, the problem will be drawing the graph and its tiles. The best solution I can think of would be using Grid Framework to calculate the world position of the vertices and then build a mesh around them. A mesh consists of triangles, so you need to divide the tiles (squares) of your landscape into two triangles each. I made a little illustration on top of your drawing (I didn't cover all of it, but you get the idea)

    [​IMG]

    I never dealt with mesh generation, but there is some information in Unity's documentation. The most important part is getting the vertices right. in this case I'd use a two-dimensional array where the position of an entry corresponds to the map coordinates of the vertex. If you look at my "moving with obstacles" examples, I did a similar thing where i create a boolean matrix and then have each obstacle mark its entry as occupied. In your case it would look something like this (I'm using C# here because the UnityScript documentation for arrays is really lacking)
    Code (csharp):
    1.  
    2. Vector3[,] vertexMatrix = new Vector3[sizeX, sizeY]; //this is where we store our vertices as Vector3 world coordinates
    3.  
    4. // a function that returns the world coordinates for a vertex
    5. Vector3 CalculateWorldCoordinates(int xPos, int yPos, int height, GFRectGrid grid){
    6.    return grid.GridToWorld(xPos, yPos, height);
    7. }
    8.  
    9. // and now a function that puts those coordinates into the matrix
    10. void registerVertex(int xPos, int yPos, int height, GFRectGrid grid){
    11.    vertexMatrix[xPos, yPos] = CalculateWorldCoordinates(int xPos, int yPos, int height, GFRectGrid grid);
    12. }
    13.  
    14. // we also want to read entries from the matrix for building the mesh
    15. Vector3 readVertex(int xPos, int yPos){
    16.    return vertexMatrix[xPos, yPos];
    17. }
    18. // note how the height is obtained without asking for it
    19.  
    Now, the next question would be how to get the information into our little class. The easiest way would be to use a plain text file like I did in the parsing example where I design a breakout field in a text file and have a text parser build the level on a grid for me. Or you could do it randomly and have randomly generated levels. Maybe you would like to move something in the editor and then have that form your mash? In that case the idea is the same, excpet you don't need the CalculateWorldCoordinates function because it would already be in world coordinates. You would have to do your own editor scripting; Grid Framework can help you by using the auto snapping feature. I know there is a mesh generation editor extension on the Asset Store, but i can't remember the name right now. Peronally I'd go for the text file, it#s simple and it#s clean, plus you can make the game moddable for users so they can add their own levels.

    I hope this sheds some light on what Grid Framework can do and what it cannot.
     
    Last edited: Jan 17, 2013
  7. Henchmen - Sebas

    Henchmen - Sebas

    Joined:
    Jan 11, 2013
    Posts:
    3
    C# is fine, I prefer it. Great response, thank you! It's something I'll have to sink my teeth in to get a real feel for it, but it's already very helpful.

    It might be a while and I hope to remember it, but I'll get back to you :)
     
  8. bngames

    bngames

    Joined:
    Jul 3, 2012
    Posts:
    56
    When building for iOS:

    Assets/Plugins/Grid Framework/GFGrid.cs(3,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?

    Error building Player because scripts had compiler errors
     
  9. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    The offending line:
    Code (csharp):
    1.  
    2. using UnityEditor;
    3.  
    This is needed because I'm using the EditorApplication class, this is what it says:
    The problem with this is that for Grid Framework to work with JavaScript the script needs to be in the Plugins folder (it shouldn't matter for C#). I could have sworn building worked fine in the past, I'll be looking into this issue immediately.

    EDIT:
    Go it! Remove or comment out the lines 3, 146 and 147 in Plugins/GFGrid.cs.
    Code (csharp):
    1.  
    2. using UnityEditor;
    3. ...
    4. if(EditorApplication.isPlaying  !drawOnPlay)
    5.     return;
    6.  
    I'll see if it's worth keeping thos elines and finding a workaround or if I should just throw them away. The idea was to prevent drawing the vertex matrix while playing because it can be a giant performance hog, but maybe I was too cautious. Besides, there is an option for that anyway. I'll upload a fix once I decide. In the meantime commenting away those lines gets the project to build properly.

    EDIT 2:
    OK, I can get it to build properly and keep the code by replacing EditorApplication with just plain Application:
    Code (csharp):
    1.  
    2. if(Application.isPlaying  !drawOnPlay)
    3.  
     
    Last edited: Jan 20, 2013
  10. apollyonbob

    apollyonbob

    Joined:
    Jan 16, 2012
    Posts:
    7
    Before I pepper you with questions, just wanna say thanks for making this. It's a great value at $20 and saves me a bunch of time trying to reinvent a wheel you have obviously done a very good job of making.

    Got a couple of questions for you:

    First: I want to make a grid that is, specifically, 6x10 squares. It's for a puzzle board. How would you recommend going about doing this? My thinking is, with the way the framework is set up at the moment, of creating "walls". However, I don't see a way of detecting all squares that a wall intersects. I saw in your example that there was a way to get the nearest face/square that an object is in, but I'm looking to just make one object that acts as a wall, or gameboard.

    Second: I'm confused about the relationship between Spacing, Minimum Spacing and Size. I would expect that Size limits the size of the Grid, but it does not seem to have any effect on the Grid itself - the grid remains infinitely large. So is there any chance you can explain the expectation of that relationship? I mean, I can see that Size gets used in the code, but again, I'm confused as to how exactly.
     
  11. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Thank you for your kind words. I'll answer the seconf question first, because it is more general.

    Spacing is how dense the grid is, meaning how far apart the vertices are. Spacing does influence the result of calculations. Minimum spacing was put in place to provide a lower limit in the editor, the reason is that if you lower spacing the drawing and rendering has more lines and thus takes up more system ressources. You won't notice it when not drawing or rendering, but a very dense grid slows the application down siginifacantly. If the grid gets too dense the editor can even crash. Looking back this was maybe not the best way to do it and I am considering removing it since it seems rather confusing. Maybe I should just hardcode a limit or 0.1, I can't imagine anyone ever needing such a dense grid anyway.
    The "size" is the size of the rendering. You are right, all grids are alway infinitely large, but often that's not what you want. For instance, the rendering can only have a finite size. To this end it uses the "size" variable, where the limits of the grid drawing/renderng stretch in each direction (using world units). I should make it so it stretches only half the amount to both sides, like the size property of Transforms in Unity, instead of the full size. The other time you would need size is when you don't want an infinitely large grid and want to put up a limit of sorts. In the grid movement example I use the size to prevent the sphere from going too far. I could use any arbitrary value, but it makes most sense to use the same limit as for the rendering.

    To make a 6x10 grid I would use custom rendering range instead of sze. make the grid start at (0, 0, 0) and let it end at (6, 10, 0) * spacing. I need more concrete information of how you want your walls to work, a quick drawing or something. There is an example included for grid-based movement with obtacles where I place cubes on tiles and then have them register into a matrix. The matrix is used for the sphere to know where it can and where it cannot go, no physics involved. Of course you could also use physics (raycasting and collision detection) if you want, I didn't use it simply to demonstrate how it can be done without.
     
  12. apollyonbob

    apollyonbob

    Joined:
    Jan 16, 2012
    Posts:
    7
    Thanks for the reply!

    I should have been more clear as to how my two questions tied together. The second one was in part driven by the first - that is to say, I want to make a grid that is 6x10 inclusive. I want to have the Grid not allow movement beyond the borders of the 6x10. Right now it does because the Grid is infinitely large, even though it is rendered as 6x10.

    I did the Custom Rendering as you suggested, and that worked out fine for displaying the Grid. But the pieces continued moving ad infinitum regardless of what I put in the Size or the Custom Rendering.

    I mean, you're right - an infinitely large grid isn't what I want haha. But I need more than just to limit the rendering, I need the snapping/alignment logic to limit as well.

    However, I'm starting to suspect that in the meanwhile, it might simply be easier to do it either create a matrix as you suggest, or to use a Screen Point to Ray/physics to see if the mouse is moving outside the bounds of the puzzle.

    Thanks again! Still saves me a ton of time haha.
     
  13. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    OK, I see what you mean. It's easy:
    Code (csharp):
    1.  
    2. var myGrid: GFGrid;
    3. var lowerLimit: Vector3 = myGrid.GridToWorld(Vector3.zero);
    4. var upperLimit: Vector3 = myGrid.GridToWorld(new Vector3(6, 10, 0));
    5. ...
    6. // let calculatedPosition be the result of your calculation
    7. objectPosition = Vector3.Max(Vector3.Min(calculatedPosition, upperLimit), lowerLimit);
    8.  
    First we get the world coordinates of the lwer left tile and then the world coordinates of the upper right tile and store those as hard limits. Then when trying to move the object we perform the calculation the usual way and finally we clamp it between the limits. For one-dimensional values you would use Mathf.Clamp, but for vectors we need to combine Min and Max to get a similar thing
    http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Min.html
    http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Max.html
     
  14. apollyonbob

    apollyonbob

    Joined:
    Jan 16, 2012
    Posts:
    7
    Wow! Okay, yeah that's easy enough! Thanks a bunch man :D
     
  15. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    20
    Noob question here: is this framework UnityScript friendly? I saw an earlier mention of an example with delegates and event handlers using C#. Will I be missing out on stuff if I use unity script?
     
  16. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Grid Framework itself has no problem with UnityScript, but events and delegates are simply a feature that missing from UnityScript (or at least it's undocumented) in general, you'll miss out on it no matter what Unity extensions (if any) you use. UnityScript is nice as just a scripting language for the Unity API on one game object at a time, but once you want to do more advanced and interconnected stuff the proper way is C#. I started writing in UnityScript because that's what all the examples and tutorials were written in, but for Grid Framework I switched to C# and after that I never went back. It's not even harder to code in, in fact it's easier because the code is clearer and everything is documented by .NET and Mono. Do you want to know what exactly the limitations of foreach are? With UnityScript you'll need to search the internet for bits and pieces of (possibly outdated) information, with C# you just read the .NET programing guide.

    Bottom line, I encourage everyone to use C#, but if for whatever reason you don't want, you can still use UnityScript, Grid Framework has no limitations, except what the languge itself is missing.
     
  17. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    20
    Thanks for the info hiphish. I'm more familiar with javascript as I also use it for web stuff. I tried C# briefly before, but the verbosity of it slowed me down and I didn't really have a reason to work on it long enough to gain speed. I might give it a try again at some point when I have more time to spend on game making.
     
  18. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    That's funny, because the lack of verbosity is what I didn’t like about UnityScript ^^ It felt too much like "magic" to me where I write one thing but it can mean two things. Well, use whatever you are comfortable with.
     
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Update time. Let's go over what's new:

    New example: sliding block puzzle
    [​IMG]

    It might not look like much, but this is the most advanced example yet; it's similar to the movement with obstacles example where we use a matrix to store which tiles are allowed and which are forbidden. The tricky part is that now objects can span more than one tile and all of them have to be free. The solution is to break up the obstacle into one tile large parts, then check them all individually and finally assemble the answer from the individual answers.
    The end result is that it feels like collision without actually using collision. Now, you might be wondering why not just use actual collision detection? For one, Unity's collision detection requires you to move objects through the physics engine instead of directly. This means instead of moving the block like you would in real life you need to use force, like dragging the block with a rubber band. This feels just wrong, especially on a touch device. If you move objects directly (i.e. using their Transform component) the physics engine is likely to miss intersections. The other reason is that Unity's collision detection just isn't made for packing objects together this tighly, sooner or later things will just randomly fly in all directions like the puzzle exploded or something.
    Don't get me wrong, PhysiX was certainly developed by talented people who know more than I do, but it was written with 3D action games in mind and trying to get it to work in such a scenrio is like trying to fit a square peg into a round hole; you might get it to kind of work well enough eventually, but in the time it took you you might as well have written your own solution which you have proper control over. Thanks to Grid Framework we can automate all the unit conversion between world space and grid coordinates.

    Changes: I removed the minimumSpacing (rectangular grids) and minimumRadius (hex grids) variables because they were stupid. The reason why they existed in the first place was to prevent the user from setting too low or nonsensical values for spacing and radius. The proper way to do this would be to use accessors (also called properties), but Unity's editor scripting documentation is rather lacking, so I couldn't figure out how to make the values not reset all the time.
    I finally found the solution (that's a topic for another time) and now I hardcoded a lower limit of 0.1 for both. I think that's a reasonable value, but if you need to go lower please let me know. The way it works now is that if you try to set the value to anything lower than 0.1 it will automatically default to 0.1. I was also able to get rid of other ugly parts inside the source code and clean things up thanks to accessors, but I don't think you will notice any difference
    In terms of scripting this has no real consequences for you, just use spacing and radius like you did before.

    Fixes: One major bug was a typo that could prevent the project from building. I also removed the redundant "Use Custom Rendering Range" flag from the inspector, now opening or closing the "Custom Rendering Range" foldout toggles the value (in terms of scripting the varaible still exists, it's just the way you set it in the inspector). Speaking of inspector and foldout, previously the state of the "Draw Render Settings" foldout reset each time you entered or exited play mode. Now the settings will stick and it will be individual for each grid type. There is also the obligatory under-the-hoods improvements, but there is nothing particular worth mentioning there.
     
  20. artonator

    artonator

    Joined:
    Sep 12, 2009
    Posts:
    67
    Hi,

    I was trying Sliding puzzle scene and I got following error when I try to move blocks... can you tell me what I need to do to fix it?

    IndexOutOfRangeException: Array index is out of range.
    SlidingPuzzleExample.RegisterObstacle (UnityEngine.Transform obstacle, Boolean state) (at Assets/Grid Framework/Examples/Sliding Puzzle/Scripts/SlidingPuzzleExample.cs:59)
    DragBlock.Start () (at Assets/Grid Framework/Examples/Sliding Puzzle/Scripts/DragBlock.cs:13)


    I imported it to the new clean project.


    Edit - I changed custom rendering range 6 x 6 and it works.... is there anyway I can prevent blocks sliding side way?
     
    Last edited: Feb 12, 2013
  21. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Sorry, I just noticed today that renderign range had reset due to a change in the code I made after finishing the example. Once you set the size to 6x6 it should stick. Preventing horizontal sliding is easy, all you want to do is force the destination to keep the current X-value. In the DragBlock script add the following line right after line 41:
    Code (csharp):
    1.  
    2. destination.x = transform.position.x;
    3.  
    A little explanation: The "destination" is where the cursor currently is (minus the offset from the object's centre). We can then modify the destination vector to our likings, like forcing the X-coordinate of the destination to be exactly the same as the current X-coordinate. Your code should now say
    Code (csharp):
    1.  
    2. destination.z = transform.position.z;
    3. destination.x = transform.position.x;
    4.  
     
  22. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Why does the grid only render in ortho? I've a 2.5D platformer that allows block placement and am using the grid as a guide for the user to easily place blocks. However it won't render in a perspective camera. Any ideas? If I switch my camera to ortho it renders, but I want to use perspective.
     
    Last edited: Feb 13, 2013
  23. artonator

    artonator

    Joined:
    Sep 12, 2009
    Posts:
    67
    I got it working, thank you for your input and constant support for your frame work....
    your frame work worth every single penny.

    One mor e question, is there a way I can apply grid constraint activated in editor as well?
     
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    I'm glad you like it. Unfortunately there is no such option in the Grid Align Panel as it wuld be too specific, but maybe you can try adding ExecuteInEditMode to your script so it works in the editor as well:
    http://docs.unity3d.com/Documentation/ScriptReference/ExecuteInEditMode.html

    I have to avoid getting into too specific stuff since Grid Framework is designed as a Framework, not a kit where you just "add water". Going too much into specifics will bloat the code with features that would be useless for all except very specific cases. Of course I still take suggestions form users and many do make it into Grid Framework, I just don't feel that your case is appropriate. You have the full source code and extending the Grid Align Panel or making a new script that does what you want is always an option. I'd go with the latter and write something like this (pseudocode)
    Code (csharp):
    1.  
    2. if(holding down Shift)
    3.     freeze transform.position.x;
    4.  
    Or you just drag the object inside the editor only using the green arrow, then it will just move along the Y-axis. Maybe I misunderstood you and you just want objects to snap to the grid in the editor? You can find the Grid Align Panel under Window, then drag your grid into the field and you can use the various settings inside the ditor.

    There is two files for documentation, one user manual and one scripting reference, they are in the global Grid Framework folder in your project. The YouTube videos are just additional material to go along the examples because some people learn better from videos. Also, here is two grids rendering in persepcetive mode:
    http://tinypic.com/r/2zolyiv/6

    Could you please give me more input so I can recreate your setup? At what position is your camera, what rotation, where is your grid and what settings are you using? Rendering in perspective mode has been as much of a priority as orthographic mode, but it's possible I missed something, so I need your input to find the issue.
     
  25. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Yeah, edited my post and found the documents; thank you!

    Below is a screenshot of my grid and camera. It works fine in Editor, but not Play mode.

    Grid:
    [​IMG]

    Camera:
    [​IMG]
     
    Last edited: Feb 13, 2013
  26. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    OK, I replicated your setting and here is what I see, even if I make an entirely new scene:
    http://i47.tinypic.com/14tbuq.png
    http://i49.tinypic.com/3136q0n.png

    When you are in editor mode and you click the camera you should see a cone of vision. Is the grid inside the cone? During play mode if you into scene eview is the grid stil inside the cone? Please make sure the grid component and the GFGrid Render Camera script (on the camera) are still enabled during play mode.

    If everything is still in place I need your help in debugging. In the GFGridRenderCamera.cs script add after line 17 the following:
    Code (csharp):
    1.  
    2. Debug.Log(grid.name);
    3.  
    and tell me what the console puts out. You should be getting the name of your grid object, if not then the camera is not aware of the grid's existence.
     
  27. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    The camera is always infront of the grid as it's locked to the grids size. I'm using Rotorz tile mapper. I created a grid to match Rotorz grid and my camera can't move outside the Rotorz grid size which is 100x100. My camera can move if that makes and difference, but even if I disable the moving it still doesn't show. All scripts are still enabled during play mode.

    Console output the following.

    Code (csharp):
    1.  
    2. Grid
    3. UnityEngine.Debug:Log(Object)
    4. GFGridRenderCamera:OnPostRender() (at Assets/Plugins/Grid Framework/Grid Rendering/GFGridRenderCamera.cs:18)
    5.  
    Edit: If I put the culling mask to "Nothing" it displays in Play mode, but then nothing else does.

    Edit 2: Seams to be adding to Layer 9? This is where my NGUI is sitting.
     
    Last edited: Feb 13, 2013
  28. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Hmmm, I can't make any sense of this. The way Grid Framework renders has nothing to do with layers, so how come changing the culling mask makes a difference? Could you please make a new scene with just a grid and a camera and see if it renders there? Then try adding parts of your level and see where it breaks. Also, why is your grid rotated 90 degrees? I don't think this has anything to do with it (I tried a rotated grid already), but it wouldn't hurt trying a non-rotated grid. Could you please post some screenshots of your scene in scene view while playing? if you don't want it publicly visible you can send me a PM. make sure your camera is selected while taking the screenshot and set the persepective so I can see how the cone touches the grid.
     
  29. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Seams to work fine in a new scene, but my current scene it messes up only if I have the NGUI gameObject enabled. Once I disable it it seams to work fine again. Really weird. Don't think it's anything from Grid Framework causing this, but some sort of Camera jackedupness going on (new scene it worked fine with NGUI, so makes this pretty odd).
     
    Last edited: Feb 13, 2013
  30. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Ok, it looks like anything that runs on every frame messes with the grid. For example I've some NPCs that walk back and forth and use raycasts every frame to determine collision (they're rigid bodies). Anything in view of the camera doing this will result in the grid going away and my NGUI is attached to my camera, which my camera can be moved, so it moves the GUI with the camera resulting in the grid always being hidden. Do I just need to call renderGrid every frame to resolve this?
     
  31. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    OK, now I know at least in what direction to look. I'm downloading the free version of NGUI right now, I'll post here once I find something out. Please subscribe to this thread so you get notified.
     
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    The GFGridRenderCamera script already calls RenderGrid every frame, more precisely during OnPostRender. Rigidbodies with raycasting you say... I'll give a try; I have several example where objects do things every frame and didn't see any problems :/
     
  33. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Ok, I completely re-created my MainCamera and now it works perfectly fine.. lol. Thank you very much for all your help and pointers!
     
  34. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    I'd still like to know what was going on. Do you happen to know what you did differently this time?
     
  35. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    I've no idea what happened. The camera was the original camera created with the scene. I simply created a new one, set the positions and everything to the same as the previous, disabled the previous, and it worked. I then deleted the previous and it now continues to work. NGUI was also messing up with the old camera which is now also fixed so I think my camera was just broken for whatever reason.
     
  36. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Ok, wrote 56 playmaker actions to handle all of GridFrameworks API (all working, all tested), but don't see anything in the API to change the color of a block on a grid. Basically I want mouseover highlighting of a block on the grid. Any suggestions on how to accomplish this?
     
  37. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
  38. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    I already have the mouseover and the world position to grid position done. I need to know how to change the color of the block on the grid only. This will allow me to highlight the spot on the grid that you moused over. I suppose I can just have another grid that's 1x1x1 that I place at that location and it has a different color and material, but that's kind of a silly workaround.
     
  39. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    You need to be more specific. Do you want to change the colour of a block (rectangular game object) that's in the scene or do you want to draw a rectangular highlight on top of existing level geometry? The former can be done using a simple OnMouseOver as long as the object has a collider. If it is the latter you need to compute the corner points of the rectangle and then draw using those points. The GL class can handle low-level drawing:
    http://docs.unity3d.com/Documentation/ScriptReference/GL.html

    To get the corner points you need the world coordinates of the face first and then us the grid's spacing:
    Code (csharp):
    1.  
    2. // cursorPoint is where the cursor is pointing at
    3. Vector3 face = myGrid.FindNearestFace(cursorPoint);
    4. // four corner pints, more if you want perspective
    5. Vector[] cornerPoints = new Vector3[4];
    6. cornerPoints[1] = face - myGrid.spacing.x * Vector.right + myGrid.spacing.y * Vector.up;
    7. cornerPoints[2] = face + myGrid.spacing.x * Vector.right + myGrid.spacing.y * Vector.up;
    8. cornerPoints[3] = face + myGrid.spacing.x * Vector.right - myGrid.spacing.y * Vector.up;
    9. cornerPoints[4] = face - myGrid.spacing.x * Vector.right - myGrid.spacing.y * Vector.up;
    10.  
    If this still doesn't answer your qustion could you please elaborate with a quick mockup? Take a screenshot and draw in an image editing application what you would want. It's hard for me to guess what you mean like that.
     
  40. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Lets forget the mouseover. I've already got that covered. I strictly want to take grid coordinates, take the surrounding lines, and give them a highlight. I believe your above answers that though. At any rate below is a image of what I am trying to accomplish.

    [​IMG]

    I'd also like to add this is a completely and totally awesome asset. Need to go give you a review!
     
  41. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    I see. Yes, the GL class is the way to go. Here is a little trick to get the points easily: the method GetVectorsityPoints can take a from and to vector and returns a list of points for use with the Vectrosity add on. Obviously points are points, so there is nothing stopping you from just using this list with whatever you want to. Here is what it would look like:
    Code (csharp):
    1.  
    2. // cursorPoint is where the cursor is pointing at
    3. Vector3 face = myGrid.GetfaceCoordinates(cursorPoint); //grid space
    4. Vector3 from = Vector3.Scale(face, myGrid.spacing);
    5. Vector3[] cornerPoints = myGrid.GetVectrosityPoints(from, from + Vector3.one);
    6.  
    We need Vector3.Scale because the from and to vectors are independent of spacing. If you play around with custom rendering range and spacing you will notice that they are independent from one another.

    You could manually call RenderGrid with these from and to coordinates instead of using the GL class manually, but there is no override for RenderGrid that lets you specify your own colours (I will add that now). You could also buy Vectrosity, it's a really great line drawing solution and Grid Framework can calculate corner points automatically for you. Back in the early days before there was any rendering i wasn't sure if I could write a good solution, that's why Vectrosity support is built in.
     
  42. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    I see, ok, will mess around with it and see what I can come up; I already own Vectrosity so is probably the best approach. I suppose the alternative is to spawn a prefab cube with highlighting shader on it.
     
  43. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    645
    Ok, my approach maybe silly but it works. I created a new grid inside of a gameobject that is from 0,0,0 to 1,1,1 so it's just 1 square. I then offset x/y/z by -0.5 so it'd be centered when spawned. I then spawned it into PoolManager far off screen. On mouseover I then just move it into position. This lets me use colors and materials on this 1x1x1 grid to do whatever highlighting I want. Thanks for the help!
     
    Last edited: Feb 16, 2013
  44. rscooke

    rscooke

    Joined:
    Feb 1, 2013
    Posts:
    4
    Sorry for my novice question. I have been using the snappingunits script with the Grid Framework and I would like a Gameoject to snap to a grid when it is created at runtime. How do I script so that a prefab that has snappingunits attached loads up and snaps to my loaded grid? A prefab wont allow me to add GFrect to the Grid parameter of SnappingUnits in the Inspector view. this needs to be done at runtime? Any help much appreciated.

    BTW, fantastic tool.
     
  45. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Hello

    The reason you can't do it in a prefab is because of how the Unity engine works. Grids are inheriting from scripts, which (through a series of more inheritance) inherit from the Object class. An object can only exist in a scene while prefabs exist outside of scenes. This means that trying to set an object as the value of a variable doesn't make sense for the preab, the object doesn't exist outside the scene it was created in. That is why we need to do it at runtime, preferably before the game really starts.

    Of course you can do it manually for each scene, but that would be tedious. If you look at the sliding puzzle example (or some others but that's the one that comes to my mind right now) you will notice we have a static class that does the managing and everything else communicates with that class. Here is a quick mockup:
    Code (csharp):
    1.  
    2. public static class GridManager{
    3.     private GFRectGrid _grid;
    4.     public GFRectGrid grid{
    5.         get{return _grid;}
    6.     }
    7.     public void RegisterGrid(GFRectGrid theGrid){
    8.         _grid = theGrid;
    9.     }
    10. }
    11.  
    The class is static, that means we don't need any instance of it, it exists throughout the entire project, it's accessible from anywhere and there is only this one. The variable _grid is private to make sure nothing can mess with it (the underscore character is just part of a naming convention, no deeper meaning), we use a public accessor to give other classes read-only access (see my recent blog post) and we have a function that allows us to register the current grid for the current level level. Now, how do we register the grid? Your grid is attached to a GameObject, so you need to attach a script that does the following:
    Code (csharp):
    1.  
    2. void Awake(){
    3.     GridManager.RegisterGrid(GetComponent<GFRectGrid>());
    4. }
    5.  
    I put it in Awake so it gets called once the game starts. As you can see I refer to class itself, no to any instance of it, because it is static. That's one of the great advantages of using a static class. I also use the generic version of GetComponent because that way I don't need to do any typecasting. Now that we have our grid registered we can use it. Add the following code to a script on your objects you want to snap in the game:
    Code (csharp):
    1.  
    2. void Start(){
    3.     GridManager.grid.AlignTransform(transform);
    4.     // or whatever you want to do
    5. }
    6.  
    Note that I used Start() this time, the reason is that Start() gets called after Awake(), making sure that the grid has already been registered by the time I need it. Now all you need to do is make sure every grid of yours has the second script attached, the rest will go automatically.

    I used C# because UnityScript cannot create classes just like that, each script you create is essentially a MonoBehaviour-derived class. If you prefer to use UnityScript you can use it for the second and third code, but the first code can only be written in C#. You can just copy-paste it and then keep using UnityScript for the rest, there is no problem with mixing languages, everything gets compiled to the same thing. (I would still encourage you to switch to C#, it has many features missing in UnityScript, but that's your choice)
     
    Last edited: Feb 23, 2013
  46. rscooke

    rscooke

    Joined:
    Feb 1, 2013
    Posts:
    4
    Hiphish, thankyou for your help. I am now able to get a cube to snap to the grid when created at runtime. Once again, Grid FrameWork is an excellent tool.
     
    Last edited: Feb 23, 2013
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    548
    Did you solve your other problem? I can see it in the email notification I got but you have edited your post, so I'm not sure.
     
  48. rscooke

    rscooke

    Joined:
    Feb 1, 2013
    Posts:
    4
    Yes I did thanks.
     
  49. Udorn

    Udorn

    Joined:
    Feb 9, 2013
    Posts:
    10
    Hi,

    I'm just trying my first steps with the Grid framework and I have some trouble rendering a hex-grid in the game. I've added the red material from the examples as "Render Material", but the grid is always shown in a black color. I also added a sphere on the same z position (xy plane) with the same material and it is shown in bright red. So the directional light doesn't seem to be the cause of the problem.

    Can you please give me some hints, on how to select a color for rendering the grid? Is one of the examples rendering the grid in some non-black color?
     
  50. SimtropBuggi

    SimtropBuggi

    Joined:
    Feb 15, 2013
    Posts:
    98
    Excellent project, I have a question. Would it be possible to use a 2d grid over a terrain and have the grid-lines conform to the surface?