Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ErhMuhGurd · Aug 30, 2014 at 06:46 PM · colordetectionmatching

Detecting color match

Is there a way for Unity to detect color matches between assets? Please refer to the image below. Notice that the two red A letters are on opposite sides.

alt text

In the image below notice that the two red A letters are matched. Is there a way to get Unity to detect that the colors match?

alt text

unmatched.png (2.4 kB)
matched.png (2.5 kB)
Comment
Add comment · Show 3
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 robertbu · Aug 30, 2014 at 08:35 PM 0
Share

There's nothing special in Unity that does this, but there are a number of different ways it can be solved. How difficult or easy it might be will depending on a lot of factors. You need to give a more detailed explanation of your app. Things like:

  • 2D or 3D?

  • Number of tiles

  • Are the tiles placed by the user?

  • Are the tiles moved by the user?

  • Can the tiles be rotated?

  • Is color the only match or do the symbols have to match as well?

  • Etc.

avatar image dsada · Aug 30, 2014 at 08:53 PM 0
Share

Assu$$anonymous$$g these are not drawn by GUI you can check if their material's color are the same with a code something like this:

 GameObject letter1;
 GameObject letter2;
 
 if(letter1.renderer.shared$$anonymous$$aterial.color == letter2.renderer.shared$$anonymous$$aterial.color)
 {
     //same color
 }

I wrote shared$$anonymous$$aterial because in this case it works and does not make a copy of the material like renderer.material.

avatar image ErhMuhGurd · Aug 31, 2014 at 12:26 AM 0
Share

Robertbu,

To answer your questions...

2D or 3D? - Game is in 2D

Number of tiles - Number of tiles will be different for each puzzle. Game starts out with 4 tiles and then 9 tiles and so on, as the levels get harder.

Are the tiles placed by the user? - The tiles are not placed by the user. They are stationary but can be flipped vertically and horizontally. To make sure I am understood, the tiles can flip horizontally, or vertically but will always remain at the same coordinates.

Are the tiles moved by the user? - Player can flip the tiles as stated above on$$anonymous$$ouseDown

Can the tiles be rotated? - I will assume you mean clockwise and counterclockwise. No.

Is color the only match or do the symbols have to match as well? - Only the colors have to match

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Aug 30, 2014 at 10:45 PM

Based on your diagram you only intend to have a limited number of colours. So the best solution would be to forget about colours altogether. Define a property on the GameObject that represents the colour. Then compare the property directly. An enum might be a good idea. You can then use this enum to determine what colour to place on the object.

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
avatar image
0

Answer by robertbu · Aug 31, 2014 at 06:16 AM

While it is possible to raycast and check texture color, typically this kind of problem is solved by building data structures, and checking data. Here is an example script. This script would go on each tile:

 using UnityEngine;
 using System.Collections;
 
 public class ColorMatch : MonoBehaviour {
 
     public enum Colors {
         RED, 
         GREEN,
         BLUE,
         YELLOW,
         WHITE,
         BLACK,
         GREY,
         PURPLE,
         ORANGE,
         }
 
     public Colors top;
     public Colors right;
     public Colors bottom;
     public Colors left;
 
     public ColorMatch cmTop;
     public ColorMatch cmRight;
     public ColorMatch cmBottom;
     public ColorMatch cmLeft;
 
 
     void OnMouseDown() {
         Debug.Log ("Here");
         if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
             transform.Rotate (180,0,0);
             Colors tmp = top;
             top = bottom;
             bottom = tmp;
         }
         else {
             transform.Rotate (0,180,0);
             Colors tmp = right;
             right = left;
             left = tmp;
         }
         CheckColors();
     }
 
     void CheckColors() {
         if (cmTop != null && cmTop.bottom == top) {
             Debug.Log ("Top color matches");
         }
         if (cmRight != null && cmRight.left == right) {
             Debug.Log ("Right color matches");
         }
         if (cmBottom != null && cmBottom.top == bottom) {
             Debug.Log ("Bottom color matches");
         }
         if (cmLeft != null && cmLeft.right == left) {
             Debug.Log ("Left color matches");
         }
 
     }
 }

After you attach this script to a tile, you will see in the inspector that top, right, bottom, and left are all assigned the color RED. You need to pick the appropriate color for each side of the tile. If you are going to have a bunch of the same tile, it would pay to do a prefab and therefore only have to assign the colors one for each tile. You can edit the Colors enum to only contain the colors used on your tiles.

cmTop, cmRight, cmBottom, and cmLeft are references to the ColorMatch scripts of the adjacent tiles. If there is no adjacent tile, the value is left to 'None' which is null. You can do this by drag and drop. Layout a pattern of tiles. Name the game objects in the hierarchy so you know by their names their position, select a tile, and then drag and drop the appropriate tile into the appropriate slot. For example say you had a layout of four tiles, the top left tile would have a neighbor to the right and a neighbor below. You would select the tile and drag the bottom left tile into the cmBottom slot and the top right tile into the cmRight slot.

All this dragging and dropping can be time consuming and error prone. After you verify the logic by setting up a 2x2 by hand, I'd work on a way to automatically do the layout and assignment of the cmTop, cmRight, cmBottom, and cmLeft variables.

The script just has something simple for the flipping. Shift-click rotate the tile around the horizontal axis, and click rotates it around the vertical. Note how the value of 'top' and 'bottom' are switched when the tile is rotated horizontally, and the swap of 'left' and 'right' for a vertical rotation. This means that top, right, bottom and left will always have the correct "world" values no matter how the tile is flipped.

The CheckColors() function is pretty straight forward. For example, you check to see if there is a tile above you, check to see if cmTop is not null. If it is not, you check this tile's 'top' against the bottom of the tile above.

Whew, it took a lot longer to explain it then it did to write the code. This should get you started.

Comment
Add comment · Show 1 · 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 OctoMan · Jan 09, 2015 at 11:31 PM 0
Share

Hi robertbu, how would you reach the same goal if the player is able to drag the tiles itself into the field? I plan to do something similar, but am a bit stuck there, since there are no fixed positions at all.

I hope you read it and can give me a hint. Btw i do it in 3D but only compare the top of the tile. Like you do in 2d.

Thanks in advance

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Color variable matching 2 Answers

Changing two different objects renderer colour 1 Answer

Game says colors are different but they actually aren't? 1 Answer

Pattern Detection 1 Answer


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