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 jpvanmuijen · Jan 04, 2013 at 03:15 PM · texturesarqualcomm

Select an object, then change the texture

Hi,

I have a number of different objects and a number of textures which people can apply to those objects.

So what I would like; someone clicks (in this case, touches, since I'm using Qualcomm AR) an object, after which they can select one of the textures to apply to that object.

This works fine when I display a set of texture triggers for each of the objects seperately, but I would like to have just one set of textures which can be applied to each one of the objects.

Attached is a Mondriaan-like illustration of what I am trying to achieve.

Currently, I have these lines of code;

 var Object01 : GameObject;
 Object01 = GameObject.Find("Object01");
 var Object02 : GameObject;
 Object02 = GameObject.Find("Object02");
 
 function Start () {
 
 }
 
 function Update () {
 
     var hit : RaycastHit;
 
     var touches = Input.touches;
 
     for (var touch : Touch in touches){
 
         var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
 
         if (Physics.Raycast (ray,hit,Mathf.Infinity)){
             
             // Object01 Texture Triggers
             
             if(hit.collider.name == ("Object01White")&& touch.phase == TouchPhase.Began){
                 Object01.renderer.material.mainTexture = null;
                 Object01.renderer.material.color = Color.white;
             }   
             if(hit.collider.name == ("Object01Black")&& touch.phase == TouchPhase.Began){
                 Object01.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                 Object01.renderer.material.mainTextureScale = Vector2 (6,6);
             }            
             if(hit.collider.name == ("Object01Red")&& touch.phase == TouchPhase.Began){
                 Object01.renderer.material.mainTexture = Resources.Load("bright_red");
                 Object01.renderer.material.mainTextureScale = Vector2 (8,8);
             }                 
             
             // Object02 Texture Triggers
             
             if(hit.collider.name == ("Object02White")&& touch.phase == TouchPhase.Began){
                 Object02.renderer.material.mainTexture = null;
                 Object02.renderer.material.color = Color.white;
             }   
             if(hit.collider.name == ("Object02Black")&& touch.phase == TouchPhase.Began){
                 Object02.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                 Object02.renderer.material.mainTextureScale = Vector2 (6,6);
             }            
             if(hit.collider.name == ("Object02Red")&& touch.phase == TouchPhase.Began){
                 Object02.renderer.material.mainTexture = Resources.Load("bright_red");
                 Object02.renderer.material.mainTextureScale = Vector2 (8,8);
             } 
 
         }
 
     }
 
 
 }

As you can see, this will result in -a lot- of redundant code.

My best guess would be to replace Object01/Object02/etc with something generic which is generated by clicking one of the objects.

I think I have the pieces, now I'm just wondering how they would fit together.

Any help would be very much appreciated.

[1]: /storage/temp/6379-unitytextureswitch.png

unitytextureswitch.png (11.7 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jpvanmuijen · Jan 04, 2013 at 07:29 PM

Got it!

 // Define objects and selected object
 
 var Object01: GameObject;
 Object01 = GameObject.Find("Object01");

 var Object02 : GameObject;
 Object02 = GameObject.Find("Object02");

 var selectedObject: GameObject;
 
 function Start () {
 
 }
 
 function Update () {
 
     var hit : RaycastHit;
 
     var touches = Input.touches;
 
     for (var touch : Touch in touches){
 
         var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
 
         if (Physics.Raycast (ray,hit,Mathf.Infinity)){
 
            // Find out what object is hit and store in selectedObject

            if(hit.collider.name == ("Object01")&& touch.phase == TouchPhase.Began){
                 selectedObject= GameObject.Find("Object01");
             }   
            if(hit.collider.name == ("Object02")&& touch.phase == TouchPhase.Began){
                 selectedObject = GameObject.Find("Object02");
             }   

             // Set texture for the selected object

             if(hit.collider.name == ("TextureWhite")&& touch.phase == TouchPhase.Began){
                 selectedObject.renderer.material.mainTexture = null;
                 selectedObject.renderer.material.color = Color.white;
             }   
             if(hit.collider.name == ("TextureBlack")&& touch.phase == TouchPhase.Began){
                 selectedObject.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                 selectedObject.renderer.material.mainTextureScale = Vector2 (6,6);
             }     
         }
 
     }
 }

Not sure if it's the most elegant or efficient way, but it works!

Thanks a lot for the pointer Vonni.

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 Vonni · Jan 04, 2013 at 04:02 PM

Not real code

  • Raycast on touch

  • Set object hit to a variable var selectedObject : GameObject;

  • On click texture buttons: selectedObject.renderer.material.mainTexture = Resources.Load("bright_red"); selectedObject.renderer.material.mainTextureScale = Vector2 (8,8);

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 jpvanmuijen · Jan 04, 2013 at 04:38 PM 0
Share

Thanks, that actually makes sense.

I'm just struggling to set the object hit to a variable i.e. where it should go (sorry, coding newbie).

This is what I've changed it to;

 function Start () {
 
 }
 
 function Update () {
 
     var hit : RaycastHit;
 
     var touches = Input.touches;
 
     for (var touch : Touch in touches){
 
         var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
 
         if (Physics.Raycast (ray,hit,$$anonymous$$athf.Infinity)){            
             
             var selectedObject : GameObject;
             
             if(hit.collider.name == ("TextureWhite")&& touch.phase == TouchPhase.Began){
                 selectedObject.renderer.material.mainTexture = null;
                 selectedObject.renderer.material.color = Color.white;
             }   
             if(hit.collider.name == ("TextureBlack")&& touch.phase == TouchPhase.Began){
                 selectedObject.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                 selectedObject.renderer.material.mainTextureScale = Vector2 (6,6);
             }            
 
         }
 
     }
 
 }

Do I still need the variables at the beginning of my previous code?

Also, the texture switches are objects themselves, how do I make sure they don't change their own texture (since that's the last object stored in selectedObject)?

Just another little nudge and I should be fine, thanks again!

avatar image
0

Answer by hollym16 · Dec 17, 2014 at 02:02 PM

Hey, I'm doing something similar to this…. well actually the opposite. Where you have selected the object then set the colour, I want to select the colour then apply it to an object. Any ideas on how to achieve this? You're code was a great starting point :)

(Also I know this isn't an answer but I know notifications are a bit weird for this, just wanted to make sure it s seen)

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Trying to use a basic combat system 2 Answers

GUI button's disappearing behind full screen Texture 1 Answer

Converting Autowaypoint (JS) to C# 1 Answer

Can't save script 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