Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by JunglePeople · Jun 21, 2016 at 11:57 AM · scripting problemtilemap

Tile Map issue

Hi, im following a Lynda tutorial about making a tile map and im having a small issue in one of the scripts. For some reason, my script doesn't recognize a Vector2 variable called tileSize, i've watched over and over, rebuild the script as many times as i could but still got frustrated.

this is the code:

using UnityEngine; using System.Collections; using UnityEditor;

public class TilePickerWindow : EditorWindow {

 public enum Scale
 {
     x1,
     x2,
     x3,
     x4,
     x5
 }


 Scale scale;
 Vector2 currentSelection = Vector2.zero;
 public Vector2 scrollPosition = Vector2.zero;

   [MenuItem("Window/Tile Picker")]
 public static void OpenTilePickerWindow()
 {
       var window = EditorWindow.GetWindow(typeof(TilePickerWindow));
       var title = new GUIContent();
       title.text = "Tile Picker";
       window.titleContent = title;
 }

 void OnGUI()
 {
     if(Selection.activeObject == null)
         return;

     var selection = ((GameObject)Selection.activeObject).GetComponent<TileMap>();
     
     if (selection != null)
     {
         var texture2d = selection.texture2d;
         if(texture2d != null)
         {
             scale = (Scale)EditorGUILayout.EnumPopup("Zoom", scale);
             var newScale = ((int)scale) + 1;
             var newTextureSize = new Vector2(texture2d.width, texture2d.height) * newScale;
             var offset = new Vector2(10, 25);

             var viewPort = new Rect(0, 0, position.width - 5, position.height - 5);
             var contentSize = new Rect(0, 0, newTextureSize.x + offset.x, newTextureSize.y + offset.y);
             scrollPosition = GUI.BeginScrollView(viewPort, scrollPosition, contentSize);

             GUI.DrawTexture(new Rect(offset.x, offset.y, newTextureSize.x, newTextureSize.y), texture2d);

             var tile = selection.tileSize * newScale;
             var grid = new Vector2(newTextureSize.x / tile.x, newTextureSize.y / tile.y);

             var selectionPos = new Vector2(tile.x * currentSelection.x + offset.x,
                                             tile.y * currentSelection.y + offset.y);

             var boxText = new Texture2D(1, 1);
             boxText.SetPixel(0, 0, new Color(0, 0.5f, 1f, 0.4f));
             boxText.Apply();

             var style = new GUIStyle(GUI.skin.customStyles[0]);
             style.normal.background = boxText;

             GUI.Box(new Rect(selectionPos.x, selectionPos.y, tile.x, tile.y), "", style);

             var cEvent = Event.current;
             Vector2 mousePos = new Vector2(cEvent.mousePosition.x, cEvent.mousePosition.y);
             if(cEvent.type == EventType.mouseDown && cEvent.button == 0)
             {
                 currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / **tileSize**.x));
                 currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / **tileSize**.y));
                 Repaint();
             }

             GUI.EndScrollView();
         }
     }
 }

}

The issue is on Line 72 and 73 also: currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / tileSize.x)); currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / tileSize.y));

Im a noob, bear with me.

Sorry for my grammar and thank you for your attention.

ps: the Vector2 tileSize is inherited from other class called TileMap.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kristof1104 · Jun 21, 2016 at 03:59 PM

Hi,

You need to access the variable tileSize through a instance of the class TileMap, Looking at your code I'm assuming your selection object is of the type TileMap. So change the 2 lines to:

 currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / selection.tileSize.x));
 currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / selection.tileSize.y));

Let me know if this fixes your problem. Greetz

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JunglePeople · Jun 21, 2016 at 04:33 PM 0
Share

Hi kristof1104 , thanks for your reply.

I've tried this, didn't worked, don't know why.

avatar image kristof1104 JunglePeople · Jun 21, 2016 at 06:22 PM 0
Share

Do you get the same error or another one?

avatar image JunglePeople kristof1104 · Jun 21, 2016 at 07:58 PM 0
Share

In this sittuation, I don't get any error, but my Tile Picker becomes invisible, like disabled.

avatar image
0

Answer by onsboy · Aug 15, 2018 at 04:34 PM

it will be : -

 currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / tile.x));
 currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / tile.y));

since tile.x and tile.y is the new tile size that is multiplied with the scale factor...I hope this solves this issue.because the code is correct and i have completed the same Tutorial and its right.. However Jesse Freeman is a great coder but his teaches with bare minimal explanation of his code and you often have to work your way out through the code and improvise.

Having said that i will buy his future courses for sure because he is a awesome guy with great coding knowledge. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Raycast2D over Tilemap? 0 Answers

How to dynamically set what Tiles are being rendered in the 2017.2 Tilemap? 0 Answers

Tilemap saved as a prefab breaks 1 Answer

Script for creating 2d map of non-random tiles 1 Answer

Tile script: Check if TileMap is a palette or not? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges